data['rootUrl'] = Doo::conf()->APP_URL; include ('./protected/controller/BaseController.php'); $this->base = new BaseController (); $userinfo = $this->base->admin_init(); $this->data['uid'] = $userinfo['id']; $this->data['username'] = $userinfo['username']; $this->__hosts = new Hosts(); $this->__servers = new Servers(); $this->__upgradelogs = new UpgradeLogs(); $this->data['currpage'] = 'server'; } public function hostList(){ //获取服务器 $serverarr = $this->__servers->getAllServer(); $sql = $pagesql = ''; if(isset($_GET['op']) && is_numeric($_GET['op'])){ $sql = 'serverid='.$_GET['op']; $pagesql = '/'.$_GET['op']; $this->data['op'] = $_GET['op']; }elseif(isset($this->params['op']) && is_numeric($this->params['op'])){ $sql = 'serverid='.$this->params['op']; $pagesql = '/'.$this->params['op']; $this->data['op'] = $this->params['op']; }else{ $sql = ' 1'; } $totalArchive = $this->__hosts->count(array('where' => $sql, 'asArray' => TRUE)); $pager = new DooPager(Doo::conf()->APP_URL . "hostlist".$pagesql."/page", $totalArchive, 15, 15); if (isset($this->params['pindex'])) $pager->paginate(intval($this->params['pindex'])); else $pager->paginate(1); if ($pager->limit != ' -15,15') $hostarr = $this->__hosts->getServerHosts($sql,$pager->limit); if(isset($hostarr) && !empty($hostarr)){ foreach($hostarr as $k => $v){ $servermsg = $this->__servers->getRowbyID($v['serverid']); $hostarr[$k]['servername'] = $servermsg['name']; $hostarr[$k]['serverip'] = $servermsg['ip']; //获取软件最后一个是否有更高版本 $hostarr[$k]['condition'] = 2; $rowData = $this->__upgradelogs->getRowLast($v['id']); if(!empty($rowData)){ if($rowData['version'] != $v['softver']){ $hostarr[$k]['softver'] = $rowData['version']; $hostarr[$k]['condition'] = $rowData['scondition']; } } } } $this->data['server'] = $serverarr; $this->data['hosts'] = isset($hostarr) ? $hostarr : ''; $this->data['pager'] = $pager->output; $this->render ( "server-list", $this->data, TRUE); } public function editHosts(){ if(isset($this->params['id']) && is_numeric($this->params['id'])){ $hostmsg = $this->__hosts->getRowbyID($this->params['id']); if(!empty($hostmsg)){ $result = $this->updateWebAndStatus($hostmsg['id'],$hostmsg['domain'],$hostmsg['webver']); $hostmsg['webver'] = $result['version']; //获取更高版本软件版本号 $rowData = $this->__upgradelogs->getRowbyhidcond($hostmsg['id'],1); if(!empty($rowData)){ $hostmsg['highsoftver'] = $rowData['version']; }else{ $hostmsg['highsoftver'] = ''; } $this->data['host'] = $hostmsg; //获取是否关闭了客户端登录 if($result['onoff'] != 2){ $this->data['onoff'] = $result['onoff']; }else{ $this->data['errormsg'] = '通讯异常,无法获取客户端是否禁止状态'; } if(isset($_POST['hostname'])){ //获取云端版本 $data = array('url' => 'http://'.$_POST['domain'].'/api/upgrade/v1/get/web/version'); $datamsg = $this->_httpClient($data); if($datamsg){ $this->__hosts->id = $this->params['id']; $this->__hosts->serverid = $_POST['server']; $this->__hosts->hostname = $_POST['hostname']; $this->__hosts->domain = $_POST['domain']; $this->__hosts->sales = $_POST['sales']; $this->__hosts->status = 1; $this->__hosts->updated_at = date('Y-m-d H:i:s', time()); $this->__hosts->update(); return '/hostlist'; }else{ $this->data['errormsg'] = '该域名不存在版本信息,无法修改'; } } }else{ $this->data['errormsg'] = '不存在该客户机'; } } $serverarr = $this->__servers->getAllServer(); $this->data['server'] = $serverarr; $this->render ( "edit-server", $this->data, TRUE); } public function addHosts(){ if(isset($_POST['hostname'])){ //获取云端版本 $data = array('url' => 'http://'.$_POST['domain'].'/api/upgrade/v1/get/web/version'); $datamsg = $this->_httpClient($data); if($datamsg){ $this->__hosts->serverid = $_POST['server']; $this->__hosts->hostname = $_POST['hostname']; $this->__hosts->domain = $_POST['domain']; $this->__hosts->sales = $_POST['sales']; $this->__hosts->status = 1; $this->__hosts->webver = json_decode($datamsg)->version; $this->__hosts->softver = $_POST['swversion']; $this->__hosts->created_at = date('Y-m-d H:i:s', time()); $this->__hosts->updated_at = date('Y-m-d H:i:s', time()); $this->__hosts->insert(); return '/hostlist'; }else{ $this->data['errormsg'] = '该域名不存在版本信息'; } } $serverarr = $this->__servers->getAllServer(); $this->data['server'] = $serverarr; $this->render ( "add-server", $this->data, TRUE); } public function updateWebVerStatus(){ $hostmsg = $this->__hosts->getRowbyID($_POST['id']); if(!empty($hostmsg)){ $result = $this->updateWebAndStatus($_POST['id'],$_POST['domain'],$_POST['version']); exit(json_encode($result)); }else{ exit(json_encode(array('msg' => 404))); } } //升级 public function upgradeinfoHosts(){ $hostmsg = $this->__hosts->getRowbyID($_POST['domainid']); $data = array( 'condition' => $_POST['scondition'], 'version' => $_POST['sversion'], 'download' => $_POST['sdownload'], 'desc' => $_POST['sdesc'] ); $rowData = $this->__upgradelogs->getRowbyhidcond($_POST['domainid'],$_POST['scondition']); if(empty($rowData)){ $this->__upgradelogs->hid = $_POST['domainid']; $this->__upgradelogs->scondition = $_POST['scondition']; $this->__upgradelogs->version = $_POST['sversion']; $this->__upgradelogs->jsondata = json_encode($data); $this->__upgradelogs->created_at = date('Y-m-d H:i:s', time()); $this->__upgradelogs->updated_at = date('Y-m-d H:i:s', time()); $this->__upgradelogs->insert(); }else{ $this->__upgradelogs->id = $rowData['id']; $this->__upgradelogs->version = $_POST['sversion']; $this->__upgradelogs->jsondata = json_encode($data); $this->__upgradelogs->updated_at = date('Y-m-d H:i:s', time()); $this->__upgradelogs->update(); } if($_POST['scondition'] == 2){ $this->__hosts->id = $_POST['domainid']; $this->__hosts->softver = $_POST['sversion']; $this->__hosts->updated_at = date('Y-m-d H:i:s', time()); $this->__hosts->update(); $result = $this->_httpClient($data2 = array('url'=> 'http://'.$hostmsg['domain'].'/api/upgrade/v1/set/upgrade/info', 'msg' => array('json' => json_encode($data))), 'post'); if($result && json_decode($result)->onoff == 1) { exit(json_encode(array('onoff' => 1, 'softver' => $_POST['sversion'], 'condition' => $_POST['scondition']))); }else{ exit(json_encode(array('onoff' => 0))); } } exit(json_encode(array('onoff' => 1, 'softver' => $_POST['sversion'], 'condition' => $_POST['scondition']))); // exit(json_encode(array('onoff' => 0))); } //更新web版本和通讯状态 function updateWebAndStatus($id,$domain,$oldwebver){ //获取云端版本 $data = array('url' => 'http://'.$domain.'/api/upgrade/v1/get/version/status'); $datamsg = $this->_httpClient($data); if($datamsg){ $change = 0; if(json_decode($datamsg)->version != $oldwebver){ $this->__hosts->id = $id; $this->__hosts->webver = json_decode($datamsg)->version; $this->__hosts->status = 1; $this->__hosts->updated_at = date('Y-m-d H:i:s', time()); $this->__hosts->update(); $change = 1; } return array('version' => json_decode($datamsg)->version, 'onoff' => json_decode($datamsg)->onoff, 'change' => $change); }else{ $this->__hosts->id = $id; $this->__hosts->webver = ''; $this->__hosts->status = 2; $this->__hosts->updated_at = date('Y-m-d H:i:s', time()); $this->__hosts->update(); return array('version' => '', 'onoff' => 2, 'change' => 2); } } /** * POST或者GET方式访问计量支付接口 * @param string $data * @return mixed */ private function _httpClient($data, $type = 'get') { try { $ch = curl_init(); // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded','charset=utf-8')); curl_setopt($ch, CURLOPT_URL,$data['url']); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, 0); if($type == 'post'){ curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data['msg']); } curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $res = curl_exec($ch); curl_close($ch); return $res; } catch (Exception $e) { return false; } } }