<?php

/**
 *      [Dzz!] (C)2001-2099 dzz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: function_dzz_core.php 1000 2011-5-31 15:41:43Z angel rain $
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
function updatemenu_plugin() {
	global $_G,$lang;
	$html='';
	$html.='<li><a href="'.ADMINSCRIPT.'?action=plugins" hidefocus="true" target="main" class=""><em onClick="menuNewwin(this)" title="'.$lang['openNewWindow'].'"></em>'.$lang['menu_plugin'].'</a></li>';
	$pluginadmin=getpluginadmin();
	foreach($pluginadmin as $value){
				$html.='<li><a href="'.ADMINSCRIPT.'?action='.$value['action'].'" hidefocus="true" target="main" class=""><em onClick="menuNewwin(this)" title="'.$lang['openNewWindow'].'"></em>'.$value['pluginname'].'</a></li>';
	}
	echo '<script type="text/JavaScript">parent.$(\'menu_plugin\').innerHTML = \''.$html.'\';parent.initCpMenus(\'leftmenu\');parent.initCpMap();</script>';
}

function getpluginadmin(){
	global $_G;
	$return=array();
	$query=DB::query("select identifier,pluginid,name,adminfile from ".DB::table('dzz_plugin')." where `available`>0 order by disp DESC");
	while($value=DB::fetch($query)){
		$modfile='./dzz/plugin/'.$value['identifier'].'/'.($value['adminfile']?$value['adminfile']:'admin.php');
		if(file_exists(DISCUZ_ROOT.$modfile)){
			$value['action']='plugin:'.$value['identifier'];
			$value['pluginname']=$value['name'];
			$value['adminfile']=$value['adminfile']?$value['adminfile']:'admin.php';
			$return[]=$value;
		}
	}
	return $return;
}
function set_ico_position($appid,$type='app'){
	global $_G;
	if($type=='app'){
		$apparr=DB::fetch_first("select * from ".DB::table('dzz_apps')." where appid='{$appid}'");
		if($apparr['appico']!='dzz/images/default/icodefault.png' && !preg_match("/^(http|ftp|https|mms)\:\/\/(.+?)/i", $apparr['appico'])){
			$apparr['appico']=$_G['setting']['attachurl'].$apparr['appico'];
		}
		$setarr=array(
								'uid'=>-1,
								'username'=>'',
								'oid'=>$appid,
								'desktop'=>intval($apparr['default']),
								'notdelete'=>intval($apparr['notdelete']),
								'defaultopen'=>$apparr['defaultopen'],
								'name'=>$apparr['appname'],
								'img'=>$apparr['appico'],
								'url'=>$apparr['appurl'],
								'titlebuttons'=>$apparr['titlebuttons'],
								'type'=>$type,
								'friend'=>$apparr['friend'],
								'open'=>$apparr['open'],
								'wheight'=>$apparr['height'],
								'wwidth'=>$apparr['width'],
								'haveflash'=>$apparr['haveflash'],
								'dateline'=>$_G['timestamp'],
								'idtype'=>$apparr['idtype'],
								'typeid'=>$apparr['typeid'],
								'havetask'=>$apparr['havetask'],
								'isshow'=>$apparr['isshow'],
								);
		if($icoid=DB::result_first("select icoid from ".DB::table('dzz_icos')." where type='{$type}' and oid='{$appid}' and uid='-1'")){
			if($apparr['default']!=0){
				DB::update('dzz_icos',daddslashes($setarr),"icoid='{$icoid}'");
			}else{
				DB::delete('dzz_icos',"icoid='{$icoid}'");	
			}
		}else{
			if($apparr['default']!=0){
				$setarr['dateline']=$_G['timestamp'];
				DB::insert('dzz_icos',daddslashes($setarr));
			}
		}
	}elseif($type=='widget'){
		$wid=$appid;
		$widget=DB::fetch_first("select * from ".DB::table('dzz_widget_market')." where wid='{$wid}'");
		if($widget['icon']!='' && $widget['icon']!='dzz/images/default/icodefault.png' && !preg_match("/^(http|ftp|https|mms)\:\/\/(.+?)/i", $widget['icon'])){
			$widget['icon']=$_G['setting']['attachurl'].$widget['icon'];
		}
		$setarr=array(
						'uid'=>-1,
						'username'=>'',
						'oid'=>$wid,
						'notdelete'=>intval($widget['notdelete']),
						'url'=>$widget['url'],
						'type'=>$widget['type'],
						'open'=>$widget['open'],
						'height'=>$widget['height'],
						'width'=>$widget['width'],
						'idtype'=>$widget['idtype'],
						'typeid'=>$widget['typeid'],
						'href'=>$widget['href'],
						'classname'=>$widget['classname'],
						'desktop'=>$widget['default']
					);
		if($gid=DB::result_first("select gid from ".DB::table('dzz_widget')." where oid='{$wid}' and uid='-1'")){
			if($widget['default']!=0){
				DB::update('dzz_widget',daddslashes($setarr),"gid='{$gid}'");
			}else{
				DB::delete('dzz_widget',"gid='{$gid}'");	
			}
		}else{
			if($widget['default']!=0){
				$setarr['dateline']=$_G['timestamp'];
				DB::insert('dzz_widget',daddslashes($setarr));
			}
		}
	}
}
function get_nav_option($default,$notdefault=0,$type='app'){
	global $_G,$lang;
	if($_G['setting']['dzz_sysconfig']){
	$sysconfig=dstripslashes(unserialize(stripslashes($_G['setting']['dzz_sysconfig'])));
	}else{
		$sysconfig=DB::fetch_first("select * from ".DB::table('dzz_config')." where id='1'");
	}
	$return='';
	if(!$notdefault){
		if($default==0) $return.='<option value="0" selected="selected">'.$lang['app_not_default'].'</option>';
		else $return.='<option value="0">'.$lang['app_not_default'].'</option>';
	}
	if($type=='app'){
		if($default==-1) $return.='<option value="-1" selected="selected">'.$lang['dock'].'</option>';
		else $return.='<option value="-1">'.$lang['dock'].'</option>';
	}
	for($i=1;$i<=$sysconfig['userscreennum'];$i++){
			if($default==$i) $return.='<option value="'.$i.'" selected="selected">'.$lang['desktop'].$i.'</option>';
			else  $return.='<option value="'.$i.'">'.$lang['desktop'].$i.'</option>';
	}
	return $return; 
}
function dzz_shownav($header = '', $menu = '', $nav = '') {
	global $action, $operation,$lang;

	$title = 'cplog_'.$action.($operation ? '_'.$operation : '');
	
	$title = ($header ? $header : '').($menu ? '&nbsp;&raquo;&nbsp;'.$menu : '').($nav ? '&nbsp;&raquo;&nbsp;'.$nav : '');
	
	//$addtomenu = "&nbsp;&nbsp;<a target=\"main\" title=\"".cplang('custommenu_addto')."\" href=\"".ADMINSCRIPT."?action=misc&operation=custommenu&do=add&title=".rawurlencode($ctitle)."&url=".rawurlencode(cpurl())."\">[+]</a>";
	$dtitle = $lang['dzz_admin_title'].' - '.str_replace('&nbsp;&raquo;&nbsp;', ' - ', $title);
	echo '<script type="text/JavaScript">parent.document.title = \''.$dtitle.'\';if(parent.$(\'admincpnav\')) parent.$(\'admincpnav\').innerHTML=\''.$title.'\';</script>';
}
function dzz_siteinformation() {
	global $_G,$dsiteuniqueid;
	$db = DB::object();
	if($_G['setting']['dzz_sysconfig']){
		$sysconfig=dstripslashes(unserialize(stripslashes($_G['setting']['dzz_sysconfig'])));
	}else{
		$sysconfig=DB::fetch_first("select * from ".DB::table('dzz_config')." where id='1'");
	}
	$update = array(
				'uniqueid' => $dsiteuniqueid,
				'version' => DZZ_VERSION,
				'release' => DZZ_RELEASE,
				'php' => PHP_VERSION,
				'mysql' => $db->version(),
				'charset' => CHARSET,
				'bbname' =>  diconv($sysconfig['sitename'],CHARSET,'UTF-8'),
				'software' => $_SERVER['SERVER_SOFTWARE'],
				'domain' => $_G['siteurl'],
			);
	/*$updatetime = @filemtime(DISCUZ_ROOT.'./data/dzzupdatetime.lock');
	if(empty($updatetime) || (TIMESTAMP - $updatetime > 3600 * 4)) {
		@touch(DISCUZ_ROOT.'./data/updatetime.lock');
	}*/
	$data = '';
	foreach($update as $key => $value) {
		$data .= $key.'='.rawurlencode($value).'&';
	}
	return 'os=dzzdesktop&update='.rawurlencode(base64_encode($data)).'&md5hash='.substr(md5($_SERVER['HTTP_USER_AGENT'].implode('', $update).TIMESTAMP), 8, 8).'&time='.TIMESTAMP;
}
function dzz_cpheader() {
	global $_G;

	if(!defined('DISCUZ_CP_HEADER_OUTPUT')) {
		define('DISCUZ_CP_HEADER_OUTPUT', true);
	} else {
		return true;
	}
	$IMGDIR = $_G['style']['imgdir'];
	$STYLEID = $_G['setting']['styleid'];
	$VERHASH = $_G['style']['verhash'];
	$frame = getgpc('frame') != 'no' ? 1 : 0;
	$charset = CHARSET;
	$basescript = ADMINSCRIPT;
echo <<<EOT

<!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=$charset">
<meta http-equiv="x-ua-compatible" content="ie=7" />
<link href="dzz/admin/default/admincp/admincp.css?{$_G[style][verhash]}" rel="stylesheet" type="text/css" />
</head>
<body>
<script type="text/JavaScript">var admincpfilename = '$basescript', IMGDIR = '$IMGDIR', STYLEID = '$STYLEID', VERHASH = '$VERHASH', IN_ADMINCP = true, ISFRAME = $frame, STATICURL='static/', SITEURL = '$_G[siteurl]', JSPATH = '{$_G[setting][jspath]}';</script> <script src="dzz/scripts/jquery-1.7.min.js" type="text/javascript"></script><script src="static/js/common.js?{$_G[style][verhash]}" type="text/javascript"></script><script src="static/js/admincp.js?{$_G[style][verhash]}" type="text/javascript"></script>
<script type="text/javascript">
	if(ISFRAME && !parent.document.getElementById('leftmenu')) {
		redirect(admincpfilename + '?frames=yes&' + document.URL.substr(document.URL.indexOf(admincpfilename) + strlen(admincpfilename)+1));
	}
	</script>
	<div id="append_parent"></div><div id="ajaxwaitid"></div>
	<div class="container" id="cpcontainer">
EOT;
}
function dzzfooter() {
	global $_G,$lang;
	if(defined('FOOTERDISABLED')) {
		exit;
	}
	require_once DISCUZ_ROOT.'./dzz/dzz_version.php';
	$version = DZZ_VERSION;
	$charset = CHARSET;
	echo "\n</div>";
	if($_G['adminid'] == 1 && $_GET['action']== 'index') {
		$release = DZZ_RELEASE;
		$newsurl =  'ht'.'tp:/'.'/d'.'z'.'z.ma'.'wang'.'ye'.'.co'.'m/dz'.'z'.'news.p'.'hp?'.dzz_siteinformation();
		echo <<<EOT
        <script type="text/javascript">
            var newhtml = '';
            newhtml += '<table class="tb tb2"><tr><th class="partition edited">$lang[version_update_info]</th></tr>';
            newhtml += '<tr><td class="tipsblock"><a href="http://dzz.cc" target="_blank"><img src="{$newsurl}" onload="shownews()" /></a></td></tr></table>';
            document.getElementById('boardnews').style.display = 'none';
            document.getElementById('boardnews').innerHTML = newhtml;
            function shownews() {
                document.getElementById('boardnews').style.display = '';
            }
        </script>
EOT;
	}
	echo "\n</body>\n</html>";
}
?>