BotController.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. class BotController extends Controller
  3. {
  4. // 首页
  5. public function actionIndex(){
  6. $machineId=''; //机器ID
  7. $modelName=''; //型号名
  8. $total=0; //总计
  9. $copierCount=0; //复印机数量
  10. $printerCount=0;//打印机数量
  11. $params=array(); //上传参数
  12. $result['success']=0;
  13. $ip=isset($_GET['ip'])?$_GET['ip']:'http://192.168.1.19';
  14. if($_FILES){
  15. foreach($_FILES as $key =>$file){
  16. $content=file_get_contents($file['tmp_name']);
  17. if($key=='machine'){
  18. $machine=$this->getMachine($content);
  19. if(!$machine){
  20. $result['message']='获取机器码失败';
  21. exit(json_encode($result));
  22. }else{
  23. if(!$machine['machineCode']){
  24. //保存失败文件
  25. $log_file='protected/runtime/log_'.$this->schoolId.'_machine_'.date('Y_m_j_H').'.txt';
  26. move_uploaded_file($file["tmp_name"], $log_file);
  27. }
  28. $params=array_merge($params,$machine);
  29. }
  30. }elseif($key=='count'){
  31. $count=$this->getCount($content);
  32. if(!$count){
  33. $result['message']='获取打印量复印量失败';
  34. exit(json_encode($result));
  35. }else{
  36. $params=array_merge($params,$count);
  37. }
  38. }
  39. }
  40. if($params){
  41. $url = 'equip-print/coach/upload';
  42. $http = http($url, 'post', $this->authUsername, $params);
  43. exit($http);
  44. // $response = formatResponse($http);
  45. //上传数据
  46. // $rs=$this->pushParams($params);
  47. // if($rs){
  48. // $result['success']=1;
  49. // $result['message']='success';
  50. // }
  51. exit(json_encode($response));
  52. }
  53. }else{
  54. //$params['remark']=$ip.' 网页内容获取失败';
  55. $result['message']='网页内容获取失败';
  56. exit(json_encode($result));
  57. }
  58. }
  59. //上传参数
  60. function pushParams($params,$method='post'){
  61. $url = 'equip-print/coach/upload';
  62. $http = http($url, $method, $this->authUsername, $params);
  63. $response = formatResponse($http);
  64. if($response['status']){
  65. return true;
  66. }else{
  67. return false;
  68. }
  69. }
  70. //获取机器型号
  71. function getMachine($content){
  72. if(!$content){
  73. return false;
  74. }
  75. $modelName='';
  76. $machineId='';
  77. //读取型号,机器ID
  78. $preg_str='#<tr class="staticProp">(.*?)</tr>#isu';
  79. preg_match_all($preg_str,$content,$status);
  80. if(isset($status[1]) && !empty($status[1])){
  81. foreach($status[1] as $val){
  82. if(strpos($val,'型号名')!==false){
  83. preg_match_all ('/<td.*?>(.+?)<\/td>/im', $val, $matches);
  84. if(isset($matches[1]) && !empty($matches[1])){
  85. if(isset($matches[1][3]) && !empty($matches[1][3])){
  86. $modelName= $matches[1][3];
  87. }
  88. }
  89. }
  90. if(strpos($val,'机器ID')!==false){
  91. preg_match_all ('/<td.*?>(.+?)<\/td>/im', $val, $matches);
  92. if(isset($matches[1]) && !empty($matches[1])){
  93. if(isset($matches[1][3]) && !empty($matches[1][3])){
  94. $machineId= $matches[1][3];
  95. }
  96. }
  97. }
  98. }
  99. }
  100. return array(
  101. 'machineCode'=>$machineId
  102. );
  103. }
  104. //获取计数
  105. function getCount($content){
  106. if(!$content) return false;
  107. $total=0;
  108. $copierCount=0;
  109. $printerCount=0;
  110. $preg_str='#<tr class="staticProp">(.*?)</tr>#isu';
  111. preg_match_all($preg_str,$content,$count);
  112. if(isset($count[0]) && !empty($count[0])){
  113. //读取总计
  114. if(isset($count[0][1]) && !empty($count[0][1])){
  115. preg_match_all ('/<td.*?>(.+?)<\/td>/im', $count[0][1], $matches);
  116. if(isset($matches[1][3]) && !empty($matches[1][3])){
  117. $total= $matches[1][3];
  118. }
  119. }
  120. //读取复印量
  121. if(isset($count[0][2]) && !empty($count[0][2])){
  122. preg_match_all ('/<td.*?>(.+?)<\/td>/im', $count[0][2], $matches);
  123. if(isset($matches[1][3]) && !empty($matches[1][3])){
  124. $copierCount= $matches[1][3];
  125. }
  126. }
  127. //读取打印量
  128. if(isset($count[0][3]) && !empty($count[0][3])){
  129. preg_match_all ('/<td.*?>(.+?)<\/td>/im', $count[0][3], $matches);
  130. if(isset($matches[1][3]) && !empty($matches[1][3])){
  131. $printerCount= $matches[1][3];
  132. }
  133. }
  134. }
  135. return array(
  136. 'print'=>$total,
  137. 'copyNum'=>$copierCount,
  138. 'printNum'=>$printerCount
  139. );
  140. }
  141. //获取机器状态
  142. function getStatus($content){
  143. /*获取状态部分代码*/
  144. $preg_str='#<li class="listboxli listBodder">(.*?)</li>#isu';
  145. preg_match_all($preg_str,$content,$status);
  146. $getStatusArr=array();
  147. if(isset($status[1]) && !empty($status)){
  148. $preg_err_key='#<dt class="listboxdtl">(.*?)</dt>#isu';
  149. $preg_err_value='#<dd class="listboxddl">(.*?)</dd>#isu';
  150. $preg_list_key='#<dt class="listboxdtm">(.*?)</dt>#isu';
  151. $preg_list_value='#<dd class="listboxddm">(.*?)</dd>#isu';
  152. foreach ($status[1] as $val){
  153. preg_match_all($preg_err_key,$val,$keyArr);
  154. preg_match_all($preg_err_value,$val,$valueArr);
  155. if(isset($keyArr[1][0]) && isset($valueArr[1][0])){
  156. $getStatusArr[]=array(
  157. 'key'=>strip_tags($keyArr[1][0]),
  158. 'value'=>$valueArr[1][0]
  159. );
  160. }
  161. preg_match_all($preg_list_key,$val,$keyArr);
  162. preg_match_all($preg_list_value,$val,$valueArr);
  163. if(isset($keyArr[1][0]) && isset($valueArr[1][0])){
  164. $getStatusArr[]=array(
  165. 'key'=>strip_tags($keyArr[1][0]),
  166. 'value'=>$valueArr[1][0]
  167. );
  168. }
  169. }
  170. }
  171. }
  172. //读取框架跳转地址链接
  173. function getPageUrl(){
  174. $ip=isset($_GET['ip'])?$_GET['ip']:'http://192.168.1.19';
  175. //改为直接获取文件内容
  176. //读取IP内容
  177. $result=$this->http($ip);
  178. //判断是否为跳转页面
  179. preg_match_all("/<meta http-equiv=\"refresh\" content=\"1; URL=(.*?)\">/u",$result,$res);
  180. if(isset($res[1]) && isset($res[1][0]) && $res[1][0]){
  181. //跳转页面
  182. $gotoUrl=$ip.$res[1][0];
  183. //拆分Url
  184. $urlArr=explode('/cn/',$gotoUrl);
  185. $location = $urlArr[0]."/cn/websys/webArch/getStatus.cgi"; //打印机状态
  186. $location = $urlArr[0]."/cn/websys/status/configuration.cgi"; //打印机设备信息
  187. }
  188. return $location;
  189. }
  190. function http($url, $type='GET')
  191. {
  192. $response = array();
  193. $type = strtoupper($type);
  194. if($type=='GET'){
  195. $curl = curl_init();
  196. if($curl){
  197. curl_setopt_array($curl, array(
  198. CURLOPT_URL => $url,
  199. CURLOPT_RETURNTRANSFER => true,
  200. CURLOPT_ENCODING => "",
  201. CURLOPT_MAXREDIRS => 10,
  202. CURLOPT_TIMEOUT => 10,
  203. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  204. ));
  205. $response = curl_exec($curl);
  206. curl_close($curl);
  207. }
  208. }elseif($type=="PUT"){
  209. $curl = @curl_init();
  210. if ($curl) {
  211. curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  212. curl_setopt($curl, CURLOPT_HEADER, 0);
  213. curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
  214. curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  215. curl_setopt($curl, CURLOPT_URL, $url);
  216. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
  217. // curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  218. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  219. 'Content-Type: application/json',
  220. // 'Content-Length: '. strlen($data),
  221. ));
  222. $response = curl_exec($curl);
  223. curl_close($curl);
  224. }
  225. }elseif ($type == 'DELETE'){
  226. $curl = @curl_init();
  227. if ($curl) {
  228. curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  229. curl_setopt($curl, CURLOPT_HEADER, 0);
  230. curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
  231. curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  232. curl_setopt($curl, CURLOPT_URL, $url);
  233. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
  234. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  235. 'Content-Type: application/json',
  236. ));
  237. $response = curl_exec($curl);
  238. curl_close($curl);
  239. }
  240. }
  241. elseif($type=="FORM"){
  242. $curl = @curl_init();
  243. if ($curl) {
  244. curl_setopt_array($curl, array(
  245. CURLOPT_URL => $url,
  246. CURLOPT_RETURNTRANSFER => true,
  247. CURLOPT_ENCODING => "",
  248. CURLOPT_MAXREDIRS => 10,
  249. CURLOPT_TIMEOUT => 10,
  250. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  251. CURLOPT_CUSTOMREQUEST => "POST",
  252. CURLOPT_HTTPHEADER => array(
  253. "Cache-Control: no-cache",
  254. "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
  255. ),
  256. ));
  257. $response = curl_exec($curl);
  258. curl_close($curl);
  259. }
  260. }else{
  261. $curl = @curl_init();
  262. if ($curl) {
  263. if(empty($params)){
  264. $data = '{}';
  265. }else{
  266. $data = json_encode($params);
  267. }
  268. curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  269. curl_setopt($curl, CURLOPT_HEADER, 0);
  270. curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
  271. curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  272. curl_setopt($curl, CURLOPT_URL, $url);
  273. curl_setopt($curl, CURLOPT_POST, 1);
  274. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  275. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  276. 'Content-Type: application/json',
  277. 'Content-Length: '. strlen($data),
  278. ));
  279. $response = curl_exec($curl);
  280. curl_close($curl);
  281. }
  282. }
  283. return $response;
  284. }
  285. }