<?php
/*
=====================================================
 Simple Title 0.1 beta
-----------------------------------------------------
 Author: Nail' R. Davydov (ROZARD)
-----------------------------------------------------
 Jabber: ROZARD@ya.ru
 E-mail: ROZARD@list.ru
-----------------------------------------------------
 © Настоящий программист никогда не ставит
 комментариев. То, что писалось с трудом, должно
 пониматься с трудом. :))
-----------------------------------------------------
 Данный код защищен авторскими правами
=====================================================
*/
if (!defined('NGCMS'))
	exit('HAL');
pluginsLoadConfig();
LoadPluginLang('simple_title_pro', 'config', '', '', '#');
switch ($_REQUEST['action']) {
	case 'list_cat':
		list_cat();
		break;
	case 'list_static':
		list_static();
		break;
	case 'list_news':
		list_news();
		break;
	case 'send_title':
		send_title();
		break;
	case 'clear_cache':
		clear_cache();
		break;
	case 'about':
		about();
		break;
	case 'license':
		license();
		break;
	case 'del_cat':
		del_cat();
		list_cat();
		break;
	case 'del_static':
		del_static();
		list_static();
		break;
	case 'del_news':
		del_news();
		list_news();
		break;
	case 'del_news':
		del_news();
		list_news();
		break;
	default:
		main();
}
function about()
{

	global $twig;
	$tpath = locatePluginTemplates(array('main', 'about'), 'simple_title_pro', 1);
	$xt = $twig->loadTemplate($tpath['about'] . 'about.tpl');
	$tVars = array();
	$xg = $twig->loadTemplate($tpath['main'] . 'main.tpl');
	$tVars = array(
		'global'  => 'О плагине',
		'entries' => $xt->render($tVars)
	);
	print $xg->render($tVars);
}

function clear_cache()
{

	global $userROW;
	$dir = get_plugcache_dir('simple_title_pro');
	if (is_dir($dir)) {
		if ($dh = opendir($dir)) {
			while (($file = readdir($dh)) !== false) {
				@unlink($dir . $file);
			}
			closedir($dh);
		}
	}
	$_SESSION['simple_title_pro']['notify'] = $userROW['name'] . ', кэш очищен';
	redirect_simple_title_pro('?mod=extra-config&plugin=simple_title_pro');
}

function list_news()
{

	global $twig, $mysql;

	// Отображение уведомлений
	if (isset($_SESSION['simple_title_pro']['notify'])) {
		msgSticker($_SESSION['simple_title_pro']['notify']);
		unset($_SESSION['simple_title_pro']['notify']);
	}
	if (isset($_SESSION['simple_title_pro']['notify_error'])) {
		msgSticker($_SESSION['simple_title_pro']['notify_error'], 'error');
		unset($_SESSION['simple_title_pro']['notify_error']);
	}

	$tpath = locatePluginTemplates(array('main', 'list_news', 'list_news_entries'), 'simple_title_pro', 1);
	$news_per_page = pluginGetVariable('simple_title_pro', 'num_news');
	if (($news_per_page < 2) || ($news_per_page > 2000)) $news_per_page = 2;
	$pageNo = intval($_REQUEST['page']) ? $_REQUEST['page'] : 0;
	if ($pageNo < 1) $pageNo = 1;
	if (!$start_from) $start_from = ($pageNo - 1) * $news_per_page;
	$count = $mysql->result('SELECT count(*) FROM ' . prefix . '_simple_title_pro as s LEFT JOIN ' . prefix . '_news as n ON (n.id = s.news_id) WHERE s.news_id != 0');
	$countPages = ceil($count / $news_per_page);
	foreach ($mysql->select('SELECT s.id as sid, s.title as stitle, n.title as name FROM ' . prefix . '_simple_title_pro as s LEFT JOIN ' . prefix . '_news as n ON (n.id = s.news_id) WHERE s.news_id != 0 ORDER BY s.id ASC LIMIT ' . $start_from . ', ' . $news_per_page) as $row) {
		$xe = $twig->loadTemplate($tpath['list_news_entries'] . 'list_news_entries.tpl');
		$tVars = array(
			'title' => $row['stitle'],
			'id'    => $row['sid'],
			'name'  => '<a href="?mod=extra-config&plugin=simple_title_pro&action=send_title&do=news&edit=' . $row['sid'] . '"  />' . $row['name'] . '</a>',
			'del'   => '<a href="?mod=extra-config&plugin=simple_title_pro&action=del_news&id=' . $row['sid'] . '"  />Удалить</a>',
		);
		$entries .= $xe->render($tVars);
	}
	$xt = $twig->loadTemplate($tpath['list_news'] . 'list_news.tpl');
	$tVars = array(
		'pagesss' => generateAdminPagelist(
			array(
				'current' => $pageNo,
				'count'   => $countPages,
				'url'     => admin_url . '/admin.php?mod=extra-config&plugin=simple_title_pro&action=list_news&page=%page%'
			)
		),
		'entries' => $entries
	);
	$xg = $twig->loadTemplate($tpath['main'] . 'main.tpl');
	$tVars = array(
		'global'  => 'Список новостей',
		'entries' => $xt->render($tVars)
	);
	print $xg->render($tVars);
}

