function AdminArticlesMain()
{
    global $editarticles;
    // Фильтр, дает возможность показывать статьи определенной категории.
    if (isset($_GET['cat']) && $_GET['cat'] > -1) {
        $cat = SafeEnv($_GET['cat'], 11, int);
        $where = "`cat_id`='{$cat}'";
    } else {
        $cat = -1;
        $where = "";
    }
    $data = array();
    $data = AdminArticlesGetTree()->GetCatsData($cat, true);
    System::site()->DataAdd($data, -1, 'Все статьи', $cat == -1);
    // Получаем номер страницы
    if (isset($_GET['page'])) {
        $page = SafeEnv($_GET['page'], 10, int);
    } else {
        $page = 1;
    }
    AddCenterBox('Статьи');
    // Форма фильтра по категориям
    System::admin()->AddJS('
	ArticlesSelectCat = function(){
		Admin.LoadPage("' . ADMIN_FILE . '?exe=articles&cat="+$("#article-cat").val());
	}
	');
    $text = '<div style="text-align: center; margin-bottom: 10px;">Категория: ' . System::site()->Select('cat', $data, false, 'id="article-cat" onchange="ArticlesSelectCat();"') . '</div>';
    AddText($text);
    // Берем статьи из БД и включаем постраничную навигацию если нужно.
    $r = System::database()->Select('articles', $where);
    SortArray($r, 'public', true);
    // Сортируем по дате добавления
    if (count($r) > System::config('articles/articles_on_page')) {
        $navigator = new Navigation($page);
        $navigator->GenNavigationMenu($r, System::config('articles/articles_on_page'), ADMIN_FILE . '?exe=articles' . ($cat > 0 ? '&cat=' . $cat : ''));
        $nav = true;
    } else {
        $nav = false;
    }
    $text = '<table cellspacing="0" cellpadding="0" class="cfgtable">';
    $text .= '<tr><th>Название</th><th>Прочитано</th><th>Оценки</th><th>Видят</th><th>Статус</th><th>Функции</th></tr>';
    $back = SaveRefererUrl();
    foreach ($r as $art) {
        $id = SafeDB($art['id'], 11, int);
        $cat_id = SafeDB($art['cat_id'], 11, int);
        $title = SafeDB($art['title'], 255, str);
        $hits = SafeDB($art['hits'], 11, int);
        $rating = '<img src="' . GetRatingImage(SafeDB($art['num_votes'], 11, int), SafeDB($art['all_votes'], 11, int)) . '" border="0" />';
        $st = $art['active'] == '1' ? 'Вкл.' : 'Выкл.';
        $link_go = Ufu("index.php?name=articles&op=read&art={$id}&cat={$cat_id}", 'articles/{cat}/{art}/');
        $func = '';
        $func .= System::admin()->SpeedButton('Перейти к странице на сайте', $link_go, 'images/link_go.png', false, false, 'target="_blank"');
        if ($editarticles) {
            $title = '<b>' . System::admin()->Link($title, ADMIN_FILE . '?exe=articles&a=editor&id=' . $id . '&back=' . $back) . '</b>';
            $hits .= '&nbsp;' . System::admin()->SpeedConfirm('Обнулить счётчик просмотров', ADMIN_FILE . '?exe=articles&a=resethits&id=' . $id . '&back=' . $back, 'images/admin/arrow_in.png', 'Сбросить счётчик просмотров?');
            $rating .= '&nbsp;' . System::admin()->SpeedConfirm('Обнулить счётчик оценок (' . SafeDB($art['num_votes'], 11, int) . ' голосов)', ADMIN_FILE . '?exe=articles&a=resetrating&id=' . $id . '&back=' . $back, 'images/admin/arrow_in.png', 'Сбросить оценки?');
            $st = System::admin()->SpeedStatus('Включена', 'Отключена', ADMIN_FILE . '?exe=articles&a=changestatus&id=' . $id, $art['active'] == '1');
            $func .= System::admin()->SpeedButton('Редактировать', ADMIN_FILE . '?exe=articles&a=editor&id=' . $id . '&back=' . $back, 'images/admin/edit.png');
            $func .= System::admin()->SpeedConfirm('Удалить', ADMIN_FILE . '?exe=articles&a=delete&id=' . $id . '&ok=1&back=' . $back, 'images/admin/delete.png', 'Удалить статью?');
        }
        $text .= '<tr>
		<td>' . $title . '</td>
		<td>' . $hits . '</td>
		<td>' . ($art['allow_votes'] == '1' ? $rating : 'Запрещены') . '</td>
		<td>' . ViewLevelToStr(SafeDB($art['view'], 1, int)) . '</td>
		<td>' . $st . '</td>
		<td>' . $func . '</td>
		</tr>';
    }
    $text .= '</table>';
    AddText($text);
    if ($nav) {
        AddNavigation();
    }
}
function IndexGalleryFilterImageData($img, $index, $count)
{
    global $ThumbsDir, $GalleryDir;
    $id = SafeDB($img['id'], 11, int);
    $cat_id = SafeDB($img['cat_id'], 11, int);
    $filename = SafeDB($img['file'], 255, str);
    $vars = array();
    $vars['id'] = $id;
    $vars['cat_id'] = $cat_id;
    $vars['date'] = TimeRender($img['public'], false);
    $vars['date_time'] = TimeRender($img['public']);
    $vars['title'] = SafeDB($img['title'], 255, str);
    $vars['description_plain'] = SafeDB($img['description'], 0, str);
    $vars['description'] = SafeDB($img['description'], 0, str, false, false);
    $vars['thumb_src'] = $ThumbsDir . $filename;
    $vars['image_src'] = $GalleryDir . $filename;
    $vars['image_view_full'] = $vars['image_src'];
    $vars['image_view'] = Ufu('index.php?name=gallery&op=view&img=' . $id . '&cat=' . $cat_id, 'gallery/{cat}/{img}/');
    $vars['size'] = false;
    $vars['asize'] = '';
    if (is_file($GalleryDir . $filename)) {
        $vars['size'] = FormatFileSize(filesize($GalleryDir . $filename));
        $asize = getimagesize($GalleryDir . $filename);
        $asize = $asize[0] . 'x' . $asize[1];
        $vars['asize'] = $asize;
    }
    $vars['hits'] = SafeDB($img['hits'], 11, int);
    $vars['author'] = SafeDB($img['author'], 255, str);
    $vars['email'] = SafeDB($img['email'], 255, str);
    $vars['site'] = SafeDB($img['site'], 255, str);
    $vars['allow_comments'] = SafeDB($img['allow_comments'], 255, str);
    $vars['comments'] = SafeDB($img['com_counter'], 11, int);
    $vars['allow_votes'] = SafeDB($img['allow_votes'], 1, bool);
    $vars['num_votes'] = SafeDB($img['num_votes'], 11, int);
    $vars['rating'] = GetRatingImage(SafeDB($img['num_votes'], 11, int), SafeDB($img['sum_votes'], 11, int));
    $vars['lcomments'] = 'Комментариев';
    $vars['ldescription'] = 'Описание';
    $vars['index'] = $index;
    $vars['count'] = $count;
    $vars['addvote_url'] = "index.php?name=gallery&op=addvote&img={$id}";
    System::site()->DataAdd($vdata, '0', 'Ваша оценка');
    System::site()->DataAdd($vdata, '1', 'Очень плохо');
    System::site()->DataAdd($vdata, '2', 'Плохо');
    System::site()->DataAdd($vdata, '3', 'Средне');
    System::site()->DataAdd($vdata, '4', 'Хорошо');
    System::site()->DataAdd($vdata, '5', 'Отлично');
    $vars['votes'] = System::site()->Select('vote', $vdata);
    $vars['addvotesubm'] = System::site()->Submit('Оценить изображение');
    return $vars;
}
function AddDetailDownload(&$down)
{
    $id = SafeDB($down['id'], 11, int);
    $cat_id = SafeDB($down['category'], 11, int);
    $func = IndexDownloadsFunc($id);
    $vars = array();
    $vars['category_url'] = Ufu("index.php?name=downloads&cat={$cat_id}", 'downloads/{cat}/');
    $vars['category_title'] = IndexDownloadsGetTree()->IdCats[$down['category']]['title'];
    $vars['category'] = '<a href="' . $vars['category_url'] . '">' . $vars['category_title'] . '</a>';
    $vars['file_link'] = "index.php?name=downloads&op=download&file={$id}";
    // Если сделать ЧПУ ссылку, то появляется проблема с относительным адресом файла
    if (AccessIsResolved($down['view'])) {
        $vars['access'] = true;
        $url = '<a href="' . $vars['file_link'] . '" target="_blank">Скачать файл</a>';
    } else {
        $vars['access'] = false;
        $url = 'Файл только для зарегистрированных пользователей.';
    }
    $vars['not_access'] = !$vars['access'];
    $vars['url'] = $url;
    $vars['file_title'] = SafeDB($down['title'], 255, str) . (System::user()->isAdmin() ? $func : '');
    $vars['description'] = SafeDB($down['description'], 0, str, false, false);
    $vars['author'] = SafeDB($down['author'], 200, str);
    $vars['homepage'] = SafeDB($down['author_site'], 250, str);
    $vars['homepage_url'] = UrlRender(SafeDB($down['author_site'], 250, str));
    $vars['mail'] = SafeDB($down['author_email'], 50, str);
    $vars['date'] = TimeRender($down['public']);
    $vars['hits'] = SafeDB($down['hits'], 11, int);
    $vars['version'] = SafeDB($down['file_version'], 250, str);
    $vars['size'] = FormatFileSize(SafeDB($down['size'], 11, real), SafeDB($down['size_type'], 1, str));
    $vars['filetype'] = IndexDownloadsGetFileType(SafeDB($down['url'], 250, str));
    $vars['addvote_url'] = "index.php?name=downloads&op=addvote&file={$id}";
    System::site()->DataAdd($vdata, '0', 'Ваша оценка');
    System::site()->DataAdd($vdata, '1', 'Очень плохо');
    System::site()->DataAdd($vdata, '2', 'Плохо');
    System::site()->DataAdd($vdata, '3', 'Средне');
    System::site()->DataAdd($vdata, '4', 'Хорошо');
    System::site()->DataAdd($vdata, '5', 'Отлично');
    $vars['votes'] = System::site()->Select('vote', $vdata);
    $vars['addvotesubm'] = System::site()->Submit('Оценить файл');
    $vars['allow_votes'] = SafeDB($down['allow_votes'], 1, bool);
    if ($down['image'] != '') {
        $vars['image'] = RealPath2(System::config('downloads/images_dir') . SafeDB($down['image'], 255, str));
        $vars['thumb_image'] = RealPath2(System::config('downloads/images_dir') . 'thumbs/' . SafeDB($down['image'], 255, str));
    } else {
        $vars['image'] = false;
    }
    //Выводим rating
    $vars['rating_num_votes'] = SafeDB($down['votes_amount'], 11, int);
    $vars['rating_image'] = GetRatingImage(SafeDB($down['votes_amount'], 11, int), SafeDB($down['votes'], 11, int));
    $rating = $vars['rating_image'];
    if ($rating == '' && SafeDB($down['allow_votes'], 1, bool)) {
        // allow_rating
        $rating = 'Нет оценки';
    } elseif (SafeDB($down['allow_votes'], 1, bool)) {
        $rating = '<img src="' . $rating . '" /> (Оценок: ' . SafeDB($down['votes_amount'], 11, int) . ')';
    } else {
        $rating = ' - ';
    }
    $vars['rating'] = $rating;
    //
    if (!SafeDB($down['allow_comments'], 1, bool)) {
        // allow coments
        $vars['comments'] = ' - ';
    } else {
        $vars['comments'] = SafeDB($down['comments_counter'], 11, int);
    }
    System::site()->AddBlock('download', true, false, 'dl');
    System::site()->Blocks['download']['vars'] = $vars;
}
function RenderArticle(&$art)
{
    $vars = array();
    $art_id = SafeDB($art['id'], 11, int);
    $cat_id = SafeDB($art['cat_id'], 11, int);
    $func = IndexArticlesFunc($art_id);
    $vars['title'] = SafeDB($art['title'], 250, str) . (System::user()->isAdmin() ? $func : '');
    $vars['cat'] = IndexArticlesGetTree()->IdCats[$cat_id]['title'];
    $vars['catlink'] = Ufu("index.php?name=articles&cat={$cat_id}", 'articles/{cat}/');
    if ($art['image'] != '') {
        $vars['image'] = RealPath2(System::config('articles/images_dir') . SafeDB($art['image'], 255, str));
        $vars['thumb_image'] = RealPath2(System::config('articles/images_dir') . 'thumbs/' . SafeDB($art['image'], 255, str));
    } else {
        $vars['image'] = false;
    }
    if ($art['auto_br_desc'] == '1') {
        $vars['description'] = nl2br(SafeDB($art['description'], 0, str, false, false, false));
    } else {
        $vars['description'] = SafeDB($art['description'], 0, str, false, false, false);
    }
    $vars['lauthor'] = 'Автор';
    $vars['author'] = SafeDB($art['author'], 200, str);
    $vars['lemail'] = 'E-mail';
    $vars['email'] = SafeDB($art['email'], 50, str);
    $vars['lurl'] = 'Источник';
    $vars['site'] = SafeDB($art['www'], 250, str);
    $vars['site_url'] = UrlRender(SafeDB($art['www'], 250, str));
    $vars['lpublic'] = 'Опубликована';
    $vars['public'] = TimeRender(SafeDB($art['public'], 11, int), false);
    $vars['link2'] = Ufu("index.php?name=articles&op=read&art={$art_id}&cat={$cat_id}", 'articles/{cat}/{art}/');
    $vars['link'] = '<a href="' . $vars['link2'] . '">Читать...</a>';
    $vars['lhits'] = 'Просмотров';
    $vars['hits'] = SafeDB($art['hits'], 11, int);
    $vars['lcomments'] = 'Комментарий';
    $vars['comments'] = SafeDB($art['comments_counter'], 11, int);
    //Выводим rating
    $rating = GetRatingImage(SafeDB($art['num_votes'], 11, int), SafeDB($art['all_votes'], 11, int));
    $vars['rating_image'] = $rating;
    $vars['alloy_rating'] = SafeDB($art['allow_votes'], 1, bool);
    $vars['disable_rating'] = !$vars['alloy_rating'];
    $vars['lrating'] = 'Оценка';
    $vars['rating_num_votes'] = SafeDB($art['num_votes'], 11, int);
    //
    $vars['form'] = SafeDB($art['form'], 11, int);
    $vars['product_id'] = SafeDB($art['product_id'], 255, str);
    $vars['form_url'] = false;
    if ($vars['form'] != 0 && $vars['product_id'] != '') {
        $vars['form_url'] = 'index.php?name=forms&form=' . $vars['form'] . '&f_product_id=' . rawurlencode($vars['product_id']);
    }
    System::site()->AddSubBlock('articles', true, $vars);
}
function AdminDownloadsMain()
{
    $editfiles = System::user()->CheckAccess2('downloads', 'edit_files');
    $back = SaveRefererUrl();
    if (isset($_GET['cat']) && $_GET['cat'] > -1) {
        $cat = SafeEnv($_GET['cat'], 11, int);
        $where = "`category`='{$cat}'";
    } else {
        $cat = -1;
        $where = "";
    }
    $data = array();
    $data = AdminDownloadsGetTree()->GetCatsData($cat, true);
    System::site()->DataAdd($data, -1, 'Все файлы', $cat == -1);
    if (isset($_GET['page'])) {
        $page = SafeEnv($_GET['page'], 10, int);
    } else {
        $page = 1;
    }
    AddCenterBox('Файлы');
    System::admin()->AddJS(Indent('
		DownloadsSelectCat = function(){
			Admin.LoadPage("' . ADMIN_FILE . '?exe=downloads&cat="+$("#download-cat").val());
		}
	'));
    $text = '<div style="text-align: center; margin-bottom: 10px;">Категория: ' . System::site()->Select('cat', $data, false, 'id="download-cat" onchange="DownloadsSelectCat();"') . '</div>';
    AddText($text);
    System::database()->Select('downloads', $where);
    SortArray(System::database()->QueryResult, 'public', true);
    if (count(System::database()->QueryResult) > System::config('downloads/filesonpage')) {
        $navigator = new Navigation($page);
        $navigator->GenNavigationMenu(System::database()->QueryResult, System::config('downloads/filesonpage'), ADMIN_FILE . '?exe=downloads' . ($cat > 0 ? '&cat=' . $cat : ''));
        $nav = true;
    } else {
        $nav = false;
    }
    $text = '<table cellspacing="0" cellpadding="0" class="cfgtable">';
    $text .= '<tr><th>Заголовок</th><th>Скачиваний</th><th>Оценки</th><th>Видят</th><th>Статус</th><th>Функции</th></tr>';
    while ($row = System::database()->FetchRow()) {
        $id = SafeDB($row['id'], 11, int);
        $title = SafeDB($row['title'], 255, str);
        $hits = SafeDB($row['hits'], 11, int);
        $rating = '<img src="' . GetRatingImage(SafeDB($row['votes_amount'], 11, int), SafeDB($row['votes'], 11, int)) . '" border="0" />';
        $st = $row['active'] == '1' ? 'Вкл.' : 'Выкл.';
        $func = '-';
        if ($editfiles) {
            $title = '<b>' . System::admin()->Link($title, ADMIN_FILE . '?exe=downloads&a=editor&id=' . $id . '&back=' . $back) . '</b>';
            $hits .= '&nbsp;' . System::admin()->SpeedConfirm('Обнулить счётчик скачиваний', ADMIN_FILE . '?exe=downloads&a=resetcounter&id=' . $id . '&back=' . $back, 'images/admin/arrow_in.png', 'Сбросить счётчик скачиваний?');
            $rating .= '&nbsp;' . System::admin()->SpeedConfirm('Обнулить счётчик оценок (' . SafeDB($row['votes_amount'], 11, int) . ' голосов)', ADMIN_FILE . '?exe=downloads&a=resetrating&id=' . $id . '&back=' . $back, 'images/admin/arrow_in.png', 'Сбросить оценки?');
            $st = System::admin()->SpeedStatus('Вкл.', 'Выкл.', ADMIN_FILE . '?exe=downloads&a=changestatus&id=' . $id, $row['active'] == '1');
            $func = System::admin()->SpeedButton('Редактировать', ADMIN_FILE . '?exe=downloads&a=editor&id=' . $id . '&back=' . $back, 'images/admin/edit.png');
            $func .= System::admin()->SpeedConfirm('Удалить', ADMIN_FILE . '?exe=downloads&a=deletefile&id=' . $id . '&ok=0&back=' . $back, 'images/admin/delete.png', 'Удалить файл?');
        }
        $text .= '<tr><td>' . $title . '</td>
		<td>' . $hits . '</td>
		<td>' . ($row['allow_votes'] == '1' ? $rating : 'Запрещены') . '</td>
		<td>' . ViewLevelToStr(SafeDB($row['view'], 1, int)) . '</td>
		<td>' . $st . '</td>
		<td>' . $func . '</td>
		</tr>';
    }
    $text .= '</table>';
    AddText($text);
    if ($nav) {
        AddNavigation();
    }
}