<?php
// XOOPS2 - Auth version 1
// Presented by ADMIN @ ROUTE286, 2004.
require ( '../../mainfile.php' );
require ( 'config.php' );

//カテゴリー名をゲット
$catfolder=2;
$token1 = strtok($_SERVER['PATH_INFO'], '/');
$token2 = strtok('/');
$token3 = strtok('/');
$token4 = strtok('/');
$title_abc=${token.$catfolder};
$sql = "SELECT cid FROM ".$xoopsDB->prefix("myhtml_cat")." WHERE title_abc = '".$title_abc."'";
$result=$xoopsDB->query($sql);
$cid=mysql_result($result,0,"cid");
//カテゴリー名をゲット終わり

//カテゴリーの中のユーザIDをゲット
$unamefolder=3;
$token1 = strtok($_SERVER['PATH_INFO'], '/');
$unamefolder=${token.$unamefolder};
//カテゴリーの中のユーザIDをゲット終わり

//カテゴリーアクセス権限がないのに直リンクしているかチェック
$perm_name = 'myhtml_permit';
//10100000は公開性のアクセス権
$perm_itemid = 10000000+$cid;
	if ($xoopsUser) {
		$groups = $xoopsUser->getGroups();
	} else {
		$groups = XOOPS_GROUP_ANONYMOUS;
	}
	$module_id = $xoopsModule->getVar('mid');
	$gperm_handler =& xoops_gethandler('groupperm');
		//チェック
		if (!$gperm_handler->checkRight($perm_name, $perm_itemid, $groups, $module_id)) {
		//echo "カテゴリー";
		redirect_header (XOOPS_URL . "/modules/" . $xoopsModule->dirname() . "/index.php", 2 ,_MYHTML_ACCESS_DENIED_CAT);
		}
//カテゴリーアクセス権限がないのに直リンクしているかチェック終わり


//公開性の権限がないのに直リンクしているかチェック
$perm_name = 'myhtml_permit';
//10100000は公開性のアクセス権
$perm_itemid = 10100000+$cid;
	if ($xoopsUser) {
		$groups = $xoopsUser->getGroups();
	} else {
		$groups = XOOPS_GROUP_ANONYMOUS;
	}
	$module_id = $xoopsModule->getVar('mid');
	$gperm_handler =& xoops_gethandler('groupperm');
		//権限がない場合
		if (!$gperm_handler->checkRight($perm_name, $perm_itemid, $groups, $module_id)) {
			//自分のフォルダではないのにアクセスしている場合	
			if($xoopsUser->getVar('uname')!=$unamefolder){
			//echo $xoopsUser->getVar('uname').$unamefolder;
			redirect_header (XOOPS_URL . "/modules/" . $xoopsModule->dirname() . "/index.php", 2 ,_MYHTML_ACCESS_DENIED_OPEN);
			}
			//自分のフォルダではないのにアクセスしている場合	
		}
		//権限がない場合終わり
//公開性の権限がないのに直リンクしているかチェック終わり