function license()
{

	global $twig;
	$tpath = locatePluginTemplates(array('main', 'about'), 'simple_title_pro', 1);
	$xg = $twig->loadTemplate($tpath['main'] . 'main.tpl');
	$tVars = array(
		'global'  => 'О плагине => Лицензия',
		'entries' => nl2br(base64_decode(' '))
	);
	print $xg->render($tVars);
}

function list_static()
{

	global $twig, $mysql;

	// Отображение уведомлений
	if (isset($_SESSION['simple_title_pro']['notify'])) {
		msgSticker($_SESSION['simple_title_pro']['notify']);
		unset($_SESSION['simple_title_pro']['notify']);
	}
	if (isset($_SESSION['simple_title_pro']['notify_error'])) {
		msgSticker($_SESSION['simple_title_pro']['notify_error'], 'error');
		unset($_SESSION['simple_title_pro']['notify_error']);
	}

	$tpath = locatePluginTemplates(array('main', 'list_static', 'list_static_entries'), 'simple_title_pro', 1);
	$news_per_page = pluginGetVariable('simple_title_pro', 'num_static');
	if (($news_per_page < 2) || ($news_per_page > 2000)) $news_per_page = 2;
	$pageNo = intval($_REQUEST['page']) ? $_REQUEST['page'] : 0;
	if ($pageNo < 1) $pageNo = 1;
	if (!$start_from) $start_from = ($pageNo - 1) * $news_per_page;
	$count = $mysql->result('SELECT count(*) FROM ' . prefix . '_simple_title_pro as s LEFT JOIN ' . prefix . '_static as n ON (n.id = s.static_id) WHERE s.static_id != 0');
	$countPages = ceil($count / $news_per_page);
	foreach ($mysql->select('SELECT s.id as sid, s.title as stitle, n.title as name FROM ' . prefix . '_simple_title_pro as s LEFT JOIN ' . prefix . '_static as n ON (n.id = s.static_id) WHERE s.static_id != 0 ORDER BY s.id ASC LIMIT ' . $start_from . ', ' . $news_per_page) as $row) {
		$xe = $twig->loadTemplate($tpath['list_static_entries'] . 'list_static_entries.tpl');
		$tVars = array(
			'title' => $row['stitle'],
			'id'    => $row['sid'],
			'name'  => '<a href="?mod=extra-config&plugin=simple_title_pro&action=send_title&do=static&edit=' . $row['sid'] . '"  />' . $row['name'] . '</a>',
			'del'   => '<a href="?mod=extra-config&plugin=simple_title_pro&action=del_static&id=' . $row['sid'] . '"  />Удалить</a>',
		);
		$entries .= $xe->render($tVars);
	}
	$xt = $twig->loadTemplate($tpath['list_static'] . 'list_static.tpl');
	$tVars = array(
		'pagesss' => generateAdminPagelist(
			array(
				'current' => $pageNo,
				'count'   => $countPages,
				'url'     => admin_url . '/admin.php?mod=extra-config&plugin=simple_title_pro&action=list_static&page=%page%'
			)
		),
		'entries' => $entries
	);
	$xg = $twig->loadTemplate($tpath['main'] . 'main.tpl');
	$tVars = array(
		'global'  => 'Список статиков',
		'entries' => $xt->render($tVars)
	);
	print $xg->render($tVars);
}

