123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- <?php
- class BotController extends Controller
- {
- // 首页
- public function actionIndex(){
-
- $machineId=''; //机器ID
- $modelName=''; //型号名
-
- $total=0; //总计
- $copierCount=0; //复印机数量
- $printerCount=0;//打印机数量
-
- $params=array(); //上传参数
-
- $result['success']=0;
-
- $ip=isset($_GET['ip'])?$_GET['ip']:'http://192.168.1.19';
- if($_FILES){
- foreach($_FILES as $key =>$file){
- $content=file_get_contents($file['tmp_name']);
- if($key=='machine'){
- $machine=$this->getMachine($content);
- if(!$machine){
- $result['message']='获取机器码失败';
- exit(json_encode($result));
- }else{
- if(!$machine['machineCode']){
- //保存失败文件
- $log_file='protected/runtime/log_'.$this->schoolId.'_machine_'.date('Y_m_j_H').'.txt';
- move_uploaded_file($file["tmp_name"], $log_file);
- }
- $params=array_merge($params,$machine);
- }
- }elseif($key=='count'){
- $count=$this->getCount($content);
- if(!$count){
- $result['message']='获取打印量复印量失败';
- exit(json_encode($result));
- }else{
- $params=array_merge($params,$count);
- }
- }
-
-
- }
- if($params){
- $url = 'equip-print/coach/upload';
- $http = http($url, 'post', $this->authUsername, $params);
- exit($http);
- // $response = formatResponse($http);
- //上传数据
- // $rs=$this->pushParams($params);
- // if($rs){
- // $result['success']=1;
- // $result['message']='success';
- // }
- exit(json_encode($response));
- }
- }else{
- //$params['remark']=$ip.' 网页内容获取失败';
- $result['message']='网页内容获取失败';
- exit(json_encode($result));
- }
- }
-
- //上传参数
- function pushParams($params,$method='post'){
- $url = 'equip-print/coach/upload';
- $http = http($url, $method, $this->authUsername, $params);
- $response = formatResponse($http);
- if($response['status']){
- return true;
- }else{
- return false;
- }
- }
-
- //获取机器型号
- function getMachine($content){
- if(!$content){
- return false;
- }
- $modelName='';
- $machineId='';
- //读取型号,机器ID
- $preg_str='#<tr class="staticProp">(.*?)</tr>#isu';
-
- preg_match_all($preg_str,$content,$status);
- if(isset($status[1]) && !empty($status[1])){
- foreach($status[1] as $val){
- if(strpos($val,'型号名')!==false){
-
- preg_match_all ('/<td.*?>(.+?)<\/td>/im', $val, $matches);
- if(isset($matches[1]) && !empty($matches[1])){
- if(isset($matches[1][3]) && !empty($matches[1][3])){
- $modelName= $matches[1][3];
- }
- }
-
- }
- if(strpos($val,'机器ID')!==false){
-
- preg_match_all ('/<td.*?>(.+?)<\/td>/im', $val, $matches);
- if(isset($matches[1]) && !empty($matches[1])){
- if(isset($matches[1][3]) && !empty($matches[1][3])){
- $machineId= $matches[1][3];
- }
- }
- }
- }
- }
- return array(
- 'machineCode'=>$machineId
- );
- }
- //获取计数
- function getCount($content){
- if(!$content) return false;
- $total=0;
- $copierCount=0;
- $printerCount=0;
-
- $preg_str='#<tr class="staticProp">(.*?)</tr>#isu';
-
- preg_match_all($preg_str,$content,$count);
-
-
- if(isset($count[0]) && !empty($count[0])){
-
- //读取总计
- if(isset($count[0][1]) && !empty($count[0][1])){
- preg_match_all ('/<td.*?>(.+?)<\/td>/im', $count[0][1], $matches);
- if(isset($matches[1][3]) && !empty($matches[1][3])){
- $total= $matches[1][3];
- }
-
- }
- //读取复印量
- if(isset($count[0][2]) && !empty($count[0][2])){
- preg_match_all ('/<td.*?>(.+?)<\/td>/im', $count[0][2], $matches);
- if(isset($matches[1][3]) && !empty($matches[1][3])){
- $copierCount= $matches[1][3];
- }
-
- }
- //读取打印量
- if(isset($count[0][3]) && !empty($count[0][3])){
- preg_match_all ('/<td.*?>(.+?)<\/td>/im', $count[0][3], $matches);
- if(isset($matches[1][3]) && !empty($matches[1][3])){
- $printerCount= $matches[1][3];
- }
-
- }
- }
- return array(
- 'print'=>$total,
- 'copyNum'=>$copierCount,
- 'printNum'=>$printerCount
- );
- }
- //获取机器状态
- function getStatus($content){
-
- /*获取状态部分代码*/
- $preg_str='#<li class="listboxli listBodder">(.*?)</li>#isu';
- preg_match_all($preg_str,$content,$status);
- $getStatusArr=array();
- if(isset($status[1]) && !empty($status)){
- $preg_err_key='#<dt class="listboxdtl">(.*?)</dt>#isu';
- $preg_err_value='#<dd class="listboxddl">(.*?)</dd>#isu';
- $preg_list_key='#<dt class="listboxdtm">(.*?)</dt>#isu';
- $preg_list_value='#<dd class="listboxddm">(.*?)</dd>#isu';
- foreach ($status[1] as $val){
- preg_match_all($preg_err_key,$val,$keyArr);
- preg_match_all($preg_err_value,$val,$valueArr);
- if(isset($keyArr[1][0]) && isset($valueArr[1][0])){
- $getStatusArr[]=array(
- 'key'=>strip_tags($keyArr[1][0]),
- 'value'=>$valueArr[1][0]
- );
- }
- preg_match_all($preg_list_key,$val,$keyArr);
- preg_match_all($preg_list_value,$val,$valueArr);
- if(isset($keyArr[1][0]) && isset($valueArr[1][0])){
- $getStatusArr[]=array(
- 'key'=>strip_tags($keyArr[1][0]),
- 'value'=>$valueArr[1][0]
- );
- }
- }
- }
- }
-
- //读取框架跳转地址链接
- function getPageUrl(){
- $ip=isset($_GET['ip'])?$_GET['ip']:'http://192.168.1.19';
-
- //改为直接获取文件内容
-
- //读取IP内容
- $result=$this->http($ip);
- //判断是否为跳转页面
- preg_match_all("/<meta http-equiv=\"refresh\" content=\"1; URL=(.*?)\">/u",$result,$res);
- if(isset($res[1]) && isset($res[1][0]) && $res[1][0]){
- //跳转页面
- $gotoUrl=$ip.$res[1][0];
- //拆分Url
- $urlArr=explode('/cn/',$gotoUrl);
- $location = $urlArr[0]."/cn/websys/webArch/getStatus.cgi"; //打印机状态
- $location = $urlArr[0]."/cn/websys/status/configuration.cgi"; //打印机设备信息
- }
- return $location;
- }
-
- function http($url, $type='GET')
- {
- $response = array();
- $type = strtoupper($type);
- if($type=='GET'){
- $curl = curl_init();
- if($curl){
- curl_setopt_array($curl, array(
- CURLOPT_URL => $url,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_ENCODING => "",
- CURLOPT_MAXREDIRS => 10,
- CURLOPT_TIMEOUT => 10,
- CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
- ));
- $response = curl_exec($curl);
- curl_close($curl);
- }
- }elseif($type=="PUT"){
- $curl = @curl_init();
- if ($curl) {
- curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
- curl_setopt($curl, CURLOPT_HEADER, 0);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
- curl_setopt($curl, CURLOPT_TIMEOUT, 10);
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
- // curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- curl_setopt($curl, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json',
- // 'Content-Length: '. strlen($data),
- ));
- $response = curl_exec($curl);
- curl_close($curl);
- }
- }elseif ($type == 'DELETE'){
- $curl = @curl_init();
- if ($curl) {
- curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
- curl_setopt($curl, CURLOPT_HEADER, 0);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
- curl_setopt($curl, CURLOPT_TIMEOUT, 10);
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
- curl_setopt($curl, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json',
- ));
- $response = curl_exec($curl);
- curl_close($curl);
- }
- }
- elseif($type=="FORM"){
- $curl = @curl_init();
- if ($curl) {
- curl_setopt_array($curl, array(
- CURLOPT_URL => $url,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_ENCODING => "",
- CURLOPT_MAXREDIRS => 10,
- CURLOPT_TIMEOUT => 10,
- CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
- CURLOPT_CUSTOMREQUEST => "POST",
- CURLOPT_HTTPHEADER => array(
- "Cache-Control: no-cache",
- "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
- ),
- ));
- $response = curl_exec($curl);
- curl_close($curl);
- }
- }else{
- $curl = @curl_init();
- if ($curl) {
- if(empty($params)){
- $data = '{}';
- }else{
- $data = json_encode($params);
- }
- curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
- curl_setopt($curl, CURLOPT_HEADER, 0);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
- curl_setopt($curl, CURLOPT_TIMEOUT, 10);
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- curl_setopt($curl, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json',
- 'Content-Length: '. strlen($data),
- ));
- $response = curl_exec($curl);
- curl_close($curl);
- }
- }
- return $response;
- }
- }
|