//ここにカテゴリーを表示させる予定
/*
//include_once XOOPS_ROOT_PATH."/class/xoopstree.php";
$xoopsOption['template_main'] = 'myhtml_viewcat.html';
$myts =& MyTextSanitizer::getInstance();// MyTextSanitizer object
$mytree = new XoopsTree($xoopsDB->prefix("myhtml_cat"),"cid","pid");

$cid = intval($HTTP_GET_VARS['cid']);

if (isset($HTTP_GET_VARS['show']) && $HTTP_GET_VARS['show']!="") {
	$show = intval($HTTP_GET_VARS['show']);
} else {
	$show = $xoopsModuleConfig['perpage'];
}
$min = isset($HTTP_GET_VARS['min']) ? intval($HTTP_GET_VARS['min']) : 0;
if (!isset($max)) {
	$max = $min + $show;
}
if(isset($HTTP_GET_VARS['orderby'])) {
	$orderby = convertorderbyin($HTTP_GET_VARS['orderby']);
} else {
	$orderby = "submitter ASC";
}


$pathstring = "<a href='index.php'>"._MD_MAIN."</a>&nbsp;:&nbsp;";
$pathstring .= $mytree->getNicePathFromId($cid, "title", "viewcat.php?op=");
$xoopsTpl->assign('category_path', $pathstring);
$xoopsTpl->assign('category_id', $cid);
// get child category objects
$arr=array();
$arr=$mytree->getFirstChild($cid, "title");
if ( count($arr) > 0 ) {
	$scount = 1;
	foreach($arr as $ele){
		$sub_arr=array();
		$sub_arr=$mytree->getFirstChild($ele['cid'], "title");
		$space = 0;
		$chcount = 0;
		$infercategories = "";
		foreach($sub_arr as $sub_ele){
			$chtitle=$myts->makeTboxData4Show($sub_ele['title']);
			if ($chcount>5){
				$infercategories .= "...";
				break;
			}
			if ($space>0) {
				$infercategories .= ", ";
			}
			$infercategories .= "<a href=\"".XOOPS_URL."/modules/myhtml/viewcat.php?cid=".$sub_ele['cid']."\">".$chtitle."</a>";
			$space++;
			$chcount++;
		}
		$xoopsTpl->append('subcategories', array('title' => $myts->makeTboxData4Show($ele['title']), 'id' => $ele['cid'], 'infercategories' => $infercategories, 'totallinks' => getTotalItems($ele['cid'], 1), 'count' => $scount, 'file' => "file"));
		$scount++;
	}
}
*/
//ここにカテゴリーを表示させる予定終わり

if ( empty($xoopsUser) )
{
    $user = "/guest";
}
else
{
    $user = $xoopsUser->getVar('uname','E');
}

$inserver = $_SERVER;

$url = XOOPS_URL . "/modules/" . $xoopsModule->dirname() . "/index.php"
     . $auth_delimiter;
$files = XOOPS_URL . "/modules/" . $xoopsModule->dirname() . "/files/";
$paths = XOOPS_ROOT_PATH . "/modules/" . $xoopsModule->dirname()
       . "/index.php" . $auth_delimiter;

if ( ( $auth_user [ $user ] == "" )
  && ( $auth_user ['/default'] == $auth_deny ) )
{
    redirect_header ( XOOPS_URL , 0 , _AT_ACCESS_DENIED );
}
//
if ( $auth_user [ $user ] == $auth_deny )
{
    redirect_header ( XOOPS_URL , 0 , _AT_ACCESS_DENIED );
}

if ( $auth_delimiter != "?" )
{
    $page = $_SERVER['PATH_INFO'];
}
else
{
    $page = "?" . $_SERVER['QUERY_STRING'];
}
if ( preg_match ( "/\:/" , $page ) )
{
    echo "XOOPS - Auth modules :-)";
    exit ();
}

if ( ( $page == "" )
  || ( preg_match ( "/\/$/" , $page ) )
  || ( preg_match ( "/\?$/" , $page ) )
  || ( preg_match ( "/index\.php$/" , $page ) ) )
{
    $url = XOOPS_URL . "/modules/" . $xoopsModule->dirname();

    if ( ! $auth_user [ $user ] == "" )
    {
        $next = $auth_user [ $user ];
    }
    else
    {
        $next = $auth_user ['/default'];
    }

    if ( preg_match ( "/\:/" , $next ) )
    {
        if ( $auth_redirect == 0 )
        {
            header ( "Location: $next" );
            exit ();
        }
        else
        {
            redirect_header ( $next , 0 , _AT_REDIRECT );
            exit ();
        }
    }
    else if ( ( $next != "" ) && ( $next != "index.php" ) )
    {
        if ( $auth_redirect == 0 )
        {
           header ( "Location: $url/index.php" . $auth_delimiter . $next );
            exit ();
        }
        else
        {
            redirect_header ( "$url/index.php" . $auth_delimiter . $next
                            , 5 , _AT_REDIRECT );
            exit ();
        }
    }
    else
    {
        redirect_header ( XOOPS_URL , 0 , _AT_ACCESS_DENIED );
        exit ();
    }
}

if ( $auth_delimiter != "/" )
{
    $page = "/" . substr ( $page , 1 );
}
//
$page = $auth_dir . $page;
if ( ! is_file ( $page ) )
{
	//echo $page;
    redirect_header ( XOOPS_URL , 2 , _AT_NOT_FOUND );
}