function list_cat()
{

	global $twig, $mysql;

	// Отображение уведомлений
	if (isset($_SESSION['simple_title_pro']['notify'])) {
		msgSticker($_SESSION['simple_title_pro']['notify']);
		unset($_SESSION['simple_title_pro']['notify']);
	}
	if (isset($_SESSION['simple_title_pro']['notify_error'])) {
		msgSticker($_SESSION['simple_title_pro']['notify_error'], 'error');
		unset($_SESSION['simple_title_pro']['notify_error']);
	}

	$tpath = locatePluginTemplates(array('main', 'list_cat', 'list_cat_entries'), 'simple_title_pro', 1);
	$news_per_page = pluginGetVariable('simple_title_pro', 'num_cat');
	if (($news_per_page < 2) || ($news_per_page > 2000)) $news_per_page = 2;
	$pageNo = intval($_REQUEST['page']) ? $_REQUEST['page'] : 0;
	if ($pageNo < 1) $pageNo = 1;
	if (!$start_from) $start_from = ($pageNo - 1) * $news_per_page;
	$count = $mysql->result('SELECT count(*) FROM ' . prefix . '_simple_title_pro as s LEFT JOIN ' . prefix . '_category as c ON (c.id = s.cat_id) WHERE s.cat_id != 0');
	$countPages = ceil($count / $news_per_page);
	foreach ($mysql->select('SELECT s.id as sid, s.title as stitle, c.name as cname FROM ' . prefix . '_simple_title_pro as s LEFT JOIN ' . prefix . '_category as c ON (c.id = s.cat_id) WHERE s.cat_id != 0 ORDER BY s.id ASC LIMIT ' . $start_from . ', ' . $news_per_page) as $row) {
		$xe = $twig->loadTemplate($tpath['list_cat_entries'] . 'list_cat_entries.tpl');
		$tVars = array(
			'title'    => $row['stitle'],
			'id'       => $row['sid'],
			'cat_name' => '<a href="?mod=extra-config&plugin=simple_title_pro&action=send_title&do=cat&edit=' . $row['sid'] . '"  />' . $row['cname'] . '</a>',
			'del'      => '<a href="?mod=extra-config&plugin=simple_title_pro&action=del_cat&id=' . $row['sid'] . '"  />Удалить</a>',
		);
		$entries .= $xe->render($tVars);
	}
	$xt = $twig->loadTemplate($tpath['list_cat'] . 'list_cat.tpl');
	$tVars = array(
		'pagesss' => generateAdminPagelist(
			array(
				'current' => $pageNo,
				'count'   => $countPages,
				'url'     => admin_url . '/admin.php?mod=extra-config&plugin=simple_title_pro&action=list_cat&page=%page%'
			)
		),
		'entries' => $entries
	);
	$xg = $twig->loadTemplate($tpath['main'] . 'main.tpl');
	$tVars = array(
		'global'  => 'Список категорий',
		'entries' => $xt->render($tVars)
	);
	print $xg->render($tVars);
}

