<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');


class Onlineoffice extends CI_Controller
{
    public $uid;
	public $admin_data;
    public function __construct() 
    {
        parent::__construct();
		//$this->output->enable_profiler(TRUE);
		$this->permission->is_admin(true);
        $this->load->model('Navigation_model');
        $this->load->model('Operator_model');
        $this->admin_data=$this->session->userdata('admin_chtcdn');
        $this->uid=$this->admin_data['OPI_SN'];
    }

    public function index() 
    {
        $data['mid']=$this->uid;
        $this->load->view('n-header', $data);
		$user_data=$this->Operator_model->get_webhtuser_by_id($this->admin_data['whu_uid']);
		$data['whu_ip']=$user_data->whu_ip;
		$data['whu_mac']=$user_data->whu_mac;
		$this->load->view('onlineoffice/wakeup',$data);
        $this->load->view('n-footer');
    }

	
	public function wakeonlan($ip,$mac,$port=7){	
	//需要在system32目录下放置wolcmd.exe来启动
		$mac=str_replace(':','',$mac);
		$last_line = system("wolcmd $mac $ip 255.255.255.0 7", $retval);
		echo $last_line;
	
	return true;
	
		$addr_byte = explode(':', $mac);
		$hw_addr = '';
		for ($i=0; $i <6; $i++){
			$hw_addr .= chr(hexdec($addr_byte[$i]));
		}
		$msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
		for ($i = 1; $i <= 16; $i++) {
			$msg .= $hw_addr;
}
		$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
		if ($socket == false) {
			echo "create socket failed!\n";
			echo "error：'".socket_last_error($socket)."' - " . socket_strerror(socket_last_error($socket));
			return FALSE;
		} else {
			$opt_ret = socket_set_option($socket,SOL_SOCKET, SO_BROADCAST, TRUE);
			if($opt_ret <0) {
				echo "setsockopt() failed, error: " . strerror($opt_ret) . "\n";
				return FALSE;
			}
			
			if(socket_sendto($socket, $msg, strlen($msg), 0, $ip, $port)) {
				echo "唤醒数据包发送成功!";
				socket_close($socket);
				return TRUE;
			} else {
				echo "唤醒数据包发送失败!";
				return FALSE;
			}
		}
	}
	
	//检查端口是否打开，用来判断电脑是否启动成功
	public function ping($ip,$port=3389) {  
			$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
			socket_set_nonblock($sock);
			@socket_connect($sock,$ip, $port);
			socket_set_block($sock);
			//返回值说明:2关闭,1打开,0超时
			switch(socket_select($r = array($sock), $w = array($sock), $f = array($sock), 5)){
				case 0:
					echo '努力启动中...';
					break;
				case 1:
					echo '电脑启动成功！';
					break;
				case 2:
					echo '电脑启动成功，远程端口未打开，联系YCC开启权限';
					break;
			}
		 }

   
}