<?php
// $Id: index.php,v 1.1 2004/01/29 14:45:12 buennagel Exp $
//  ------------------------------------------------------------------------ //
//                XOOPS - PHP Content Management System                      //
//                    Copyright (c) 2000 XOOPS.org                           //
//                       <http://www.xoops.org/>                             //
// ------------------------------------------------------------------------- //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  You may not change or alter any portion of this comment or credits       //
//  of supporting developers from this source code or any supporting         //
//  source code which is considered copyrighted (c) material of the          //
//  original comment or credit authors.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
//  ------------------------------------------------------------------------ //
include "header.php";
include_once XOOPS_ROOT_PATH."/class/xoopstree.php";
$myts =& MyTextSanitizer::getInstance(); // MyTextSanitizer object
$mytree = new XoopsTree($xoopsDB->prefix("myhtml_cat"),"cid","pid");
$xoopsOption['template_main'] = 'myhtml_index.html';
include XOOPS_ROOT_PATH."/header.php";

$sql = "SELECT cid, title, imgurl FROM ".$xoopsDB->prefix("myhtml_cat")." WHERE pid = 0  ORDER BY cid";
$result=$xoopsDB->query($sql);
$count = 1;
$count_result=mysql_num_rows($result);

//カテゴリーの表示ループ
while($myrow = $xoopsDB->fetchArray($result)) {
//adminと権限があるユーザの場合のみ表示
$uid=$xoopsUser->getVar('uid');
//Get permission
$perm_name = 'myhtml_permit';
//10000000はカテゴリーに対する
$perm_itemid = 10000000+$myrow['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)) {
		//権限がない場合は、変数として何も持っていかない。
		$xoopsTpl->append('categories', array('image' => "", 'id' => "", 'title' => "", 'subcategories' => $subcategories, 'totaldownloads' => "", 'count' => ""));
		}
		else{
		//権限がある場合はカテゴリー表示
		$title = $myts->makeTboxData4Show($myrow['title']);
		if ($myrow['imgurl'] && $myrow['imgurl'] != "http://"){
			$imgurl = $myts->makeTboxData4Edit($myrow['imgurl']);
		} else {
			$imgurl = '';
		}
		$totaldownload = getTotalItems($myrow['cid'], 1);
		// get child category objects
		$arr=array();
		$arr=$mytree->getFirstChild($myrow['cid'], "title");
		$space = 0;
		$chcount = 0;
		$subcategories = "";
		foreach($arr as $ele){
			$chtitle=$myts->makeTboxData4Show($ele['title']);
			if ($chcount>5){
				$subcategories .= "...";
				break;
			}
			if ($space>0) {
				$subcategories .= ", ";
			}
			$subcategories .= "<a href=\"".XOOPS_URL."/modules/myhtml/viewcat.php?cid=".$ele['cid']."\">".$chtitle."</a>";
			$space++;
			$chcount++;
		}
		
		$xoopsTpl->append('categories', array('image' => $imgurl, 'id' => $myrow['cid'], 'title' => $myts->makeTboxData4Show($myrow['title']), 'subcategories' => $subcategories, 'totaldownloads' => $totaldownload, 'count' => $count));
		
		//cidを変数として持っておく。
		$cid_sql_and.=" or (cid=".$myrow['cid']."";
		//公開性のチェック
		$perm_name = 'myhtml_permit';
		//10100000は公開性のアクセス権
		$perm_itemid = 10100000+$myrow['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)) {
			$cid_sql_and.=" and d.submitter=".$uid."";
			}
		$cid_sql_and.=")";
		}
		//権限がある場合終わり
		//チェック終わり
		
		$count++;	
}
//カテゴリーの表示ループ終わり

//SQLの頭から5文字までを取り除く(このSQLがプログラム上, or ではじまっているため)
$cid_sql_and=substr_replace($cid_sql_and,"",0,4);
//もし1件でもあればandを付ける
	if($count_result>=1){
	$cid_sql_and=" and (".$cid_sql_and.")";
	}
//もし1件でもあればandを付ける終わり

