<?php
define('IN_OUN', true);
include_once( "includes/command.php");
//include_once( ROOT_PATH.'includes/fck/fckeditor.php'); // 包含 html editor 类文件
if($Aconf['priveMessage'] != '')
{
   echo showMessage($Aconf['priveMessage']);
   exit;
}

/* 检查用户模板目录是否存在 */
$db_table = $pre."sysconfig";
if($_POST[action] == 'save')
{
    clear_all_files();
    $template = stripslashes($_POST['templates_name']);

    $sql = "UPDATE " . $db_table . " SET 
                template='$template',user_template=0   
		        WHERE  scid=".$_SESSION[$un_domain_id];
    $oPub->query($sql);

     $strMessage =  "模板选择成功！";

}
/* 得到当前模版 */

$sql = "SELECT template,user_template FROM ".$db_table." WHERE scid = ".$_SESSION[$un_domain_id]." ORDER BY scid ASC LIMIT 1";
$Anorm = $oPub->selectrow($sql);
//模板列表

if($Anorm[user_template])
{
	$user_template = '<span style="color:#CC0000;font-size: 14px">当前使用自定义模板。重新选择,<A HREF="template_edit.php">自定义模板</A>将会被覆盖!</span><br>';
	$template = '';
}
else
{
	$template = $Anorm[template]; //当前模版名
}



/* 获得所有模版信息 */
$available_templates = array();
$template_dir        = @opendir(ROOT_PATH . 'themes/');
while ($file = readdir($template_dir))
{
    if ($file != '.' && $file != '..' && is_dir(ROOT_PATH. 'themes/' . $file) && $file != '.svn' && $file != 'index.htm')
    {
        $available_templates[] = get_template_info($file);
    }
}

@closedir($template_dir);



/* 模板列表 */
$strtemplates = '';
while( @list( $k, $v ) = @each($available_templates ) ) {
	$strtemplates .= '<div style="float:left; text-align:center; border: 1px solid #DADADA; margin: 4px; padding:2px;">';
	$strtemplates .= '<IMG SRC="'.$v["screenshot"].'" WIDTH="200" HEIGHT="150" BORDER="0" TITLE="'. $v["desc"].'">';
	$checked = ($v["code"] == $template )?'checked':'';
    $strtemplates .= '<br/><INPUT TYPE="radio" NAME="templates_name" value="'.$v["code"].'" '.$checked.' />';
	$strtemplates .= $v['name'].' '.$v['version']. '<br/><A HREF="'.$v['author_uri'].'" target="_blank">'.$v['author'] .'</A>';
    $strtemplates .= '</div>';
}
?>
<?php
include_once( "header.php");
if ($strMessage != ''){
	echo  '<SCRIPT language="javascript"> alert( "'.$strMessage.'");</script>';
}
?>
<DIV class=content>
<TABLE width="100%" border=0>
  <TR  >
    <FORM METHOD=POST NAME="TEMPLATE_EDIT" ACTION="">      
      <TD width="13%" align="left" >
	    
           <?php  echo $strtemplates; ?>
		    <div style="clear:left"></div>
			<?php echo $user_template;?>
		    <input type="submit" name="Submit" value="确定选择" style="background-color: #FFCC66;font-size: 14px"/>
			<INPUT TYPE="hidden" name="action" value="save">			
      </TD>
	</FORM>
   </TR>	
</TABLE> 
</DIV>
<?php
/**
 * 获得模版的信息
 *
 * @access  private
 * @param   string      $template_name      模版名
 * @return  array
 */
function get_template_info($template_name)
{
    $info = array();
    $ext  = array('png', 'gif', 'jpg', 'jpeg');

    $info['code']       = $template_name;
    $info['screenshot'] = '';

    foreach ($ext AS $val)
    {
        if (file_exists('../themes/' . $template_name . "/screenshot.$val"))
        {
            $info['screenshot'] = '../themes/' . $template_name . "/screenshot.$val";

            break;
        }
    }

    if (file_exists('../themes/' . $template_name . '/style.css') && !empty($template_name))
    {
        $arr = array_slice(file('../themes/'. $template_name. '/style.css'), 0, 9);

        $template_name      = explode(': ', $arr[1]);
        $template_uri       = explode(': ', $arr[2]);
        $template_desc      = explode(': ', $arr[3]);
        $template_version   = explode(': ', $arr[4]);
        $template_author    = explode(': ', $arr[5]);
        $author_uri         = explode(': ', $arr[6]);
        $logo_filename      = explode(': ', $arr[7]);

        $info['name']       = isset($template_name[1]) ? trim($template_name[1]) : '';
        $info['uri']        = isset($template_uri[1]) ? trim($template_uri[1]) : '';
        $info['desc']       = isset($template_desc[1]) ? trim($template_desc[1]) : '';
        $info['version']    = isset($template_version[1]) ? trim($template_version[1]) : '';
        $info['author']     = isset($template_author[1]) ? trim($template_author[1]) : '';
        $info['author_uri'] = isset($author_uri[1]) ? trim($author_uri[1]) : '';
        $info['logo']       = isset($logo_filename[1]) ? trim($logo_filename[1]) : '';
    }
    else
    {
        $info['name']       = '';
        $info['uri']        = '';
        $info['desc']       = '';
        $info['version']    = '';
        $info['author']     = '';
        $info['author_uri'] = '';
        $info['logo']       = '';
    }

    return $info;
}

?>
<?php
include_once( "footer.php");
?>
