<?php
RegisterPlugin("bluecms","ActivePlugin_bluecms");
function ActivePlugin_bluecms(){
	Add_Filter_Plugin('Filter_Plugin_Admin_TopMenu','bluecms_AddMenu');
	Add_Filter_Plugin('Filter_Plugin_Zbp_MakeTemplatetags','bluecms_Footer');
	Add_Filter_Plugin('Filter_Plugin_Edit_Response','bluecms_Edit');
	Add_Filter_Plugin('Filter_Plugin_Category_Edit_Response', 'bluecms_Category');
	Add_Filter_Plugin('Filter_Plugin_Admin_Header', 'bluecms_CSS');
}
function bluecms_AddMenu(&$m){
	global $zbp;
	array_unshift($m, MakeTopMenu("root",'主题配置',$zbp->host . "zb_users/theme/bluecms/main.php?act=config","","topmenu_bluecms"));
}
function bluecms_SubMenu($id){
	$arySubMenu = array(
		0 => array('图片设置', 'config', 'left', false),
		1 => array('网站设置', 'other', 'left', false),
		2 => array('联系方式', 'contact', 'left', false),
		3 => array('分类id设置', 'fenlei', 'left', false),
		4 => array('nav导航栏', 'nav', 'left', false),
	);
	foreach($arySubMenu as $k => $v){
		echo '<a href="?act='.$v[1].'" '.($v[3]==true?'target="_blank"':'').'><span class="m-'.$v[2].' '.($id==$v[1]?'m-now':'').'">'.$v[0].'</span></a>';
	}
}
function bluecms_Footer(&$template) {
    global $zbp;
    $zbp->footer .= ''."\r\n";
}
function InstallPlugin_bluecms(){
	global $zbp;
    if(!$zbp->Config('bluecms')->HasKey('Version')){
		$zbp->Config('bluecms')->Version = '1.0';
		/*seo设置*/
		$zbp->Config('bluecms')->title = '新乡志远网络';
		$zbp->Config('bluecms')->keywords = '新乡志远网络';
        $zbp->Config('bluecms')->description = '新乡志远网络';
		$zbp->Config('bluecms')->separator = '_';
		$zbp->Config('bluecms')->tongji = '';	
		$zbp->Config('bluecms')->link1 = '';
		$zbp->Config('bluecms')->link2 = '';
		$zbp->Config('bluecms')->googlejia = '';

        /*联系方式*/
		$zbp->Config('bluecms')->add1 = '';
		$zbp->Config('bluecms')->add2 = '';
		$zbp->Config('bluecms')->add3 = '';
		$zbp->Config('bluecms')->add4 = '';
		$zbp->Config('bluecms')->add5 = '';
		$zbp->Config('bluecms')->add6 = '';
		$zbp->Config('bluecms')->add7 = '';
		$zbp->Config('bluecms')->add8 = '';
		$zbp->Config('bluecms')->add9 = '© 2018 <a href="http://demo.xxzhiyuan.cn/bluecms/">我的网站</a>&nbsp;Powered By <a href="http://www.zblogcn.com/" title="RainbowSoft Z-BlogPHP" target="_blank">Z-BlogPHP</a>&nbsp;Theme By <a href="http://www.xxzhiyuan.cn/" title="ZBlog模板" target="_blank">ZBlog模板</a>&nbsp;备案信息';		
		

		/*首页分类id设置*/
        $zbp->Config('bluecms')->chanpin = '1';
		$zbp->Config('bluecms')->news = '1';
		$zbp->Config('bluecms')->news1 = '1';
		$zbp->Config('bluecms')->news2 = '1';
           /*ad*/
		$zbp->Config('bluecms')->topad = '';
		$zbp->Config('bluecms')->topadNO = 1;
		$zbp->Config('bluecms')->btmad = '';
		$zbp->Config('bluecms')->btmadNO = 1;
		$zbp->Config('bluecms')->postad = '';
		$zbp->Config('bluecms')->postadNO = 1;
		$zbp->Config('bluecms')->clearSetting = 0;
	}
		$zbp->SaveConfig('bluecms');
}
function UninstallPlugin_bluecms(){
	global $zbp;
	if ($zbp->Config('bluecms')->clearSetting){
		$zbp->DelConfig('bluecms');		
	}
}
function bluecms_thumbnail($related) {
     global $zbp;	
	$temp=mt_rand(1,10);
	$pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
	$content = $related->Content; 
	preg_match_all($pattern,$content,$matchContent);
	if(isset($matchContent[1][0])){
		$thumb=$matchContent[1][0]; 
	}else{
		$thumb=$zbp->host . "zb_users/theme/" .$zbp->theme. "/style/random/" .$temp. ".jpg";
	}
    return $thumb;
}
function bluecms_intro($as,$type,$long,$other){
	global $zbp;
    $str = '';
    if ($type=='0') {
    $str .= preg_replace('/[\r\n\s]+/', '', trim(SubStrUTF8(TransferHTML($as->Intro,'[nohtml]'),$long)).$other);
    } else {
    $str .= preg_replace('/[\r\n\s]+/', '', trim(SubStrUTF8(TransferHTML($as->Content,'[nohtml]'),$long)).$other);
    }
    return $str;
}
function bluecms_subCate($id){
	global $zbp;
	if($id){
		foreach ($zbp->categorys[$id]->SubCategorys as $cate) {
			echo '<li><a href="'.$cate->Url.'" title="'.$cate->Name.'">'.$cate->Name.'</a></li>';
		}
	}
}