//ダウンロードの件数の検索
$sql="SELECT COUNT(*) FROM ".$xoopsDB->prefix("myhtml_downloads")." WHERE status>0".$cid_sql_and."";
list($numrows)=$xoopsDB->fetchRow($xoopsDB->query($sql));

$xoopsTpl->assign('lang_thereare', sprintf(_MD_THEREARE,$numrows));
if ($xoopsModuleConfig['useshots'] == 1) {
	$xoopsTpl->assign('shotwidth', $xoopsModuleConfig['shotwidth']);
	$xoopsTpl->assign('tablewidth', $xoopsModuleConfig['shotwidth'] + 10);
	$xoopsTpl->assign('show_screenshot', true);
	$xoopsTpl->assign('lang_noscreenshot', _MD_NOSHOTS);
}
if ($xoopsUser && $xoopsUser->isAdmin($xoopsModule->mid())) {
	$isadmin = true;
} else {
	$isadmin = false;
}
$xoopsTpl->assign('lang_description', _MD_DESCRIPTIONC);
$xoopsTpl->assign('lang_lastupdate', _MD_LASTUPDATEC);
$xoopsTpl->assign('lang_hits', _MD_HITSC);
$xoopsTpl->assign('lang_ratingc', _MD_RATINGC);
$xoopsTpl->assign('lang_email', _MD_EMAILC);
$xoopsTpl->assign('lang_ratethissite', _MD_RATETHISFILE);
$xoopsTpl->assign('lang_reportbroken', _MD_REPORTBROKEN);
$xoopsTpl->assign('lang_tellafriend', _MD_TELLAFRIEND);
$xoopsTpl->assign('lang_modify', _MD_MODIFY);
$xoopsTpl->assign('lang_version' , _MD_VERSION);
$xoopsTpl->assign('lang_subdate' , _MD_SUBMITDATE);
$xoopsTpl->assign('lang_dlnow' , _MD_DLNOW);
$xoopsTpl->assign('lang_category' , _MD_CATEGORYC);
$xoopsTpl->assign('lang_size' , _MD_FILESIZE);
$xoopsTpl->assign('lang_platform' , _MD_SUPPORTEDPLAT);
$xoopsTpl->assign('lang_homepage' , _MD_HOMEPAGE);
$xoopsTpl->assign('lang_latestlistings' , _MD_LATESTLIST);
$xoopsTpl->assign('lang_comments' , _COMMENTS);

//一覧表示のSQL
//$sql="SELECT d.lid, d.cid, d.title, d.url, d.homepage, d.version, d.size, d.platform, d.logourl, d.status, d.date, d.hits, d.rating, d.votes, d.comments, t.description , d.file,d.submitter FROM ".$xoopsDB->prefix("myhtml_downloads")." d, ".$xoopsDB->prefix("myhtml_text")." t where d.status>0 AND d.lid=t.lid".$cid_sql_and." group by submitter,cid order by d.date desc";
//echo $gperm_handler->checkRight($perm_name, $perm_itemid, $groups, $module_id);
$sql="SELECT d.lid, d.cid, d.title, d.url, d.homepage, d.version, d.size, d.platform, d.logourl, d.status, d.date, d.hits, d.rating, d.votes, d.comments, t.description , d.file,d.submitter FROM ".$xoopsDB->prefix("myhtml_downloads")." d, ".$xoopsDB->prefix("myhtml_text")." t where d.status>0".$cid_sql_and.$submitter_sql_and." group by submitter,cid order by d.date desc";
//echo $sql;

