<?php
//获取根目
function get_arearoot($area_id) {
  if (!$arearoot = strstr($area_id, '_', true)) {
    if (!$arearoot = substr($area_id, 0, strpos($area_id, '_'))) {
      $arearoot = $area_id;
    }
  }
  return $arearoot;
}
@ require ('inc/set/set.php');
@ require ('inc/set/set_sql.php');

if (isset($_GET['mode']) && !empty($_GET['mode']) && file_exists('inc/css/'.$_GET['mode'].'/index.php') && file_exists('inc/css/'.$_GET['mode'].'/list.php') && file_exists('inc/css/'.$_GET['mode'].'/article.php')) {
  $GLOBALS['this_mode'] = $_GET['mode'];
} else {
  $GLOBALS['this_mode'] = $web['sitemode'];
}

//处理伪静态
if ($web['url_html_list'] == 1) {
  $list_ = 'list.'.$GLOBALS['this_mode'].'...1.html';
  @ $list_url = 'list.'.$GLOBALS['this_mode'].'.'.$_GET['area_id'].'..1.html';
  $list_url_area = '"list.'.$GLOBALS['this_mode'].'.".$row["area_id"]."..1.html"';
} else {
  $list_ = 'list.php?mode='.$GLOBALS['this_mode'].'';
  @ $list_url = 'list.php?mode='.$GLOBALS['this_mode'].'&area_id='.$_GET['area_id'].'';
  $list_url_area = '"list.php?mode='.$GLOBALS['this_mode'].'&area_id=".$row["area_id"].""';
}
if ($web['url_html_article'] == 1) {
  $article_url = '"article.'.$GLOBALS['this_mode'].'.".$row["id"].".1.html"';
} else {
  $article_url = '"article.php?mode='.$GLOBALS['this_mode'].'&id=".$row["id"].""';
}

$n = $mess = 0;
$text = $AREA_EXPIRES_data_type = $area_name_ = $arspit = '';

$arr_area_title = $arr_area_child = array();
if (!isset($sql['db_err'])) {
  db_conn();
}
if ($sql['db_err'] != '') {
  die($sql['db_err']);
}

if (preg_match('/^(\d+\-\d+)(\_\d+)*$/', @ $_GET['area_id'], $area_m)) {
  $area_id_root = $area_m[1];
  $mark_child = '该标签仍可细分：';
  $base_title = $_GET['area_id'];
  $eval_title = 'area_id="'.$_GET['area_id'].'"';
  $area_title = 'strip_tags($arr_area_title[\''.$_GET['area_id'].'\'])';
  while (strpos($base_title, '_') !== false) {
    $base_title = preg_replace('/\_\d+$/', '', $base_title);
    $eval_title .= ' OR area_id="'.$base_title.'"';
    $area_title = '$arr_area_title[\''.$base_title.'\'].\' &gt; \'.'.$area_title.'';}
  $result = @mysql_query('SELECT * FROM '.$sql['pref'].''.$sql['data']['承载版区分类的表名'].' WHERE '.$eval_title.' OR area_id REGEXP "^'.$_GET['area_id'].'_[0-9]+$"', $db);
  if (@mysql_num_rows($result) > 0) {
    $q_l = strlen($_GET['area_id']);
    while ($row = @mysql_fetch_assoc($result)) {
      if ($row['area_id'] == $area_id_root) {
        $AREA_MARK = $row['area_mark'];
        $web['listmode'] = $row['area_mode'];
        if ($row['area_expires'] > 0) {
          $AREA_EXPIRES_data_type .= ' AND NOT (topdate<"'.(gmdate('Y-m-d H:i:s', time() + floatval($web['time_pos']) * 3600 - $row['area_expires'] * 24 * 60 * 60)).'") ';
        }
        /*
        if ($row['area_check'] == '1') {
          $AREA_CHECK_data_type .= ' AND area_check="0"';
        }
        */
      }


     if ($q_l >= strlen($row['area_id'])) {
        $arr_area_title[$row['area_id']] = '<a href="'.eval('return '.$list_url_area.';').'"'.$row['area_style'].'>'.$row['area_name'].'</a> ';
      } else/*if (strrpos($row['area_id'], '_') == $q_l)*/ {
        $arr_area_child[$row['area_id']] = $row['area_name'];
      }
      unset($row);
    }
    @uksort($arr_area_child, 'strnatcmp');
  }
  @mysql_free_result($result);
  eval('$title = strip_tags('.$area_title.'); $title1 = \'<a href="./'.$list_.'">全部根目</a> &gt; \'.'.$area_title.';');

  $data_type = ' WHERE '.(!empty($arr_area_child) ? 'area_id LIKE "'.$_GET['area_id'].'\_%"' : 'area_id="'.$_GET['area_id'].'"').'';
  if (!empty($AREA_EXPIRES_data_type)) {
    $data_type .= $AREA_EXPIRES_data_type;
  }
  //$data_type .= $AREA_CHECK_data_type;
  $data_type .= ' AND area_check="0"';
  $arspit='if($row["area_id"] != $_GET["area_id"]){
               $a_id=$_GET["area_id"]."_".abs(substr($row["area_id"], strlen($_GET["area_id"]."_")));
               @ $row[\'area_name_\']=$arr_area_child[$a_id]."".($a_id == $row["area_id"] ? "" : "/…")."";
           }';
  $logo = get_area_logo(get_arearoot($_GET['area_id']));
  $path = $list_url;
  $name = $title.' - '.$web['sitename2'];

} else {
  $_GET['area_id'] = '';
  $title = $title1 = '全部根目';

  $mark_child = '全部根类目：'; //
  $result = @mysql_query('SELECT * FROM '.$sql['pref'].''.$sql['data']['承载版区分类的表名'].' WHERE area_id REGEXP "^[0-9]+\-[0-9]+$" AND area_index LIKE "%'.$GLOBALS['this_mode'].'%"', $db);
  if (@mysql_num_rows($result) > 0) {
    while ($row = @mysql_fetch_assoc($result)) {
      //$area_id_root = get_arearoot($row['area_id']);
      $area_id_root = $row['area_id'];
      $area_sql_forlistdata[abs($area_id_root)] = 1;
      if ($row['area_expires'] > 0) {
        $AREA_EXPIRES_data_type .= ' AND NOT (area_id LIKE "'.$area_id_root.'\_%" AND topdate<"'.(gmdate('Y-m-d H:i:s', time() + floatval($web['time_pos']) * 3600 - $row['area_expires'] * 24 * 60 * 60)).'") ';
      }
     /*
      if ($row['area_check'] == '1') {
        //$AREA_CHECK_data_type .= ' AND (area_id LIKE "'.$area_id_root.'\_%" AND area_check="0")'; //严格来讲用这个，不过检索条件太烦琐了
        $AREA_CHECK_data_type .= ' AND area_check="0"';
      }
      */
      $arr_area_child[$row['area_id']] = $row['area_name'];
      unset($row, $area_id_root);
    }
    @uksort($arr_area_child, 'strnatcmp');
  }
  @mysql_free_result($result);

  $data_type = ' WHERE ((area_id LIKE "'.@implode('-%" OR area_id LIKE "', array_keys($area_sql_forlistdata)).'-%") OR area_id IN ('.@implode(',', array_keys($area_sql_forlistdata)).')) ';
  if (!empty($AREA_EXPIRES_data_type)) {
    $data_type .= $AREA_EXPIRES_data_type;
  }
  //$data_type .= $AREA_CHECK_data_type;
  $data_type .= ' AND area_check="0"';
  $arspit = '@ $row[\'area_name_\']=$arr_area_child[$area_id_root]."/…";';
	  
  $logo = 'inc/images/logo.gif';
  $path = $web['path'];
  $name = $web['sitename'];
}

