<?php
if(!defined('IN_MICECMS')){header("HTTP/1.0 404 Not Found");die();}
function MenuList($micefun,$menuid){
	if($menuid){
		$menu=new model_Menu();
		$menu->setSubsection('WHERE isvisible=0 AND parentid='.$menuid.' ORDER BY orderid ASC');
		$menus=$menu->doSelect();
	}else{
		$menus=new model_Menu();
	}
	return $menus;
}

function CategoryName($micefun,$str){
	$category=new model_Category();
	$category->setSubsection('WHERE isvisible=0 AND type LIKE \'article%\' ORDER BY category_id');
	$categoryrs=$category->doSelect();

	$categoryname='';
	foreach($categoryrs as $category){
		if(strstr($str,','.$category->getCategoryId().',')){
			if($categoryname)	$categoryname.="<br>";
			$categoryname.=$category->getTitle();
		}
	}
	return $categoryname;
}

function LinkCategoryName($micefun,$str){
	$category=new model_Category();
	$category->setSubsection('WHERE isvisible=0 AND type = \'link\' ORDER BY category_id');
	$categoryrs=$category->doSelect();

	$categoryname='';
	foreach($categoryrs as $category){
		if(strstr($str,','.$category->getCategoryId().',')){
			if($categoryname)	$categoryname.="<br>";
			$categoryname.=$category->getTitle();
		}
	}
	if(!$categoryname){
		$categoryname='默认分类';
	}
	return $categoryname;
}

function CategoryId($micefun,$str){
	$strs=explode(',',$str);
	return $strs[sizeof($strs)-2];
}

function UsergroupName($micefun,$str){
	$usergroup=new model_Usergroup();
	$usergroup->setSubsection('WHERE usergroup_id='.$str);
	$results=$usergroup->doSelectOne();
	
	return $results->getName();
}

//执行SQL查询,返回html结果
function record_html($query,$num) {
	global $conn;
	$query=str_replace("`","",$query);
	$body = '<h3>'.$query.'</h3>';
	//执行sql
	$rs=@$conn->RawExecute($query);
	if ($rs===false) $body .= '<p style="color:red">';
	else $body.='<p>';
	// 显示返回信息
	$body .= $conn->ErrorMsg();
	$body .=   '</p>';
	if (is_bool($rs))
		return $body;
	$body .=  'Affected Rows: '.$conn->Affected_Rows().' - ';
	$row_total = $rs->RecordCount();
	if (!$row_total) return $body;
	$body .=  'Result rows: '.$row_total.' - ';
	$body .=  'Fields: '.$rs->FieldCount();
	$body.= '<table   border="0" cellpadding="0" cellspacing="1" bgcolor="#666666">';
	$body .= "<thead><tr>\n";
	$rsa = $rs->GetArray();	$rs = null;
	foreach(array_keys($rsa[0]) as $col)
		$body.='<th  bgcolor="#FFFFFF">'.$col."</th>\n";
	$body .= "</tr></thead>\n";
	reset($rsa);
	foreach($rsa as $row) {
		$body .="<tbody><tr>\n";
		foreach($row as $val)
			$body .= '<td  bgcolor="#FFFFFF" valign="top">'.$val."</td>\n";
		$body .= "</tbody></tr>\n";
	} $rsa = null;
	$body .= '</table>';
	return $body;
}

//删除指定目录下的所有文件
function dir_clear($dir) { 
	$ret=true;
    $directory = dir($dir);                //创建一个dir类，用来读取目录中的每一个文件 
    while($entry = $directory->read()) {   //循环每一个文件,并取得文件名$entry 
        $filename = $dir.'/'.$entry;       //取得完整的文件名，带路径的 
        if(is_file($filename)) {           //如果是文件，则执行删除操作 
            $ret=@unlink($filename); 
        } 
    } 
    $directory->close();                   //关闭读取目录文件的类 
	return $ret;
}

//弹出对话框
function MessageBox($jsstring)
{
	$ret= '<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta http-equiv="Content-Language" content="zh-cn" /><title>'.MICE_TITLE.'</title><base href="'.MICE_URL.MICE_TPL.'/admin/" /><link href="css/Default.css" rel="stylesheet" type="text/css" /><link href="css/fieldset.css" rel="stylesheet" type="text/css"><script language="JavaScript">var baseurl = "'.MICE_URL.MICE_TPL.'/admin/";</script><script type="text/javascript" src="js/Main.js"></script>';

	$ret.= "<Script language='JavaScript'>function msgbox(){";
	if($jsstring != "")
	{
		$ret.= $jsstring;
	}
	$ret.= "return;}</Script></head><body onload='javascript:msgbox();'><table width='100%' height='100%'><tr><td></td></tr></table></body></html>";
	echo $ret;
}

function MiceStrstr($micefun,$str1,$str2){
	if(strstr($str1,$str2))
		return 1;
	else
		return 0;
}

//字符串重复函数
function MiceStrRepeat($micefun,$num){
	$strs='';
	for($i=1;$i<$num;$i++){
		$strs.='—';
	}
	return $strs;
}

function UpPostnums($micefun,$userid){
	$user=new model_User();
	$user->setSubsection('WHERE user_id='.$userid);
	$results=$user->doSelectOne();

	$user->setPostnums($results->getPostnums()+1);
	$user->doUpdate();

	return '';
}

//提取文章中的第一张图片的地址
function GetImgUrl($str)   
{   
  $ereg = '/<img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i';   

  if(preg_match_all($ereg,$str,$imgs))   
  {   
	  $path = $imgs[1];
	  $imgurl = str_replace(array("'","\"","\'","\\"),array('','','',''),$path[0]) ;
	  return $imgurl;   
  }   
  else   
  {   
	return false;   
  }   
}

//获取分类列表(SQL条件语句，起始记录数，偏移记录数)
function CategoryLists($micefun,$subsql,$nrows,$offset){		//Nrows,起始记录数，0为第一条,1为第2条
	$category=new model_Category();
	if($subsql){
		$category->setSubsection($subsql);
	}
	if($offset > 0){
		$category->setNrows($nrows);			//记录下标从0开始
		$category->setOffset($offset);
		$results=$category->doSelectLimit();
	}else{
		$results=$category->doSelect();
	}
	return $results;
}

//获取文件分类列表(SQL条件语句，起始记录数，偏移记录数)
function FileCategoryLists($micefun,$subsql,$nrows,$offset){		//Nrows,起始记录数，0为第一条,1为第2条
	$category=new model_FileCategory();
	if($subsql){
		$category->setSubsection($subsql);
	}
	if($offset > 0){
		$category->setNrows($nrows);			//记录下标从0开始
		$category->setOffset($offset);
		$results=$category->doSelectLimit();
	}else{
		$results=$category->doSelect();
	}
	return $results;
}

//获取菜单列表(SQL条件语句，起始记录数，偏移记录数)
function MenuLists($micefun,$subsql,$nrows,$offset){		//Nrows,起始记录数，0为第一条,1为第2条
	$category=new model_Menu();
	if($subsql){
		$category->setSubsection($subsql);
	}
	if($offset > 0){
		$category->setNrows($nrows);			//记录下标从0开始
		$category->setOffset($offset);
		$results=$category->doSelectLimit();
	}else{
		$results=$category->doSelect();
	}
	return $results;
}

//判断字符串2是否包含字符串1,包含返回 checked
function IsChecked($micefun,$str1,$str2,$str3='micedefaultvalue'){
	if(stripos($str2,','.$str1.',')===false){
		if($str1==$str3){
			return 'checked';
		}else{
			return '';
		}
	}else{
		return 'checked';
	}
}
?>