Page 1 of 1

รบกวนสอบถามเรื่อง Json กับ PHP Script ครับ

Posted: 15/02/2013 10:01 pm
by itsmenow
รบกวนอธิบาย code ชุดนี้ให้ทีครับ

function json()
{
$output = array("aaData" => array());

$count = 1;
foreach(get_dhcplist() as $data)
{

$jdata = array();

if($data['status']=="pass") { $status = 'Online'; $color = "#00BF00"; } else if($data['status']=="dnat") { $status = 'Offline'; $color = "#FFC000"; } else if($data['status']=="drop") { $status = 'Block'; $color = "#FF6666"; } else { $status = 'Release..'; $color = "#00C8FF"; }
if($data['ip']=="0.0.0.0" || $data['status']=="drop") { $ipaddress = $data['ip']; $auth = ' '; } else { $ipaddress = $data['ip']; $auth = anchor('admin/client/action/authorize/'.$data['sessionid'],img(ASSETS.'images/unplug.png'),'title="Bypass" class="action"'); }

$jdata[] .= $count++;
$jdata[] .= $data['username'];
$jdata[] .= $ipaddress;
$jdata[] .= $data['mac'];
$jdata[] .= $data['bandwidth_down'];
$jdata[] .= $data['bandwidth_up'];
$jdata[] .= $data['idletimeout'];
$jdata[] .= '<div id="status" style="background:'.$color.';color:#545454;">'.$status.'</div>';
$jdata[] .= (($status=='Block') ? ' ' : anchor('admin/client/action/block/'.$data['sessionid'],img(ASSETS.'images/backlist.png'),'title="Block" class="action"')).' '
.(($data['status']=="pass") ? anchor('admin/client/action/disconnect/'.$data['sessionid'],img(ASSETS.'images/plug.png'),'title="Disconnect" class="action"') : $auth).' '
.anchor('admin/client/action/release/'.$data['sessionid'], img(ASSETS.'images/rotate_c.gif'), 'title="Release" class="action"');

$output['aaData'][] = $jdata;

}

$this->output->enable_profiler(FALSE);

print json_encode($output);

}

มันเป็น Authen คร่าวมันทำงานแบบนี้ DHCP Server Assigned = Authorized คือ LAN Connection นะครับ

แต่ถ้า WAN Connection มันจะเปิด Function Hidden Form ทำให้ซ่อน FORM Login เอาไว้ครับ

แก้ยังไงให้มันรับ Authen ได้จากทุก IP ครับ คือ 0.0.0.0

ช่วยอธิบายให้ละเอียดได้ไหมครับอยากแก้ไปใช้ Wan Authen ครับ


เพิ่มเติมอันนี้เป็น Function get_dhcp

function get_dhcplist()
{
$CI =& get_instance();
$strFileName = set_realpath('').'tmp/dhcp';

$file = exec('/usr/bin/sudo '.$CI->config->item('chilli_query').' list > '.$strFileName.' 2>&1');

$data = array();

$objFopen = fopen($strFileName, 'r');
if ($objFopen)
{
$ic = 0;
while (!feof($objFopen))
{
$ic++;
$str = fgets($objFopen, 4096);
if($str!='') $data[] = explode(' ', $str);
}
fclose($objFopen);
}

$output = array();

if(isset($data))
{
foreach($data AS $value)
{
$d_name = array();

$d_name['mac'] = $value[0];
$d_name['ip'] = $value[1];
$d_name['status'] = $value[2];
$d_name['sessionid'] = $value[3];
$d_name['username'] = $value[5];
$d_name['timeusage'] = $value[6];
$d_name['idletimeout'] = $value[7];
$d_name['download'] = $value[8];
$d_name['upload'] = $value[9];
$d_name['maxpacket'] = $value[10];
$d_name['bandwidth_up'] = substr($value[12],0,strpos($value[12],'/'));
$d_name['bandwidth_down'] = substr($value[13],0,strpos($value[13],'/'));

$output[] = $d_name;
}
}

return $output;

}

function operation($opt, $target, $user = 'bypass', $bwup = '256000', $bwdown = '512000', $acct = 'noacct')
{
$CI =& get_instance();
$command = $CI->config->item('rootcommand').' ' .$CI->config->item('chilli_query');

switch($opt)
{
//ปล่อยผ่าน
case 'authorize' :
$result = exec($command. ' authorize sessionid '.$target.' maxbwup '.$bwup.' maxbwdown '.$bwdown.' username '.$user.' interiminterval 60 '.$acct.' 2>&1');
break;
//ตัดการเชื่อมต่อ
case 'disconnect' :
$result = exec($command.' logout '.$target.' 2>&1');
break;
//บล๊อคแมค
case 'block' :
$result = exec($command.' block '.$target.' 2>&1');
break;
//ให้รับ ไอพีใหม่
case 'release' :
$result = exec($command. ' dhcp-release '.$target.' 2>&1');
break;
default: $result = 'Error';
}

if($result=="") return TRUE;
return FALSE;
}



อันนี้เป็น file ที่เขียนจาก function get_dhcp จาก Array

$d_name['mac'] = $value[0];
$d_name['ip'] = $value[1];
$d_name['status'] = $value[2];
$d_name['sessionid'] = $value[3];
$d_name['username'] = $value[5];
$d_name['timeusage'] = $value[6];
$d_name['idletimeout'] = $value[7];
$d_name['download'] = $value[8];
$d_name['upload'] = $value[9];
$d_name['maxpacket'] = $value[10];
$d_name['bandwidth_up'] = substr($value[12],0,strpos($value[12],'/'));
$d_name['bandwidth_down'] = substr($value[13],0,strpos($value[13],'/'));


00-1B-63-B9-D1-48 172.1.1.151 dnat 511e3d2400000001 0 - 0/0 0/0 0/0 0/0 0 0 0/0 0/0 -