function bluecms_GetArticleCategorys($Rows,$CategoryID,$hassubcate){
        global $zbp;
    $ids = strpos($CategoryID,',') !== false ? explode(',',$CategoryID) : array($CategoryID);
    $wherearray=array(); 
    foreach ($ids as $cateid){
      if (!$hassubcate) {
        $wherearray[]=array('log_CateID',$cateid); 
      }else{
                $wherearray[] = array('log_CateID', $cateid);
                foreach ($zbp->categorys[$cateid]->SubCategorys as $subcate) {
                    $wherearray[] = array('log_CateID', $subcate->ID);
                }
      }
    }
    $where=array( 
                    array('array',$wherearray), 
                    array('=','log_Status','0'), 
                    ); 
 
    $order = array('log_PostTime'=>'DESC'); 
    $articles=    $zbp->GetArticleList(array('*'),$where,$order,array($Rows),'');     
 
        return $articles;
}
function bluecms_hotTags(){//top10 tag
        global $zbp,$str;
        $str = '';
        $array = $zbp->GetTagList('','',array('tag_Count'=>'DESC'),array(10),'');
        foreach ($array as $tag) {
            $str .= "<a href=\"{$tag->Url}\" title=\"{$tag->Name}\">{$tag->Name}</a> ";
        }
        return $str;
    }
	
	function bluecms_is_https() {
    if ( !empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') {
        return true;
    } elseif ( isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' ) {
        return true;
    } elseif ( !empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') {
        return true;
    }
    return false;
}
function bluecms_is_mobile() {
	if ( empty($_SERVER['HTTP_USER_AGENT']) ) {
		$is_mobile = false;
	} elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false // many mobile devices (all iPhone, iPad, etc.)
		|| strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false
		|| strpos($_SERVER['HTTP_USER_AGENT'], 'Silk/') !== false
		|| strpos($_SERVER['HTTP_USER_AGENT'], 'Kindle') !== false
		|| strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') !== false
		|| strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false
		|| strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mobi') !== false ) {
			$is_mobile = true;
	} else {
		$is_mobile = false;
	} 
	return $is_mobile;
}

function bluecms_CSS() {
    global $zbp;
    echo '<link href="' .$zbp->host. 'zb_users/theme/bluecms/style/css/zdi.css" rel="stylesheet" type="text/css" />';
}
function bluecms_Edit(){
    global $zbp,$article;
	echo '<div id="editCustomMeta" ><div class="title">主题自定区域</div><div class="form-group"><label  class="title" for="meta_seotitle">seo标题</label><input type="text" id="meta_seotitle" name="meta_seotitle" value="'.htmlspecialchars($article->Metas->seotitle).'" /></div><div class="form-group"><label  class="title" for="meta_seokey">seo关键词</label><input type="text" id="meta_seokey" name="meta_seokey" value="'.htmlspecialchars($article->Metas->seokey).'" /></div></div>';
}
function bluecms_Category(){
    global $zbp,$cate;
   	echo '<div id="editCustomMeta" ><div class="title">主题自定区域</div><div class="form-group"><label  class="title" for="meta_seotitle">分类seo标题</label><input type="text" id="meta_seotitle" name="meta_seotitle" value="'.htmlspecialchars($cate->Metas->seotitle).'" /></div><div class="form-group"><label  class="title" for="meta_seokey">分类seokey</label><input type="text" id="meta_seokey" name="meta_seokey" value="'.htmlspecialchars($cate->Metas->seokey).'" /></div></div>';
}
function bluecms_cloud(){//top10 tag
        global $zbp,$str;
        $str = '';
        $array = $zbp->GetTagList('','',array('tag_Count'=>'DESC'),array(15),'');
        foreach ($array as $tag) {
            $str .= "<a href=\"{$tag->Url}\" title=\"{$tag->Name}\">{$tag->Name}</a>";
        }
        return $str;
    }
?>