function send_title()
{

	global $twig, $mysql, $config;
	$tpath = locatePluginTemplates(array('main', 'send_cat', 'send_news', 'send_static'), 'simple_title_pro', 1);
	$dir = get_plugcache_dir('simple_title_pro');
	$id = intval($_REQUEST['id']);
	foreach ($mysql->select('SELECT * FROM ' . prefix . '_simple_title_pro') as $result) {
		$cat[$result['cat_id']] = true;
		$static[$result['static_id']] = true;
		$news[$result['news_id']] = true;
	}
	switch ($_REQUEST['do']) {
		case 'cat':
			$template = $tpath['send_cat'] . 'send_cat.tpl';
			if (isset($_REQUEST['edit']) & !empty($_REQUEST['edit']))
				$frow = $mysql->record('SELECT id, title, cat_id FROM ' . prefix . '_simple_title_pro WHERE id = ' . intval($_REQUEST['edit']) . '  LIMIT 1');
			$options = '';
			foreach ($mysql->select('SELECT id, name FROM ' . prefix . '_category') as $row) {
				if (!$cat[$row['id']] or $row['id'] == $frow['cat_id'])
					$options .= '<option value="' . $row['id'] . '"' . (($row['id'] == $frow['cat_id']) ? 'selected' : '') . '>' . $row['name'] . '</option>';
			}
			if (isset($_REQUEST['submit'])) {
				$title = secure_html(trim($_REQUEST['title']));
				$id = intval($_REQUEST['id'] ?? 0);
				$edit = intval($_REQUEST['edit'] ?? 0);

				// Очищаем массив ошибок
				$error_text = [];

				// Проверка обязательных полей
				if (empty($title)) {
					$error_text[] = 'Title обязателен для заполнения';
				}
				if (empty($id)) {
					$error_text[] = 'ID не передан';
				}

				// Проверка существования записи (исправленная версия)
				$exists = $mysql->result('SELECT COUNT(*) FROM ' . prefix . '_simple_title_pro WHERE cat_id = ' . db_squote($id));
				if ($exists === false) {
					$error_text[] = 'Ошибка проверки записи в базе данных';
				} elseif ($exists > 0 && empty($edit)) {
					$error_text[] = 'Для этой категории уже есть TITLE';
				}

				// Если ошибок нет - выполняем действие
				if (empty($error_text)) {
					if (empty($edit)) {
						// Добавляем новую запись
						$mysql->query('INSERT INTO ' . prefix . '_simple_title_pro (title, cat_id)
                VALUES (' . db_squote($title) . ', ' . db_squote($id) . ')');
					} else {
						// Обновляем существующую запись
						$cacheFileName = md5('block_directory_sites_cat' . $frow['cat_id'] . $config['default_lang']) . '.txt';
						cacheStoreFile($cacheFileName, $title, 'simple_title_pro');
						$mysql->query('UPDATE ' . prefix . '_simple_title_pro SET
                title = ' . db_squote($title) . ',
                cat_id = ' . db_squote($id) . '
                WHERE id = ' . db_squote($edit));
					}
					redirect_simple_title_pro('?mod=extra-config&plugin=simple_title_pro&action=list_cat');
				}
			}
			break;
		case 'news':
			$template = $tpath['send_news'] . 'send_news.tpl';
			if (isset($_REQUEST['edit']) & !empty($_REQUEST['edit']))
				$frow = $mysql->record('SELECT id, title, news_id FROM ' . prefix . '_simple_title_pro WHERE id = ' . intval($_REQUEST['edit']) . '  LIMIT 1');
			$options = '';
			foreach ($mysql->select('SELECT id, title FROM ' . prefix . '_news') as $row) {
				if (!$news[$row['id']] or $row['id'] == $frow['news_id'])
					$options .= '<option value="' . $row['id'] . '"' . (($row['id'] == $frow['news_id']) ? 'selected' : '') . '>' . $row['title'] . '</option>';
			}
			if (isset($_REQUEST['submit'])) {
				$title = secure_html(trim($_REQUEST['title']));
				if (empty($title))
					$error_text[] = 'Титле обязательна для заполнения';
				if (empty($id))
					$error_text[] = 'id не передан';

				// Исправленная строка 292:
				$exists = $mysql->result('SELECT COUNT(*) FROM ' . prefix . '_simple_title_pro WHERE news_id = ' . db_squote($id));
				if ($exists === false) {
					$error_text[] = 'Ошибка проверки наличия записи';
				} elseif ($exists > 0 && empty($_REQUEST['edit'])) {
					$error_text[] = 'Для этой новости уже есть TITLE';
				}

				if (empty($error_text)) {
					if (empty($_REQUEST['edit'])) {
						$mysql->query('INSERT INTO ' . prefix . '_simple_title_pro (title, news_id)
                VALUES (' . db_squote($title) . ', ' . db_squote($id) . ')');
					} else {
						$cacheFileName = md5('block_directory_sites_news' . $frow['news_id'] . $config['default_lang']) . '.txt';
						cacheStoreFile($cacheFileName, $title, 'simple_title_pro');
						$mysql->query('UPDATE ' . prefix . '_simple_title_pro SET
                title = ' . db_squote($title) . ',
                news_id = ' . db_squote($id) . '
                WHERE id = ' . intval($_REQUEST['edit']));
					}
					redirect_simple_title_pro('?mod=extra-config&plugin=simple_title_pro&action=list_news');
				}
			}
			break;
		case 'static':
			$template = $tpath['send_static'] . 'send_static.tpl';
			if (isset($_REQUEST['edit']) & !empty($_REQUEST['edit']))
				$frow = $mysql->record('SELECT id, title, static_id FROM ' . prefix . '_simple_title_pro WHERE id = ' . intval($_REQUEST['edit']) . '  LIMIT 1');
			$options = '';
			foreach ($mysql->select('SELECT id, title FROM ' . prefix . '_static') as $row) {
				if (!$static[$row['id']] or $row['id'] == $frow['static_id'])
					$options .= '<option value="' . $row['id'] . '"' . (($row['id'] == $frow['static_id']) ? 'selected' : '') . '>' . $row['title'] . '</option>';
			}
			if (isset($_REQUEST['submit'])) {
				$title = secure_html(trim($_REQUEST['title']));
				if (empty($title))
					$error_text[] = 'Title обязателен для заполнения';
				if (empty($id))
					$error_text[] = 'ID не передан';

				// Исправленная строка 332:
				$exists = $mysql->result('SELECT COUNT(*) FROM ' . prefix . '_simple_title_pro WHERE static_id = ' . db_squote($id));
				if ($exists === false) {
					$error_text[] = 'Ошибка проверки наличия записи';
				} elseif ($exists > 0 && empty($_REQUEST['edit'])) {
					$error_text[] = 'Для этой статики уже есть TITLE';
				}

				if (empty($error_text)) {
					if (empty($_REQUEST['edit'])) {
						$mysql->query('INSERT INTO ' . prefix . '_simple_title_pro (title, static_id)
                VALUES (' . db_squote($title) . ', ' . db_squote($id) . ')');
					} else {
						$cacheFileName = md5('block_directory_sites_static' . $frow['static_id'] . $config['default_lang']) . '.txt';
						cacheStoreFile($cacheFileName, $title, 'simple_title_pro');
						$mysql->query('UPDATE ' . prefix . '_simple_title_pro SET
                title = ' . db_squote($title) . ',
                static_id = ' . db_squote($id) . '
                WHERE id = ' . intval($_REQUEST['edit']));
					}
					redirect_simple_title_pro('?mod=extra-config&plugin=simple_title_pro&action=list_static');
				}
			}
			break;
		default:
			redirect_simple_title_pro('?mod=extra-config&plugin=simple_title_pro');
	}
	if (!empty($error_text)) {
		foreach ($error_text as $error) {
			$error_input .= '<div class="alert alert-danger">' . $error . '</div>';
		}
	} else {
		$error_input = '';
	}
	$xt = $twig->loadTemplate($template);
	$tVars = array(
		'options' => $options,
		'title'   => empty($frow) ? $title : $frow['title'],
		'error'   => $error_input,
	);
	$xg = $twig->loadTemplate($tpath['main'] . 'main.tpl');
	$tVars = array(
		'global'  => 'Формируем Титл',
		'entries' => $xt->render($tVars)
	);
	print $xg->render($tVars);
}

function list_all()
{

	global $mysql, $twig;
	$tpath = locatePluginTemplates(array('main', 'list_cat', 'list_cat_entries'), 'simple_title_pro', 1);
	foreach ($mysql->select('SELECT s.id as sid, s.title as stitle, n.title as name FROM ' . prefix . '_simple_title_pro as s LEFT JOIN ' . prefix . '_static as n ON (n.id = s.static_id) WHERE s.static_id != 0 ORDER BY s.id ASC') as $row) {
		$xe = $twig->loadTemplate($tpath['list_cat_entries'] . 'list_cat_entries.tpl');
		$tVars = array(
			'title'        => $row['stitle'],
			'id'           => $row['sid'],
			'cat_name'     => '<a href="?mod=extra-config&plugin=simple_title_pro&action=edit&id=' . $row['sid'] . '"  />' . $row['name'] . '</a>',
			'cat_name_del' => '<a href="?mod=extra-config&plugin=simple_title_pro&action=del&id=' . $row['id'] . '" />Удалить</a>',
		);
		$entries .= $xe->render($tVars);
	}
	foreach ($mysql->select('SELECT s.id as sid, s.title as stitle, n.title as name FROM ' . prefix . '_simple_title_pro as s LEFT JOIN ' . prefix . '_news as n ON (n.id = s.news_id) WHERE s.news_id != 0 ORDER BY s.id ASC') as $row) {
		$xe = $twig->loadTemplate($tpath['list_cat_entries'] . 'list_cat_entries.tpl');
		$tVars = array(
			'title'        => $row['stitle'],
			'id'           => $row['sid'],
			'cat_name'     => '<a href="?mod=extra-config&plugin=simple_title_pro&action=cat_edit&id=' . $row['sid'] . '"  />' . $row['name'] . '</a>',
			'cat_name_del' => '<a href="?mod=extra-config&plugin=simple_title_pro&action=cat_name_del&id=' . $row['id'] . '"  />Удалить</a>',
		);
		$entries .= $xe->render($tVars);
	}
	foreach ($mysql->select('SELECT s.id as sid, s.title as stitle, c.name as cname FROM ' . prefix . '_simple_title_pro as s LEFT JOIN ' . prefix . '_category as c ON (c.id = s.cat_id) WHERE s.cat_id != 0 ORDER BY s.id ASC') as $row) {
		$xe = $twig->loadTemplate($tpath['list_cat_entries'] . 'list_cat_entries.tpl');
		$tVars = array(
			'title'        => $row['stitle'],
			'id'           => $row['sid'],
			'cat_name'     => '<a href="?mod=extra-config&plugin=simple_title_pro&action=cat_edit&id=' . $row['sid'] . '"  />' . $row['cname'] . '</a>',
			'cat_name_del' => '<a href="?mod=extra-config&plugin=simple_title_pro&action=cat_name_del&id=' . $row['id'] . '"  />Удалить</a>',
		);
		$entries .= $xe->render($tVars);
	}
	$xt = $twig->loadTemplate($tpath['list_cat'] . 'list_cat.tpl');
	$tVars = array(
		'entries' => $entries
	);
	$xg = $twig->loadTemplate($tpath['main'] . 'main.tpl');
	$tVars = array(
		'global'  => 'Список',
		'entries' => $xt->render($tVars)
	);
	print $xg->render($tVars);
}

function main()
{

	global $twig;
	$tpath = locatePluginTemplates(array('main', 'general.from'), 'simple_title_pro', 1);

	// Отображение уведомлений из сессии
	if (isset($_SESSION['simple_title_pro']['notify'])) {
		msgSticker($_SESSION['simple_title_pro']['notify']);
		unset($_SESSION['simple_title_pro']['notify']);
	}
	if (isset($_SESSION['simple_title_pro']['notify_error'])) {
		msgSticker($_SESSION['simple_title_pro']['notify_error'], 'error');
		unset($_SESSION['simple_title_pro']['notify_error']);
	}

	if (isset($_SESSION['simple_title_pro']['info'])) {
		$info = $_SESSION['simple_title_pro']['info'];
		unset($_SESSION['simple_title_pro']['info']);
	}
	if (is_file(root . 'plugins/simple_title_pro/tpl/reklama')) {
		$reklama = file_get_contents(root . 'plugins/simple_title_pro/tpl/reklama');
		unlink(root . 'plugins/simple_title_pro/tpl/reklama');
	}
	if (isset($_REQUEST['submit'])) {
		pluginSetVariable('simple_title_pro', 'c_title', secure_html(trim($_REQUEST['c_title'])));
		pluginSetVariable('simple_title_pro', 'n_title', secure_html(trim($_REQUEST['n_title'])));
		pluginSetVariable('simple_title_pro', 'm_title', secure_html(trim($_REQUEST['m_title'])));
		pluginSetVariable('simple_title_pro', 'static_title', secure_html(trim($_REQUEST['static_title'])));
		pluginSetVariable('simple_title_pro', 'num_title', secure_html(trim($_REQUEST['num_title'])));
		pluginSetVariable('simple_title_pro', 'num_cat', secure_html(trim($_REQUEST['num_cat'])));
		pluginSetVariable('simple_title_pro', 'num_news', secure_html(trim($_REQUEST['num_news'])));
		pluginSetVariable('simple_title_pro', 'num_static', secure_html(trim($_REQUEST['num_static'])));
		pluginSetVariable('simple_title_pro', 'o_title', secure_html(trim($_REQUEST['o_title'])));
		pluginSetVariable('simple_title_pro', 'e_title', secure_html(trim($_REQUEST['e_title'])));
		pluginSetVariable('simple_title_pro', 'html_secure', secure_html(trim($_REQUEST['html_secure'])));
		pluginSetVariable('simple_title_pro', 'p_title', secure_html(trim($_REQUEST['p_title'])));
		pluginSetVariable('simple_title_pro', 'cache', secure_html(trim($_REQUEST['cache'])));
		pluginsSaveConfig();
		redirect_simple_title_pro('?mod=extra-config&plugin=simple_title_pro');
	}
	$c_title = pluginGetVariable('simple_title_pro', 'c_title');
	$n_title = pluginGetVariable('simple_title_pro', 'n_title');
	$m_title = pluginGetVariable('simple_title_pro', 'm_title');
	$static_title = pluginGetVariable('simple_title_pro', 'static_title');
	$o_title = pluginGetVariable('simple_title_pro', 'o_title');
	$e_title = pluginGetVariable('simple_title_pro', 'e_title');
	$p_title = pluginGetVariable('simple_title_pro', 'p_title');
	$num_title = pluginGetVariable('simple_title_pro', 'num_title');
	$num_cat = pluginGetVariable('simple_title_pro', 'num_cat');
	$num_news = pluginGetVariable('simple_title_pro', 'num_news');
	$num_static = pluginGetVariable('simple_title_pro', 'num_static');
	$cache = pluginGetVariable('simple_title_pro', 'cache');
	$html_secure = pluginGetVariable('simple_title_pro', 'html_secure');
	$xt = $twig->loadTemplate($tpath['general.from'] . 'general.from.tpl');
	$xg = $twig->loadTemplate($tpath['main'] . 'main.tpl');
	$tVars = array(
		'c_title'      => array(
			'print' => $c_title,
			'error' => empty($c_title) ? '<img src="' . skins_url . '/images/error.gif" hspace="5" alt="" />Поле не заполнено!<br /><b>Ремомендованно:</b> %home%  / %cat% %num%' : ''
		),
		'n_title'      => array(
			'print' => $n_title,
			'error' => empty($n_title) ? '<img src="' . skins_url . '/images/error.gif" hspace="5" alt="" />Поле не заполнено!<br /><b>Ремомендованно:</b> %home%  / %cat% / %title%  %num%' : ''
		),
		'm_title'      => array(
			'print' => $m_title,
			'error' => empty($m_title) ? '<img src="' . skins_url . '/images/error.gif" hspace="5" alt="" />Поле не заполнено!<br /><b>Ремомендованно:</b> %home% %num%' : ''
		),
		'static_title' => array(
			'print' => $static_title,
			'error' => empty($static_title) ? '<img src="' . skins_url . '/images/error.gif" hspace="5" alt="" />Поле не заполнено!<br /><b>Ремомендованно:</b> %home% / %static%' : ''
		),
		'o_title'      => array(
			'print' => $o_title,
			'error' => empty($o_title) ? '<img src="' . skins_url . '/images/error.gif" hspace="5" alt="" />Поле не заполнено!<br /><b>Ремомендованно:</b> %home% / %other% %html% %num%' : ''
		),
		'e_title'      => array(
			'print' => $e_title,
			'error' => empty($e_title) ? '<img src="' . skins_url . '/images/error.gif" hspace="5" alt="" />Поле не заполнено!<br /><b>Ремомендованно:</b> %home% / %other%' : ''
		),
		'num_title'    => array(
			'print' => $num_title,
			'error' => empty($num_title) ? '<img src="' . skins_url . '/images/error.gif" hspace="5" alt="" />Поле не заполнено!<br /><b>Ремомендованно:</b> / Страница %count%' : ''
		),
		'num_cat'      => array(
			'print' => $num_cat,
			'error' => empty($num_cat) ? '<img src="' . skins_url . '/images/error.gif" hspace="5" alt="" />Поле не заполнено!<br /><b>Ремомендованно:</b> 20' : ''
		),
		'num_news'     => array(
			'print' => $num_news,
			'error' => empty($num_news) ? '<img src="' . skins_url . '/images/error.gif" hspace="5" alt="" />Поле не заполнено!<br /><b>Ремомендованно:</b> 20' : ''
		),
		'num_static'   => array(
			'print' => $num_static,
			'error' => empty($num_static) ? '<img src="' . skins_url . '/images/error.gif" hspace="5" alt="" />Поле не заполнено!<br /><b>Ремомендованно:</b> 20' : ''
		),
		'html_secure'  => array(
			'print' => $html_secure,
			'error' => empty($html_secure) ? '<img src="' . skins_url . '/images/error.gif" hspace="5" alt="" />Поле не заполнено!<br /><b>Ремомендованно:</b> / %html%' : ''
		),
		'p_title'      => array(
			'print' => $p_title,
			'error' => ''
		),
		'cache'        => array(
			'print' => $cache,
			'error' => empty($cache) ? '<img src="' . skins_url . '/images/error.gif" hspace="5" alt="" />Поле не заполнено!<br /><b>Ремомендованно:</b> 1' : ''
		),
	);
	foreach ($tVars as $row) {
		//print "<pre>".var_export($row['error'], true)."</pre>";
		if (!empty($row['error'])) {
			$info .= '<div class="alert alert-warning">У вас ошибка! Если не получается её исправить, обратитесь на форум <a href="http://ngcms.org/forum/viewtopic.php?id=2055" target="_blank"><b>simple_title_pro</b></a> или мне на ICQ: 209388634 или jabber: rozard@ya.ru</div>';
			break;
		}
	}
	$tVars = array(
		'info'    => array(
			'true'  => !empty($info) ? 1 : 0,
			'print' => $info
		),
		'reklama' => array(
			'true'  => !empty($reklama) ? 1 : 0,
			'print' => nl2br($reklama)
		),
		'global'  => 'Общие',
		'entries' => $xt->render($tVars)
	);
	print $xg->render($tVars);
}

function del_cat()
{

	global $mysql;
	$id = intval($_REQUEST['id']);
	if (empty($id)) {
		$_SESSION['simple_title_pro']['notify_error'] = 'Ошибка, вы не выбрали что хотите удалить';
		return;
	}
	$mysql->query("delete from " . prefix . "_simple_title_pro where id = {$id} LIMIT 1");
	$_SESSION['simple_title_pro']['notify'] = 'Запись удалена';
}

function del_news()
{

	global $mysql;
	$id = intval($_REQUEST['id']);
	if (empty($id)) {
		$_SESSION['simple_title_pro']['notify_error'] = 'Ошибка, вы не выбрали что хотите удалить';
		return;
	}
	$mysql->query("delete from " . prefix . "_simple_title_pro where id = {$id} LIMIT 1");
	$_SESSION['simple_title_pro']['notify'] = 'Запись удалена';
}

function del_static()
{

	global $mysql;
	$id = intval($_REQUEST['id']);
	if (empty($id)) {
		$_SESSION['simple_title_pro']['notify_error'] = 'Ошибка, вы не выбрали что хотите удалить';
		return;
	}
	$mysql->query("delete from " . prefix . "_simple_title_pro where id = {$id} LIMIT 1");
	$_SESSION['simple_title_pro']['notify'] = 'Запись удалена';
}

function redirect_simple_title_pro($url)
{

	if (headers_sent()) {
		echo "<script>document.location.href='{$url}';</script>\n";
		exit;
	} else {
		header('HTTP/1.1 302 Moved Permanently');
		header("Location: {$url}");
		exit;
	}
}
