<?php
/**
 * Copyright (C) 2009  Honghao Geng (genghonghao@gmail.com)
 * $Id: other.php 550 2010-01-24 04:52:21Z genghonghao $
 */

define('IN_ADMIN', TRUE);

require_once dirname(__FILE__) . '/includes/init.php';
require_once GEN_ROOT . '/languages/' . $_CONF['lang'] . '/admin/other.lang.php';

// 验证用户是否登录
if (false === verify_user()) {;
    header("Location: ../login.php");
}

$tpl->assign('lang', $lang);

$action = isset($_REQUEST['act']) ? $_REQUEST['act'] : 'link';

// 获得当前页面的完整URL
$current_page_url = $base_url . '/' . $current_file . '?' . get_uri_param_string();
$tpl->assign('current_page_url', $current_page_url);
$tpl->assign('base_root_url', $base_root_url);

// 友情链接
if ($action == 'link') {

// 添加友情链接
    if (isset($_REQUEST['sub_link'])) {
        $text_link_name = isset($_REQUEST['text_link_name']) ? (trim($_REQUEST['text_link_name']) == NULL ? NULL : trim($_REQUEST['text_link_name'])) : NULL;
        $text_link_text = isset($_REQUEST['text_link_text']) ? (trim($_REQUEST['text_link_text']) == NULL ? NULL : trim($_REQUEST['text_link_text'])) : NULL;
        $text_link_address = isset($_REQUEST['text_link_address']) ? (trim($_REQUEST['text_link_address']) == NULL ? NULL : trim($_REQUEST['text_link_address'])) : NULL;
        $text_link_order = isset($_REQUEST['text_link_order']) ? (trim($_REQUEST['text_link_order']) == NULL ? NULL : trim($_REQUEST['text_link_order'])) : NULL;
        $text_link_type = isset($_REQUEST['text_link_type']) ? (trim($_REQUEST['text_link_type']) == NULL ? NULL : trim($_REQUEST['text_link_type'])) : NULL;
        $text_display_type = isset($_REQUEST['text_display_type']) ? (trim($_REQUEST['text_display_type']) == NULL ? NULL : trim($_REQUEST['text_display_type'])) : NULL;

        $insert_sql = "INSERT INTO " . $db->get_table(TB_LINKS) . " VALUES(NULL, '$text_link_name', '$text_link_text', '$text_link_address', '$text_link_order', '$text_link_type', '$text_display_type')";
        if (false === $db->query($insert_sql, '', 1)) {
            sys_msg($lang['notice_link_add_failure'], 1, array(), false);
        } else {
            redirect('Success', $lang['notice_link_add_success'], $current_page_url);
        }
    }

    // 删除友情链接
    if (isset($_REQUEST['delid'])) {
        $delid = intval($_REQUEST['delid']);
        $del_sql = "DELETE FROM " . $db->get_table(TB_LINKS) . " WHERE link_id='$delid'";
        if (false === $db->query($del_sql, '', 1)) {
            redirect('Failure', $lang['notice_link_del_failure'], 'other.php?act=link');
        } else {
            redirect('Success', $lang['notice_link_del_success'], 'other.php?act=link');
        }
    }

    // 取出链接数据
   $links =  $db->result("SELECT * FROM " . $db->get_table(TB_LINKS) . " ORDER BY `link_order`");
   $tpl->assign('links', $links);

    // 所在位置
    $location_link = array("<a href='other.php'>" . $lang['8_other_manage']['name'] . "</a>",
        "<a href='other.php?act=$action'>" . $lang['8_other_manage']['other_link'] . "</a>"
    );
    // 相关操作
    $location_other = array("<a class='realbutton' href='other.php?act=about'>" . $lang['8_other_manage']['other_about'] . "</a>"  );

    $tpl->assign('location_link', $location_link);
    $tpl->assign('location_other', $location_other);
    $tpl->display('link.html');
}
// 首页广告管理
if ($action == 'ad_manage') {
    $ad_file = GEN_ROOT . '/data/ad_conf.php';
    // 添加首页广告
    if (isset($_REQUEST['text_submit_ad'])) {
        $text_ad_name = isset($_REQUEST['text_ad_name']) ? (trim($_REQUEST['text_ad_name']) == NULL ? NULL : trim($_REQUEST['text_ad_name'])) : NULL;
        $text_ad_desc = isset($_REQUEST['text_ad_desc']) ? (trim($_REQUEST['text_ad_desc']) == NULL ? NULL : trim($_REQUEST['text_ad_desc'])) : NULL;
        $text_ad_url = isset($_REQUEST['text_ad_url']) ? (trim($_REQUEST['text_ad_url']) == NULL ? NULL : trim($_REQUEST['text_ad_url'])) : NULL;
        $text_ad_img = isset($_REQUEST['text_ad_img']) ? (trim($_REQUEST['text_ad_img']) == NULL ? NULL : trim($_REQUEST['text_ad_img'])) : NULL;

        $content = '<?php' . "\n";
        $content .= '$ad_array = array();' . "\n";
        $content .= '$ad_array[\'name\'] = "' . $text_ad_name . '";' . "\n";
        $content .= '$ad_array[\'desc\'] = "' . $text_ad_desc . '";' . "\n";
        $content .= '$ad_array[\'url\'] = "' . $text_ad_url . '";' . "\n";
        $content .= '$ad_array[\'img\'] = "' . $text_ad_img . '";' . "\n";
        $content .= '?>' . "\n";
        $fp = @fopen($ad_file, 'wb');
        if (@fwrite($fp, $content)) {
            redirect('Success', $lang['notice_ad_add_success'], 'other.php?act=ad_manage');
        } else {
            sys_msg($lang['notice_ad_add_failure'], 1, array(), false);
        }
    }
    // 取出广告数据
    @include $ad_file;
    if (isset($ad_array)) {
        $tpl->assign('ad', $ad_array);
    }
    // 所在位置
    $location_link = array("<a href='other.php'>" . $lang['8_other_manage']['name'] . "</a>",
        "<a href='other.php?act=$action'>" . $lang['8_other_manage']['other_link'] . "</a>"
    );
    // 相关操作
    $location_other = array("<a class='realbutton' href='other.php?act=about'>" . $lang['8_other_manage']['other_about'] . "</a>"  );

    $tpl->assign('location_link', $location_link);
    $tpl->assign('location_other', $location_other);
    $tpl->display('ad_manage.html');
}
// 关于本程序
elseif ($action == 'about') {
    // 上传许可文件大小
     $result = $db->result("SELECT conf_value FROM " . $db->get_table(TB_CONF) . " WHERE conf_name='update_file_max'");
     $tpl->assign('update_file_max', format_size($result[0]['conf_value'], 'K'));
    $tpl->assign('program_version', get_smartgoon_version());
    $tpl->assign('os_and_php', get_os_and_php_version());
    $tpl->assign('http_server', get_http_server_version());
    $tpl->assign('mysql_version', get_db_server_version());
    $tpl->assign('db_size', get_db_size());

    $tpl->assign('copyright', $lang['copyright']);
    $tpl->assign('manage', $lang['manage']);
    $tpl->assign('dev_project', $lang['dev_project']);
    $tpl->assign('style_project', $lang['style_project']);
    $tpl->assign('contribution', $lang['contribution']);
    $tpl->assign('interrelated', $lang['interrelated']);

     // 所在位置
    $location_link = array("<a href='other.php'>" . $lang['8_other_manage']['name'] . "</a>",
        "<a href='other.php?act=$action'>" . $lang['8_other_manage']['other_about'] . "</a>"
    );
    $tpl->assign('location_link', $location_link);
    
    $tpl->display('about.html');
}
?>