if ( ( strpos ( $page , ".html")  !== false )
  || ( strpos ( $page , ".php")   !== false )
  || ( strpos ( $page , ".cgi")   !== false ) )
{
   include ( XOOPS_ROOT_PATH . '/header.php' );
}
else
{
   $ext = substr ( strrchr ( $page , "." ) , 1 );

   if ( $auth_mimetype [ $ext ] == "" )
   {
       header ( "Content-Type: application/octet-stream" );
   }
   else
   {
       $set_mimetype = $auth_mimetype [ $ext ];
       header ( "Content-Type: $set_mimetype" );
   }
}

if ( ( strpos ( $page , ".html") !== false )
  || ( strpos ( $page , ".htm")  !== false )
  || ( strpos ( $page , ".php")  !== false )
  || ( strpos ( $page , ".php5") !== false )
  || ( strpos ( $page , ".php4") !== false )
  || ( strpos ( $page , ".php3") !== false ) )
{	
	//ファイルの読み込み
	//include_once "viewcat.php";
	//echo $page;
	include_once ( $page );
}
else if ( ( strpos ( $page , ".cgi") !== false )
       || ( strpos ( $page , ".pl")  !== false ) )
{
    foreach ( $inserver as $name => $value )
    {
        putenv ( $name . "=" . $value );
    }

    foreach ( $_POST as $name => $value )
    {
        if ( ! $inpost == "" )
        {
            $inpost .= "&";
        }

        $inpost .= urlencode ( $name ) . "=" . urlencode ( $value );
    }

    putenv ( "POST_DATA="  . $inpost );
    putenv ( "URL_DATA="   . $url );
    putenv ( "PATHS_DATA=" . $paths );
    putenv ( "FILES_DATA=" . $files );
    putenv ( "USER_DATA="  . $user );

    if ( ! empty($xoopsUser) )
    {
        if ( is_object ( $xoopsUser ) && $xoopsUser->isAdmin() )
        {
            putenv ( "USER_DATA_ADMIN=" . $xoopsUser->getVar('uname') );
        }

        putenv ( "USER_DATA_UID="     . $xoopsUser->getVar('uid') );
        putenv ( "USER_DATA_UNAME="   . $xoopsUser->getVar('uname') );
        putenv ( "USER_DATA_AVATAR="  . $xoopsUser->getVar('user_avatar') );
        putenv ( "USER_DATA_NAME="    . $xoopsUser->getVar('name') );
        putenv ( "USER_DATA_URL="     . $xoopsUser->getVar('url') );
        putenv ( "USER_DATA_MAIL="    . $xoopsUser->getVar('email') );

        if ( $xoopsUser->getVar('user_viewemail') == 1 )
        {
            putenv ( "USER_DATA_EMAIL=". $xoopsUser->getVar('email') );
        }

        putenv ( "USER_DATA_ICQ="     . $xoopsUser->getVar('user_icq') );
        putenv ( "USER_DATA_AIM="     . $xoopsUser->getVar('user_aim') );
        putenv ( "USER_DATA_YIM="     . $xoopsUser->getVar('user_yim') );
        putenv ( "USER_DATA_MSNM="    . $xoopsUser->getVar('user_msnm') );
        putenv ( "USER_DATA_FROM="    . $xoopsUser->getVar('user_from') );
        putenv ( "USER_DATA_OCC="     . $xoopsUser->getVar('user_occ') );
        putenv ( "USER_DATA_INTREST=" . $xoopsUser->getVar('user_intrest') );
    }

    passthru ( $page );
}
else
{
    $filename = substr ( strrchr ( $page , "/" ) , 1 );
    $filesize = filesize ( $page );

    mb_http_output ( "pass" );
//    header ( "Content-Disposition: inline; filename=$filename" );
    header ( "Content-length: $filesize" );
    header ( "Cache-Control: public" );
    header ( "Pragma: public" );
	readfile ( $page );
}

if ( ( strpos ( $page , ".html")  !== false )
  || ( strpos ( $page , ".php")   !== false )
  || ( strpos ( $page , ".cgi")   !== false ) )
{
    include ( XOOPS_ROOT_PATH . '/footer.php' );
}
?>