* Copyright (c) 2001-2008 Platon Group, http://platon.sk/
* All rights reserved.
*
* See README file for more information about this software.
* See COPYING file for license information.
*
* Download the latest version from
* http://platon.sk/projects/Metafox/
*/
/* $Platon: Metafox/subst.inc.php,v 1.65 2015/10/27 11:14:14 igor Exp $ */
require_once 'cfgldr.inc.php';
require_once 'error.inc.php';
require_once 'functions.inc.php';
require_once 'db-core/db-core.inc.php';
require_once 'db.inc.php';
if ($ezin_cfg['debug_level'] == 10) {
ezin_cache_clear('');
}
/*
* Include libraries
*/
require_once 'HTML/Template/Platon.php';
HTML_Template_Platon::setLoadFunction('ezin_load_template');
if (isset($ezin_sys['weblog']['dsn']) && strlen($ezin_sys['weblog']['dsn']) > 0) {
include_once 'lib/phpWebLogAnalyzer/phpWebLogAnalyzer.inc.php';
$ezin_weblog = new Web_Log($ezin_sys['weblog']['dsn'],
$ezin_cfg['session_timeout'], 2 /* ident timeout */);
} else {
$ezin_weblog = false;
}
/*
* Input parameters parsing
*/
ezin_cgi_parse();
/*
* Mandatory HTTP host check
*/
$ezin_https = ezin_get_schema() == 'https';
switch ($ezin_cfg['https_admin']) {
case 0: // Always OFF
$https_admin_redirect = false;
break;
case 1: // Auto-detect
$https_admin_redirect = ! $ezin_https
&& strlen($_SERVER['HTTP_HOST']) > 0;
break;
case 2: // Always ON
$https_admin_redirect = true;
break;
}
if ($ezin_cgi['output'] == 'admin'
&& $https_admin_redirect
&& ! $ezin_https)
{
$socket = @fsockopen('ssl://'.$_SERVER['HTTP_HOST'], 443);
if (is_resource($socket)) {
$req_str = sprintf("HEAD %s HTTP/1.1\r\n", $_SERVER['REQUEST_URI']);
$req_str .= sprintf("Host: %s\r\n", $_SERVER['HTTP_HOST']);
$req_str .= "Connection: close\r\n\r\n";
fwrite($socket, $req_str);
stream_set_blocking($socket, true);
stream_set_timeout($socket, 1);
$data = fread($socket, 100);
if (preg_match('/^HTTP.*\s*200\s*/', $data)) {
Platon::redirect('https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
}
}
}
$ezin_http_host = strtolower($_SERVER['HTTP_HOST']);
// Set language by HTTP host
if (strlen($ezin_cfg['http_host_language']) > 0) {
preg_match_all('|(\w\w)[^\(]*\(([^\)]*)\)|', $ezin_cfg['http_host_language'], $matches);
foreach ($matches[1] as $index => $lang) {
$hosts = preg_split('/(\s+|\xc2\xa0)/', $matches[2][$index]);
if (in_array($ezin_http_host, $hosts)) {
$ezin_cgi['lang'] = $lang;
break;
}
}
}
if (strlen($ezin_cfg['http_host_redirect']) > 0 && strcasecmp($_SERVER['REQUEST_METHOD'], 'POST')) { /* {{{ */
$ezin_redirect_ar = preg_split('/(\s+|\xc2\xa0)/', $ezin_cfg['http_host_redirect']);
if (count($ezin_redirect_ar) > 1) {
$ezin_redirect_new = array();
foreach ($ezin_redirect_ar as $val) {
if (preg_match('|^(\w\w)\((.*)\)$|', $val, $matches)) {
$ezin_redirect_new[strtolower($matches[1])] = $matches[2];
// if ($ezin_cgi['lang_init'] >= 2 && ! strcasecmp($matches[2], $ezin_http_host)) {
// $ezin_cgi['lang'] = strtolower($matches[1]);
// }
} else {
$ezin_redirect_new[''] = $val;
}
}
if (isset($ezin_redirect_new[$ezin_cgi['lang']])) {
$ezin_redirect_url = $ezin_redirect_new[$ezin_cgi['lang']];
} else {
$ezin_redirect_url = @$ezin_redirect_new[$ezin_cgi['']];
}
} else {
$ezin_redirect_url = $ezin_redirect_ar[0];
}
$ezin_redirect_ar = @parse_url($ezin_redirect_url);
if (@strlen($ezin_redirect_ar['scheme']) <= 0) {
$ezin_redirect_ar = @parse_url(
($ezin_https ? 'https' : 'http')
.'://'.$ezin_redirect_url);
}
if (strlen($ezin_redirect_ar['host']) > 0 && strlen($ezin_http_host) > 0
&& strcasecmp($ezin_redirect_ar['host'], $ezin_http_host)) {
//die("here: ".$ezin_http_host.'-'.$ezin_redirect_ar['host']);
$ezin_http_host_allowed_ar = preg_split('/(\s+|\xc2\xa0)/', $ezin_cfg['http_host_allowed']);
$ezin_http_host_forbidden_ar = preg_split('/(\s+|\xc2\xa0)/', $ezin_cfg['http_host_forbidden']);
if (count($ezin_http_host_forbidden_ar) <= 0
|| in_array($ezin_http_host, $ezin_http_host_forbidden_ar)) {
if (! in_array($ezin_http_host, $ezin_http_host_allowed_ar)) {
if (! isset($ezin_redirect_ar['path'])) {
$ezin_redirect_ar['path'] = '/';
}
$append_path = Platon::str_replace_prefix(
Platon::append_slash($ezin_cfg['url']), '', $_SERVER['REQUEST_URI']);
$ezin_redirect_ar['path'] .= ($append_path == '/' && substr($ezin_redirect_ar['path'], -1) == '/')
? ''
: $append_path;
Platon::redirect(Platon::glue_url($ezin_redirect_ar));
}
}
}
} /* }}} */
if ($ezin_cgi['lang'] != @$_COOKIE['ezin_lang']) {
$expire_time = time() + (3600 * 24 * 30 * 12 * 2); // two years
setcookie('ezin_lang', $ezin_cgi['lang'], $expire_time, $ezin_cfg['url']);
}
/*
* Check number of articles after SYSDATE
* (the same principle as 'version')
*/
$ezin_atodo_real = ezin_get_articles_todo();
$ezin_atodo_cached = ezin_cache_load_content('atodo');
if ($ezin_atodo_cached === false) {
ezin_cache_save('atodo', $ezin_atodo_real);
} elseif (intval($ezin_atodo_real) != intval($ezin_atodo_cached)) {
ezin_cache_clear('');
ezin_cache_save('atodo', $ezin_atodo_real, 0, 0);
}
/*
* Write counts
*/
ezin_update_counts_temp();
ezin_update_counts($ezin_cgi['author_id'], $ezin_cgi['section_id'], $ezin_cgi['article_id']);
/*
* Get section parameters
*/
$ezin_tmpl_key = '';
$ezin_db_author = '';
$ezin_db_section = '';
$ezin_db_article = '';
$ezin_db_poll = '';
if ($ezin_cgi['author_id'] > 0 || $ezin_cgi['output'] == 'author' || $ezin_cgi['output'] == 'author_edit') { /* {{{ */
//var_dump($ezin_cgi);
$ezin_db_author = ezin_get_author($ezin_cgi['author_id']);
if (! empty($ezin_db_author['author_template_key'])) {
$ezin_tmpl_key .= $ezin_sys['tmpl']['delim'].$ezin_db_author['author_template_key'];
}
if (! empty($ezin_db_author['author_link']) && $ezin_cgi['output'] == 'author') {
ezin_handle_author_link($ezin_db_author['author_link']);
}
$name = $ezin_db_author['author_name'];
$ezin_db_author['search'] = $ezin_db_author['author_id'];
if (strlen($name) > 0) {
if (preg_match('|^(\S*)\s*(.*)|', $name, $matches)) {
$ezin_db_author['search'] = $matches[2];
} else if (preg_match('|^(\S*),\s*(\S*)|', $name)) {
$ezin_db_author['search'] = $matches[1];
}
}
$ezin_tmpl_key .= $ezin_sys['tmpl']['delim'].'author';
} /* }}} */
if (! empty($ezin_cgi['article_id'])) { /* {{{ */
$ezin_db_article = ezin_get_article($ezin_cgi['article_id']);
if (! empty($ezin_db_article['article_template_key'])) {
$ezin_tmpl_key .= $ezin_sys['tmpl']['delim'].$ezin_db_article['article_template_key'];
}
} /* }}} */
/* {{{ section - always (at least editorial is fetched) */
$ezin_db_section = ezin_get_section($ezin_cgi['section_id'], $ezin_cgi['article_id']);
if (! empty($ezin_db_section['section_template_key'])) {
/* What si not good here: if we have "author" page, we add also section
template key, what is not neccessary and most-likely unwanted. */
$ezin_tmpl_key .= $ezin_sys['tmpl']['delim'].$ezin_db_section['section_template_key'];
}
if (! empty($ezin_db_section['section_link']) && $ezin_cgi['output'] == 'section') {
ezin_handle_section_link($ezin_db_section['section_link']);
}
/* }}} */
/*
* Main template object
*/
if (strlen($_REQUEST['ezin_template_key']) > 0) {
$ezin_tmpl_key = $ezin_sys['tmpl']['delim'].$_REQUEST['ezin_template_key'].$ezin_tmpl_key;
//$ezin_db_section['section_template_key'] = $_REQUEST['ezin_template_key'];
//$ezin_db_author['author_template_key'] = $_REQUEST['ezin_template_key'];
}
if ($ezin_cgi['output'] == 'notfound'
|| ezin_exist_access_key_redirect($ezin_cgi['notfound_id'], $access_key_redirect['type']))
{
$access_key_redirect = ezin_get_access_key_redirect($ezin_cgi['notfound_id'], $ezin_cgi['input']);
if ($access_key_redirect == false) {
foreach(array('section','article','author','print') as $type) {
$access_key_redirect = ezin_get_access_key_redirect($ezin_cgi['notfound_id'], $type);
if ($access_key_redirect !== false) {
break;
}
}
}
switch($access_key_redirect['type']) {
case 'section':
ezin_redirect_section($access_key_redirect['id']);
break;
case 'article':
ezin_redirect_article($access_key_redirect['id']);
break;
case 'author':
ezin_redirect_author($access_key_redirect['id']);
break;
case 'print':
ezin_redirect_print($access_key_redirect['id']);
break;
}
}
switch ($ezin_cgi['output']) { /* {{{ */
default:
die('input method fatal error: '.$ezin_cgi['output']);
break;
case 'error':
$ezin_tmpl_main_fname = $ezin_sys['tmpl']['error'];
break;
case 'notfound':
$ezin_tmpl_main_fname = $ezin_sys['tmpl']['notfound'];
break;
case 'admin':
// for admin section we need to include this early
if (file_exists($ezin_cfg['theme']['dir'].'/'.$ezin_sys['tmpl']['fnc_file'])) {
include_once $ezin_cfg['theme']['dir'].'/'.$ezin_sys['tmpl']['fnc_file'];
}
$ezin_tmpl_main_fname = $ezin_sys['tmpl']['admin'];
$ezin_tmpl_main_fname .= $ezin_sys['tmpl']['delim'];
$ezin_tmpl_main_fname .= $ezin_auth->getAuth() && $ezin_admin_action != 'logout'
? 'logged' : 'notlogged';
break;
case 'message':
$ezin_tmpl_main_fname = $ezin_sys['tmpl']['message_edit'].$ezin_tmpl_key;
break;
case 'message_full_list':
case 'message_selected_list':
$ezin_tmpl_main_fname = $ezin_sys['tmpl']['message'].$ezin_tmpl_key;
break;
case 'article_print':
$ezin_tmpl_main_fname = $ezin_sys['tmpl']['article_print'].$ezin_tmpl_key;
break;
case 'article':
$ezin_tmpl_main_fname = $ezin_sys['tmpl']['article'].$ezin_tmpl_key;
break;
case 'section':
$ezin_tmpl_main_fname = $ezin_sys['tmpl']['section'].$ezin_tmpl_key;
break;
case 'author':
if (strlen($ezin_tmpl_key) <= 0) {
$ezin_tmpl_key = $ezin_sys['tmpl']['delim'].'author'; // TODO?
}
$ezin_tmpl_main_fname = $ezin_sys['tmpl']['author'].$ezin_tmpl_key;
break;
case 'author_edit':
if (strlen($ezin_tmpl_key) <= 0) {
$ezin_tmpl_key = $ezin_sys['tmpl']['delim'].'author_edit'; // TODO?
}
$ezin_tmpl_main_fname = $ezin_sys['tmpl']['author_edit'].$ezin_tmpl_key;
break;
} /* }}} */
$ezin_tmpl_main = new HTML_Template_Platon($ezin_tmpl_main_fname);
/*
* Try to include user defined function(s) file
*/
if (file_exists($ezin_cfg['theme']['dir'].'/'.$ezin_sys['tmpl']['fnc_file'])) {
include_once $ezin_cfg['theme']['dir'].'/'.$ezin_sys['tmpl']['fnc_file'];
}
/*
* Substitute current section parameters
*/
if ($ezin_db_author != '') { /* {{{ */
ezin_author_link_process($ezin_db_author);
ezin_author_counts_process($ezin_db_author);
// if author edit page, than do not modify description content
// (because it adds
at the end of lines, etc.)
if ($ezin_cgi['output'] != 'author_edit') {
ezin_handle_special_tags($ezin_db_author['author_description'],
$ezin_db_author['author_description_flags']);
ezin_process_text_flags($ezin_db_author['author_description'], 'author');
}
$ezin_tmpl_main->set($ezin_db_author);
foreach ($ezin_db_author as $key => $val) {
$ezin_tmpl_main->setAlias("ezin_$key", $key);
}
} /* }}} */
if ($ezin_db_section != '') { /* {{{ */
ezin_section_link_process($ezin_db_section);
ezin_section_counts_process($ezin_db_section);
$ezin_db_section['section_editorial_text'] =Platon::tilde_replace($ezin_db_section['section_editorial_text']);
$ezin_db_section['section_editorial_title']=Platon::tilde_replace($ezin_db_section['section_editorial_title']);
$ezin_db_section['section_parent_id2'] = explode('::', $ezin_db_section['section_id2']);
$ezin_db_section['section_parent_id2'] = $ezin_db_section['section_parent_id2'][0];
if (isset($ezin_db_section['section_editorial_timestamp'])) {
$ezin_db_section['section_editorial_date'] =
date($ezin_cfg['format_date'], $ezin_db_section['section_editorial_timestamp']);
$ezin_db_section['section_editorial_datetime'] =
date($ezin_cfg['format_datetime'], $ezin_db_section['section_editorial_timestamp']);
} else {
$ezin_db_section['section_editorial_date'] = '';
$ezin_db_section['section_editorial_datetime'] = '';
}
if (isset($ezin_db_section['section_editorial_text'])) {
ezin_handle_special_tags($ezin_db_section['section_editorial_text'],
$ezin_db_section['section_editorial_text_flags']);
ezin_process_text_flags($ezin_db_section['section_editorial_text'], 'editorial');
} else {
$ezin_db_section['section_editorial_text'] = '';
}
$ezin_tmpl_main->set($ezin_db_section);
foreach ($ezin_db_section as $key => $val) {
$ezin_tmpl_main->setAlias("ezin_$key", $key);
}
} /* }}} */
/*
* Build up sections list
*/
if ($ezin_tmpl_main->isVar('section_items')) { /* {{{ */
$cache_id = 'S'.@$ezin_cgi['lang'].$ezin_tmpl_key;
$cache_ar = ezin_cache_load($cache_id);
if (intval($cache_ar['timestamp']) <= 0) {
$item = new HTML_Template_Platon($ezin_sys['tmpl']['section_item'].$ezin_tmpl_key,
HTML_TEMPLATE_PLATON_METHOD_VAR_KEEP);
$cache_ar['var_var'] = $item->isVar('section_selected')
|| $item->isVar('section_accesses_count')
|| $item->isVar('section_raw_accesses_count')
|| $item->isVar('section_sessions_count')
|| $item->isVar('section_raw_sessions_count');
$sections = ezin_get_section_list($ezin_cgi['lang'], $ezin_cgi['section_id']);
$cache_ar['content'] = '';
for ($k = 0; $k < count($sections); $k++) {
$item->reload();
ezin_section_link_process($sections[$k]);
ezin_section_counts_process($sections[$k]);
if ($cache_ar['var_var']) {
$id = $sections[$k]['section_id'];
$item->setVar('section_selected', 'section_selected_'.$id);
$item->setVar('section_accesses_count', 'section_accesses_count_'.$id);
$item->setVar('section_sessions_count', 'section_sessions_count_'.$id);
$item->setVar('section_raw_accesses_count', 'section_raw_accesses_count_'.$id);
$item->setVar('section_raw_sessions_count', 'section_raw_sessions_count_'.$id);
}
$item->set($sections[$k]);
$cache_ar['content'] .= $item->getCode();
}
//$cache_ar['content'] = Platon::tilde_replace($cache_ar['content']);
unset($sections);
ezin_cache_save($cache_id, $cache_ar['content'], $cache_ar['var_var']);
}
$ezin_tmpl_main->setCode('section_items', $cache_ar['content']);
if ($cache_ar['var_var']) {
$ezin_tmpl_main->set(ezin_get_section_list_counts($ezin_cgi['lang'], $ezin_cgi['section_id']));
}
} /* }}} */
/*
* Building articles list
*/
if ($ezin_tmpl_main->isVar('article_items') && ($ezin_db_section != '' || $ezin_db_author != '')) { /* {{{ */
$list_var_keep = $ezin_cgi['output'] != 'section';
$cache_id = $ezin_db_author != ''
? 'U'.@$ezin_db_author['author_id'].@$ezin_cgi['lang'].$ezin_tmpl_key
: 'S'.@$ezin_db_section['section_id'].$ezin_tmpl_key.($list_var_keep ? '-VarKeep' : '');
$cache_ar = ezin_cache_load($cache_id);
$section_id = $ezin_db_section != '' && (strpos($ezin_db_section['section_type2'], 'global') === false) ? $ezin_db_section['section_id'] : 0;
$author_id = $ezin_db_author != '' ? $ezin_db_author['search'] : '';
$limit = $ezin_db_section != '' ? intval($ezin_db_section['section_limit']) : 0;
// check sources in section
$sources = ezin_get_sources($section_id);
if (count($sources) > 0) {
$min_time = $sources[0]['normal_refresh_time']; // minutes
foreach($sources as $source) {
$cache_rss_xml = ezin_cache_load('Source'.intval($source['id']));
$refresh_time = ($cache_rss_xml['var_var'] == 1) ? $source['fail_refresh_time'] : $source['normal_refresh_time'];
$min_time = ($min_time > $refresh_time) ? $refresh_time : $min_time;
}
$min_time *= 60; // seconds
// compare time of update for section update
if (($cache_ar['timestamp'] + $min_time) < Time()) {
ezin_cache_clear($cache_id);
$cache_ar = array();
}
}
if (intval($cache_ar['timestamp']) <= 0) {
$articles = ezin_get_article_list($section_id, $author_id, $limit, $ezin_cgi['lang']);
$cache_ar['content'] = ezin_build_content_article($articles);
global $ezin_cfg;
if ($ezin_cfg['article_items_loadmore'] == 1) {
$cache_ar['content'] .= '{ ?'.'>