<?php

####################################################
# Author: Ivan　谭俊青　
# email:drinching@gmail.com
# QQ:55300231
# msn:showsa@live.cn
# copyright: GPL v3.0
# date: 2008-01-15
####################################################

error_reporting(E_ERROR);

#####################################################
# 配置
#####################################################
$config = array(
	'host' 		=> '192.168.7.250',
	'username' 	=> 'search',
	'password' 	=> 'xxxxxxxxxxx',
	'dbname' 	=> 'test',
	'port' 		=> 3306
);
#####################################################




header( 'Content-type:text/plain; charset=utf-8' );
header( 'Expires: Mon, 08 Aug 2005 08:00:00 GMT' ); // Date in the past
header( 'Cache-Control: no-cache, no-store, must-revalidate, private, max-age=0', false );         // HTTP/1.1
header( 'Pragma: no-cache' );


$__node = mysql_escape_string(trim($_REQUEST['node']));

$master_ip = mysql_escape_string(trim($_REQUEST['ip']));
$master_port = mysql_escape_string(trim($_REQUEST['port']));




$config['host'] = $master_ip;
$config['port'] = $master_port;

$config['dbname'] = ''; # 有些可能不存在test库，因此留空
		
$master_mysqli = new mysqli(
	$config['host'],
	$config['username'], 
	$config['password'],
	$config['dbname'],
	$config['port']
);

/* check connection */
if (mysqli_connect_errno())
{	
	echo "{ success: false, errors: { reason: ' Connect failed: error no:" . mysqli_connect_errno() . ' error: ' . addslashes( mysqli_connect_error() ) . ' on ' . $config['host'] . ':' . $config['port'] . ' in ' . addslashes( __FILE__ ) . ' on line ' . addslashes( __LINE__ ) . "' }}";
	exit();
}


if ($__node == '__root')
{
	# 取db列表
	$sql = 'show databases';
	$result = $master_mysqli->query($sql);
	$rows = array();
	while ($tmp_row = mysqli_fetch_assoc($result))
	{
		if ( !($tmp_row['Database'] == 'information_schema' or $tmp_row['Database'] == 'mysql') )
		{
			$rows[] = $tmp_row['Database']; 
		}
	}
		
	
	$nodes = array();
	foreach($rows as $item)
	{
		$node = array();
		$node['id'] 	= $item;
		$node['text']	= $item;
		//$node['href']	= '/asset/asset/list/f_class/' . iconv('gb2312', 'utf-8', $row['f_group_name']);
		$node['leaf']	= false;
		$node['cls']	= 'folder';
		$node['singleClickExpand'] = true;

		$nodes[] = $node;
	}
			
	echo json_encode($nodes);
}
else 
{
	# 取table列表
	$master_mysqli->query('use ' . $__node);
	
	$sql = 'show tables';
	$result = $master_mysqli->query($sql);
	$rows = array();
	while ($tmp_row = mysqli_fetch_array($result))
	{
		$rows[] = $tmp_row[0]; 
	}

	$nodes = array();
	foreach($rows as $item)
	{
		$node = array();
		$node['id'] 	= $__node . '__ivan__' .$item;
		$node['text']	= $item;
		$node['leaf']	= true;
		$node['cls']	= 'file';

		$nodes[] = $node;
	}
			
	echo json_encode($nodes);

}