$result = @mysql_query('SELECT * FROM `'.$sql['pref'].''.$sql['data']['承载主题文章的表名'].'`'.$data_type.' ORDER BY topdate DESC LIMIT '.$web['pagesize'].'', $db);
if (@mysql_num_rows($result) > 0) {
  while ($row = @mysql_fetch_assoc($result)) {
    $area_id_root = get_arearoot($row['area_id']);
    eval($arspit);
    $text .= '
<item>
  <title><![CDATA['.$row['title'].']]></title>
  <description><![CDATA['.$row['text'].']]></description>
  <category><![CDATA['.$row['area_name_'].']]></category>
  <link><![CDATA['.eval('return '.$article_url.';').']]></link>
  <author><![CDATA['.$row['author_ip'].']]></author>
  <pubDate><![CDATA['.get_GMT_date($row['topdate']).']]></pubDate>
</item>';
    unset($row, $area_id_root);
  }
}
@mysql_free_result($result);

@mysql_close();

header("Content-type:text/xml");
echo '<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title><![CDATA['.$name.']]></title>
<link><![CDATA['.$path.']]></link>
<description><![CDATA['.$mark_child.''.@implode(' ', $arr_area_child).']]></description>
<copyright>(c) 2012, 162100.com. All rights reserved.</copyright>
<generator>162100.com</generator>
<image>
  <url><![CDATA['.$logo.']]></url>
  <title><![CDATA['.$name.']]></title>
  <link><![CDATA['.$path.']]></link>
</image>'.$text.'
</channel>
</rss>';






function get_area_logo($area_id) {
  if (file_exists('inc/css/'.$GLOBALS['this_mode'].'/area_logo_'.$area_id.'.gif')) {
    return 'inc/css/'.$GLOBALS['this_mode'].'/area_logo_'.$area_id.'.gif';
  } else {
    return 'inc/images/logo.gif';
  }
}



function get_GMT_date($date) {
  if (!empty($date)) {
    @list($Year, $Month, $Day, $Hour, $Minute, $Second) = @preg_split('/[\s-:]+/', $date);
    return @date('r', @mktime($Hour, $Minute, $Second, $Month, $Day, $Year));
  }
}
?>