$result=$xoopsDB->query($sql, $xoopsModuleConfig['newdownloads'], 0);
while(list($lid, $cid, $dtitle, $url, $homepage, $version, $size, $platform, $logourl, $status, $date, $hits, $rating, $votes, $comments, $description,$file,$submitter)=$xoopsDB->fetchRow($result)) {
	$path = $mytree->getPathFromId($cid, "title");
	$path = substr($path, 1);
	$path = str_replace("/"," <img src='".XOOPS_URL."/modules/myhtml/images/arrow.gif' board='0' alt=''> ",$path);
	$rating = number_format($rating, 2);
	$dtitle = $myts->makeTboxData4Show($dtitle);
	$url = $myts->makeTboxData4Show($url);
	$homepage = $myts->makeTboxData4Show($homepage);
	$version = $myts->makeTboxData4Show($version);
	$size = PrettySize($myts->makeTboxData4Show($size));
	$platform = $myts->makeTboxData4Show($platform);
	$logourl = $myts->makeTboxData4Show($logourl);
	//$datetime = formatTimestamp($time,"s");
	$datetime = formatTimestamp($date, "Y-m-d H:i:s");
	$description = $myts->makeTareaData4Show($description,0); //no html
	$file= $file;
	$count=$count;
	$date=formatTimestamp($date, "Y-m-d H:i:s");
	$submitter=$submitter;
	
	//get uname,name
	$sql1="SELECT uname,name from ".$xoopsDB->prefix("users")." where uid=".$submitter."";
	$result1 = $xoopsDB->query($sql1);
	$name=mysql_result($result1,0,"name");
	$uname=mysql_result($result1,0,"uname");
	
	$new = newdownloadgraphic($time, $status);
	$pop = popgraphic($hits);
	if ($isadmin) {
		$adminlink = '<a href="'.XOOPS_URL.'/modules/myhtml/admin/index.php?lid='.$lid.'&fct=myhtml&op=modDownload"><img src="'.XOOPS_URL.'/modules/myhtml/images/editicon.gif" border="0" alt="'._MD_EDITTHISDL.'" /></a>';
	} else {
		$adminlink = '';
	}
	if ($votes == 1) {
		$votestring = _MD_ONEVOTE;
	} else {
		$votestring = sprintf(_MD_NUMVOTES,$votes);
	}
	$xoopsTpl->append('file', array('id' => $lid,'test'=>$test,'cid'=>$cid,'rating' => $rating,'title' => $dtitle.$new.$pop,'logourl' => $logourl,'datetime' => $datetime,'description' => $description,'adminlink' => $adminlink,'hits' => $hits,'votes' => $votestring, 'comments' => $comments, 'platform' => $platform,'size'  => $size,'file' => $file,'name' => $name,'uname' => $uname,'submitter' => $submitter,'marge0'=>$cid."_".$submitter,'cid1'=>$_REQUEST['cid'],'submitter1'=>$_REQUEST['submitter'],'marge1'=>$_REQUEST['cid']."_".$_REQUEST['submitter'],'homepage' => $homepage,'version'  => $version,'category'  => $path,'lang_dltimes' => sprintf(_MD_DLTIMES,$hits),'mail_subject' => rawurlencode(sprintf(_MD_INTFILEFOUND,$xoopsConfig['sitename'])),'mail_body' => rawurlencode(sprintf(_MD_INTFILEFOUND,$xoopsConfig['sitename']).':  '.XOOPS_URL.'/modules/myhtml/singlefile.php?cid='.$cid.'&amp;lid='.$lid)));
	//$xoopsTpl->append('file', array('id' => $lid,'test'=>$test,'cid'=>$cid,'rating' => $rating,'title' => $dtitle.$new.$pop,'logourl' => $logourl,'updated' => $datetime,'description' => $description,'adminlink' => $adminlink,'hits' => $hits,'votes' => $votestring, 'comments' => $comments, 'platform' => $platform,'size'  => $size,'file' => $file,'homepage' => $homepage,'version'  => $version,'category'  => $path,'lang_dltimes' => sprintf(_MD_DLTIMES,$hits),'mail_subject' => rawurlencode(sprintf(_MD_INTFILEFOUND,$xoopsConfig['sitename'])),'mail_body' => rawurlencode(sprintf(_MD_INTFILEFOUND,$xoopsConfig['sitename']).':  '.XOOPS_URL.'/modules/myhtml/singlefile.php?cid='.$cid.'&amp;lid='.$lid)));
}

include XOOPS_ROOT_PATH."/modules/myhtml/footer.php";
?>