<?php
require_once(dirname(__FILE__)."/../inc/common.php");
$action = isset($action) ? $action : '';
//升级服务器，如果有变动，请到 http://bbs.djcms.net 查询
$updateHost = 'http://www.163.com/';
//当前软件版本锁定文件
$verLockFile = 'imgs/ver.txt';
//升级日志文件
$updatelogfile = 'imgs/updatelog.txt';
$selfMenuXml = 'imgs/quickmenu.txt';
$customMenuXml ="imgs/custommenu.txt";
$menuList = getBigMenuInfo(0);

function viewFoot() {
	global $t1;
	echo "<div align=center>";
	echo getRunTime($t1);
	echo "</div><div align=center >Copyright 2005-2009 All rights reserved. <a target=\"_blank\" href=\"http://www.djcms.net/\">DjCms2.0</a></div>" .chr(10). "</body>" .chr(10). "</html>";
}

function viewHead($str){
	checkPower();
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gbk" />
<meta http-equiv="X-UA-Compatible" content="IE=7" />
<TITLE><?=$str?>-DjCms后台管理</TITLE>
<link href="imgs/admin.css" rel="stylesheet" type="text/css" />
<script src="../js/common.js" type="text/javascript"></script>
<script src="imgs/main.js" type="text/javascript"></script>
<style type="text/css">
.btn{border:1px solid;border-color:#fff #999 #999 #fff;}
</style>
</head>
<body>
<?php
}

function getBigMenuInfo($level) {
	switch (intval($level)) {
		case "0" :
			$bigMenu[0][0]='首页' ; $bigMenu[0][1]='common' ; $bigMenu[0][2]='index.php?action=notice';
			$bigMenu[1][0]="数据" ; $bigMenu[1][1]="content" ; $bigMenu[1][2]="admin_video.php?action=else";
			$bigMenu[2][0]="模板" ; $bigMenu[2][1]="template" ; $bigMenu[2][2]="admin_template.php?action=main";
			$bigMenu[3][0]="生成" ; $bigMenu[3][1]="make" ; $bigMenu[3][2]="admin_makehtml.php?action=main";
			$bigMenu[4][0]="工具" ; $bigMenu[4][1]="tool" ; $bigMenu[4][2]="admin_datarelate.php?action=repeat";
			$bigMenu[5][0]="广告" ; $bigMenu[5][1]="ads" ; $bigMenu[5][2]="admin_ads.php?action=main";
			$bigMenu[6][0]="采集" ; $bigMenu[6][1]="gathersoft" ; $bigMenu[6][2]="admin_gather.php?action=main";
			$bigMenu[7][0]="扩展" ; $bigMenu[7][1]="webhelper" ; $bigMenu[7][2]="admin_gbook.php?action=comment&where=gbook";
			$bigMenu[8][0]="系统" ; $bigMenu[8][1]="system" ; $bigMenu[8][2]="admin_config.php";
			break;
		case "1" :
			$bigMenu[0][0]="数据管理" ; $bigMenu[0][1]="common" ; $bigMenu[0][2]="admin_video.php?action=else";
			break;
	}
	return $bigMenu;
}

function getSmallMenuInfo($level) {
	switch (intval($level)) {
		case 0:
			$smallMenu[0]="后台首页,index.php?action=notice|快捷菜单,admin_selfmenu.php|".getSelfMenu(0);
			$smallMenu[1]="资源采集管理,admin_reslib.php|临时表管理,admin_tempvideo.php||分类管理,admin_type.php||数据管理,admin_video.php?action=else|添加数据,admin_video.php?action=add|连载管理,admin_video.php?m_state=ok|推荐管理,admin_video.php?m_commend=ok|数据回收站,admin_video.php?action=recycle||专题管理,admin_topic.php||新闻分类,admin_newstype.php|新闻管理,admin_news.php|新闻添加,admin_news.php?action=add|新闻回收站,admin_news.php?action=recycle||报错数据,admin_gbook.php?action=reporterror&where=reporterror";
			$smallMenu[2]="模板管理,admin_template.php?action=main|管理自定义模板,admin_template.php?action=custom||自定义标签,admin_selflabel.php||标签向导,admin_labelguide.php";
			$smallMenu[3]="生成选项,admin_makehtml.php?action=main||生成百度地图,admin_makehtml.php?action=baidu|生成谷歌地图,admin_makehtml.php?action=google|生成RSS,admin_makehtml.php?action=rss";
			$smallMenu[4]="重复数据检测,admin_datarelate.php?action=repeat|数据批量替换,admin_datarelate.php?action=batch|删除指定来源,admin_datarelate.php?action=delvideoform|批量设置点击量,admin_datarelate.php?action=randomset||无效图片清除,admin_datarelate.php?action=checkpic||SQL高级助手,admin_datarelate.php?action=sql||数据库备份,admin_database.php|数据库还原,admin_database.php?action=restore|数据库优化,admin_database.php?action=repair";
			$smallMenu[5]="广告管理,admin_ads.php?action=main|添加广告,admin_ads.php?action=add||友情链接,admin_link.php";
			$smallMenu[6]="项目列表,admin_gather.php?action=main|添加项目,admin_gather.php?action=wizard|临时数据,admin_gather.php?action=tempdatabase|分类转换,admin_gather.php?action=customercls|信息过滤,admin_gather.php?action=filters";
			$smallMenu[7]="留言管理,admin_gbook.php?action=comment&where=gbook|回复留言,../gbook.php|视频评论管理,admin_comment.php?action=comment&where=1|新闻评论管理,admin_comment.php?action=comment&where=2|幻灯片管理,admin_expand.php";
			$smallMenu[8]="网站配置,admin_config.php||播放器设置,admin_player.php|播放来源管理,admin_player.php?action=boardsource||系统账号管理,admin_manager.php";
			break;
		case 1:
		    $smallMenu[0]="后台首页,index.php?action=notice|快捷菜单,admin_selfmenu.php|数据管理,admin_video.php?action=else|添加数据,admin_video.php?action=add|生成选项,admin_makehtml.php?action=main|".getSelfMenu(1);
			break;
	}
	return $smallMenu;
}

function getSelfMenu($level) {
	global $selfMenuXml,$customMenuXml;
	$path=$selfMenuXml;
	if ($level==1) $path=$customMenuXml;
	$dtp=trim(file_get_contents($path));
    $dtp=str_replace(chr(13).chr(10), "|", $dtp);
	if (!empty($dtp)) {
	    return "|".$dtp;
	} else {
	    return "";
	}
}

function checkPower() {
	$loginValidate = "djcms";
	$row = DB::fetch_first("SELECT m_random,m_username,m_level FROM ".DB::table('manager')." WHERE m_username='".getcookie("m_username")."'");
	if (!is_array($row)) { 
	    clearPower();
	} else {
	    $loginValidate = md5(getAgent().getIp().$row['m_random']);
	    if (getcookie("check".$row['m_username']) != $loginValidate){
		   clearPower();
	    }
	    checkManagerLevel($row['m_level']);
	}
}

function selfManageDir() {
	if ($_SERVER["SERVER_PORT"]<>"80"){
    	$dir = "http://".$_SERVER['SERVER_NAME']. ":" .$_SERVER["SERVER_PORT"]. $_SERVER['SCRIPT_NAME'];
    } else {
    	$dir = "http://".$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'];
	}
	$strt = explode('/',$dir);
	$dir = $strt[3];
	$ps = preg_split("/\./", $dir);
	$dir = $ps[count($ps)-1]."/";
	return $dir;
}

function checkManagerLevel($level) {
	$curUrl = $_SERVER['SCRIPT_NAME'];
	if ((strpos($curUrl, "admin_video.php") == 0) && (strpos($curUrl, "index.php") == 0) && (strpos($curUrl, "admin_ajax.php") == 0) && (strpos($curUrl, "admin_webgather.php") == 0) && (strpos($curUrl, "admin_makehtml.php") == 0) && (strpos($curUrl, "admin_selfmenu.php") == 0)) {
		if (intval($level) != 0) alertMsg("没有权限","index.php");
    }
}

function clearPower() {
	dsetcookie("check".getcookie("m_username"));
	dsetcookie("m_username");
	dsetcookie("m_id");
	dsetcookie("m_level");
	dsetcookie("loginflag");
	echo "<script>top.location.href='index.php?action=login';</script>";
}

function getReferedId($str) {
	if (isExistStr($str,"土豆高清")) return "hd_tudou";
	if (isExistStr($str,"新浪高清")) return "hd_iask";
	if (isExistStr($str,"搜狐高清")) return "hd_sohu";
	if (isExistStr($str,"天线高清")) return "hd_openv";
	if (isExistStr($str,"56高清")) return "hd_56";
	if (isExistStr($str,"56")) return "56";
	if (isExistStr($str,"优酷")) return "youku";
	if (isExistStr($str,"土豆")) return "tudou";
	if (isExistStr($str,"搜狐")) return "sohu";
	if (isExistStr($str,"新浪")) return "iask";
	if (isExistStr($str,"六间房")) return "6rooms";
	if (isExistStr($str,"qq")) return "qq";
	if (isExistStr($str,"youtube")) return "youtube";
	if (isExistStr($str,"17173")) return "17173";
	if (isExistStr($str,"ku6视频")) return "ku6";
	if (isExistStr($str,"FLV")) return "flv";
	if (isExistStr($str,"SWF")) return "swf";
	if (isExistStr($str,"real")) return "real";
	if (isExistStr($str,"media")) return "media";
	if (isExistStr($str,"qvod")) return "qvod";
	if (isExistStr($str,"ppstream")) return "pps";
	if (isExistStr($str,"迅播高清")) return "gvod";
	if (isExistStr($str,"远古高清")) return "wp2008";
	if (isExistStr($str,"ppvod高清")) return "ppvod";
	if (isExistStr($str,"闪播Pvod")) return "pvod";
	if (isExistStr($str,"播客CC")) return "cc";
	if (isExistStr($str,"皮皮影音")) return "pipi";
	if (isExistStr($str,"激动")) return "joy";
}

function downSinglePic($picUrl,$vid,$vname,$filePath,$infotype) {
	if ($infotype=="") $spanstr=""; else $spanstr="<br/>";
	if (empty($picUrl) || substr($picUrl,0,7) != 'http://') {
		echo "数据<font color=red>".$vname."</font>的图片路径错误1,请检查是否有效  ".$spanstr;
		return false;
	}
	$fileext = getFileFormat($filePath);
	$ps = explode("/",$picUrl);
	$filename = $ps[count($ps)-1];
	if (strpos($picUrl, ".ykimg.com/") > 0) {
		$fileext = ".gif";
		$filePath = $filePath.$fileext;
	}
	if (!empty($fileext) && strpos("|.jpg|.gif|.png|.bmp|.jpeg|",strtolower($fileext)) > 0) {
		if (!(strpos($picUrl, ".ykimg.com/") > 0)) {
			if (empty($filename) || strpos(' '.$filename,'www.') > 0 || strpos($filename,".") == 0) {
				echo "数据<font color=red>".$vname."</font>的图片路径错误2,请检查是否有效  ".$spanstr;
				return false;
			}
		}
		$imgStream = getRemoteContent($picUrl);
		$createStreamFileFlag = createTextFile($imgStream,$filePath);
		if ($createStreamFileFlag) {
			$streamLen = strlen($imgStream);
			if ($streamLen < 2048) {
				echo "数据<font color=red>".$vname."</font>的图片下载发生错误5,请检查是否有效  ".$spanstr;
				return false;
			} else {
				echo "数据<font color=red>".$vname."</font>的图片下载成功,大小为<font color=red>".number_format($streamLen/1024,2)."</font>KB <a target=_blank href=../".$filePath.">预览图片</a>  ".$spanstr;
				return true;
			}
		} else {
			if (empty($vid)) {
				echo "数据<font color=red>".$vname."</font>的图片下载发生错误3,请检查是否有效  ".$spanstr;
				return false;
			} else {
				echo "数据<font color=red>".$vname."</font>的图片下载发生错误4,id为<font color=red>".$vid."</font>,请检查是否有效  ".$spanstr;
				return false;
			}
		}
	} else {
		echo "数据<font color=red>".$vname."</font>的图片下载发生错误6,图片格式不对  ".$spanstr;
		return false;
	}
}

function gatherIntoLibTransfer($data, $str, $typeset) {
	$str1 = $data;$str2 = $str;
	$transtr = '';
	if ($str1 != '' && $str2 != ''){
		switch ($typeset) {
			case 0: //智能覆盖
				$str1Array = explode("$$$",$str1);$str2Array = explode("$$$",$str2);$m = count($str1Array);$n = count($str2Array);
				for ($k = 0;$k < $n;$k++) {
					$str2fromarray = explode("$$", $str2Array[$k]);
					$x = findIsExistFrom($str1Array, $str2fromarray[0]);
					if (is_numeric($x)) $str1Array[$x] = $str2Array[$k]; else $transtr .= $str2Array[$k]."$$$";
				}
				for ($j = 0;$j < $m;$j++) {
					$transtr .= $str1Array[$j]."$$$";
				}
				$transtr = rtrim($transtr,"$$$");
			break;
			case 1: //追加为第一个来源
				$transtr = $str."$$$".$data;
			break;
			case 2: //追加为最后一个来源
				$transtr = $data."$$$".$str;
			break;
			case 3: //完全覆盖
				$transtr = $str;
			break;
		}
	} else if ($str1!='' && empty($str2)){
		$transtr=$str1;
	} else if (empty($str1) && $str2!=''){
		$transtr = $str2;
	}
	return $transtr;
}

function findIsExistFrom($array1, $from) {
	$str1 = $array1;$str2 = $from;
	$m = count($str1);
	for ($i = 0;$i < $m;$i++) {
		$array1fromarray = explode("$$",$str1[$i]);
		if (trim($array1fromarray[0]) == trim($str2)) return $i;
	}
	return "";
}

function GetEditor($fname, $fvalue) {
	require 'fckeditor/fckeditor.php';
    $oFCKeditor = new FCKeditor($fname);
    $oFCKeditor->Value = $fvalue;
    $oFCKeditor->Create();
}

function makeTypeOptionSelected($topId, $separateStr, $span="", $compareValue) {
	$tlist = getTypeLists();
	if ($topId != 0){$span .= $separateStr;} else {$span = "";}
	foreach ($tlist as $row) {
		if ($row['m_upid'] == $topId) {
			if ($row['m_id'] == $compareValue){$selectedStr = " selected";} else {$selectedStr = "";}
			echo "<option value='".$row['m_id']."'".$selectedStr.">".$span."&nbsp;|—".$row['m_name']."</option>";
			makeTypeOptionSelected($row['m_id'], $separateStr, $span, $compareValue);
		}
	}
}

function makenewsTypeOptionSelected($topId, $separateStr, $span="", $compareValue="") {
	$tlist = getNewsTypeLists();
	if ($topId != 0){$span .= $separateStr;} else {$span = "";}
	foreach ($tlist as $row) {
		if ($row['m_upid'] == $topId) {
			if ($row['m_id'] == $compareValue){$selectedStr = " selected";} else {$selectedStr = "";}
			echo "<option value='".$row['m_id']."'".$selectedStr.">".$span."&nbsp;|—".$row['m_name']."</option>";
			makeTypeOptionSelected($row['m_id'], $separateStr, $span, $compareValue);
		}
	}
}

function fPageCount($TotalResult, $numPerPage, $Page) {
    $NaviLength = '10' ;
	$showMorePageGo_Type_ = 1;
    $int_showNumberLink_ = '十';
    $nonLinkColor_ = "#999999"; 
    $toF_ = "<font face=webdings title='首页'>9</font>" ;
    $toP10_ = " <font face=webdings title='上十页'>7</font>";
    $toP1_ = " <font face=webdings title='上一页'>3</font>";
    $toN1_ = " <font face=webdings title='下一页'>4</font>";
    $toN10_ = " <font face=webdings title='下十页'>8</font>"; 
    $toL_ = "<font face=webdings title='最后一页'>:</font>"; 
    $page_index = "";
	
    $tpagecount = ceil($TotalResult / $numPerPage);
	$nowCoolPage = ceil($Page/$NaviLength);
    $toPage_ = $_SERVER['QUERY_STRING'];
    if ($toPage_ == '' ) {
       $toPage_ = "?page=";
    } else {
       $toPage_ = preg_replace("/&?page=\d+/", '', $toPage_);
       $toPage_ = "?".$toPage_."&page=";
    }
	if ($nowCoolPage == 1) {
        $page_index .= "<font color=".$nonLinkColor_." title='首页'>".$toF_."</font> ";
        $page_index .= "<font color=".$nonLinkColor_." title='上".$int_showNumberLink_."页'>".$toP10_."</font> ";
    } else {
        $preRow =  $Page-$NaviLength;
		$page_index .= "<a href=".$toPage_."1 title='首页'>".$toF_."</a> ";
		$page_index .= "<a href=".$toPage_.$preRow." title='上".$int_showNumberLink_."页'>".$toP10_."</a> ";
    } 
    $upRow   = $Page - 1;
    $downRow = $Page + 1;
    if ($upRow > 0) {
       $page_index .= "<a href=".$toPage_.$upRow." title='上一页'>".$toP1_."</a> ";
    } else {
       $page_index .= "<font color=".$nonLinkColor_." title='上一页'>".$toP1_."</font> ";
    } 
     
	for ($i = 1;$i <= $NaviLength;$i++) {
         $nowPage = ($nowCoolPage-1)*$NaviLength+$i;
         if ($nowPage != $Page) {
             if ($nowPage <= $tpagecount) {
                 $page_index .= "  <a href=".$toPage_.$nowPage.">" .$nowPage. "</a> ";
             } else {
                 break;
             }
         } else {
             if ($tpagecount != 1) {
                $page_index .= "<b>".$nowPage."</b>";
             }
         }
      }
     if ($downRow <= $tpagecount) {
         $page_index .= "<a href=".$toPage_.$downRow." title='下一页'>".$toN1_."</a> ";
     } else {
         $page_index .= "<font color=".$nonLinkColor_." title='下一页'>".$toN1_."</font> ";
     }
     if ($Page >= $tpagecount) {
         $page_index .= " <font color=".$nonLinkColor_." title='下".$int_showNumberLink_."页'>".$toN10_."</font> ";
         $page_index .= "<font color=".$nonLinkColor_." title='尾页'>".$toL_."</font>";
     } else {
         $nextRow = $Page+$NaviLength;
         $theEndRow = $tpagecount;
         $page_index .= " <a href=".$toPage_.$nextRow." title='下".$int_showNumberLink_."页'>".$toN10_."</a> ";
         $page_index .= "<a href=".$toPage_.$theEndRow." title='尾页'>".$toL_."</a>";
     }

    if ($showMorePageGo_Type_ == 1) {
        $Show_Page_i = $Page + 1 ;
    if ($Show_Page_i > $tpagecount) $Show_Page_i = 1; 
	    $page_index .= "&nbsp;&nbsp;<input type=\"text\" size=\"4\" maxlength=\"10\" name=\"Func_Input_Page\" onmouseover=\"this.focus();\" onfocus=\"this.value='$Show_Page_i';\" onKeyUp=\"value=value.replace(/[^0-9]/g,'')\" onbeforepaste=\"clipboardData.setData('text',clipboardData.getData('text').replace(/[^0-9]/g,''))\">&nbsp;&nbsp;<input type=\"button\" value=\"Go\" class=\"btn\" onmouseover=\"Func_Input_Page.focus();\" onclick=\"javascript:var Js_JumpValue;Js_JumpValue=document.all.Func_Input_Page.value;if(Js_JumpValue=='' || !isNaN(Js_JumpValue)) location='$toPage_'+Js_JumpValue; else location='".$toPage_."1';\">\n";
    } else {
        $page_index .= " 跳转:<select name='select' size='1' style='font-size: 12px' onchange='javascript:window.location.href=this.options[this.selectedIndex].value'>"; 
        for ($i = 1;$i <= $tpagecount;$i++) {
	       if ($Page == $i) {
	           $selected = " selected";
	       } else {
	           $selected = "";
           }
           $page_index .= " <option value=".$toPage_.$i; 
           $page_index .= $selected." style='color:#FF0000'"; 
           $page_index .= ">第".$i."页</option>";
       }
       $page_index .= "</select>";

   }
        $page_index .= "  每页<b>".$numPerPage."</b>个记录，现在是:<b><span class='tx'>".$Page."</span>/".$tpagecount."</b>页，共<b><span id='recordcount'>".$TotalResult."</span></b>个记录。"; 
    return $page_index; 
}
?>