rockqueueChajian.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * 信呼中node队列的处理
  4. */
  5. class rockqueueChajian extends Chajian
  6. {
  7. //队列服务器主机
  8. private $rockqueue_host = '127.0.0.1';
  9. //队列服务端口,数字类型,为0从服务器设置上读取
  10. private $rockqueue_port = 0;
  11. //初始化配置读取
  12. protected function initChajian()
  13. {
  14. $this->rockqueue_host = getconfig('rockqueue_host', $this->rockqueue_host);
  15. $this->rockqueue_port = getconfig('rockqueue_port', $this->rockqueue_port);
  16. if($this->rockqueue_port==0){
  17. $reim = m('reim');
  18. $reimhot = $reim->getpushhostport($reim->serverpushurl);
  19. $this->rockqueue_host = $reimhot['host'];
  20. $this->rockqueue_port = $reimhot['port'];
  21. }
  22. $this->cmdshell = array(
  23. array('soffice.exe','php.exe'), //win下必须包含
  24. array('libreoffice'), //Linux下包含
  25. array('pdf:writer_pdf_Export') //命令里至少要有一个
  26. );
  27. }
  28. /**
  29. * 发送队列信息
  30. * $cont 内容可以是http地址,也可以如:cli,run
  31. * $param 参数
  32. * 使用 c('rockqueue')->push('cli,run');
  33. */
  34. public function push($cont, $param=array(), $runtime=0, $id=0)
  35. {
  36. $type = 'cmd';
  37. $url = $cont;
  38. $queuelogid = 0;
  39. if(!isset($param['nolog'])){
  40. $queuelogid= m('log')->addlogs('异步队列','', 3);
  41. $param['queuelogid'] = $queuelogid;
  42. }
  43. if(substr($cont,0,4)=='http'){
  44. $type='url';
  45. }else{
  46. if(!contain($url, ','))$url='cli,'.$url.'';
  47. $phppath = getconfig('phppath');
  48. if(!contain($this->rockqueue_host, '127.0.0.1') || isempt($phppath)){
  49. $urla= explode(',', $url);
  50. $url = URL.'task.php?m='.$urla[0].'|runt&a='.$urla[1].'';
  51. $type= 'url';
  52. }else{
  53. $st1 = '';
  54. $check = c('check');
  55. foreach($param as $k=>$v)$st1.=' -'.$k.'='.$v.'';
  56. if(contain($phppath,' ') || $check->isincn($phppath))
  57. return returnerror('配置文件phppath不能有空格,请加入环境变量设置并为php');
  58. if(contain(ROOT_PATH,' ') || $check->isincn(ROOT_PATH))
  59. return returnerror('OA系统目录“'.ROOT_PATH.'”有空格,无法使用');
  60. $url = ''.$phppath.' '.ROOT_PATH.'/task.php '.$url.''.$st1.'';
  61. }
  62. }
  63. if($type=='url'){
  64. $jg = contain($url,'?')?'&':'?';
  65. $st1 = '';
  66. foreach($param as $k=>$v)$st1.='&'.$k.'='.$v.'';
  67. if($st1!='')$url.=''.$jg.''.substr($st1,1).'';
  68. }
  69. if($id==0)$id = rand(1,99999);
  70. $rarr[] = array(
  71. 'qtype' => $type,
  72. 'runtime' => $runtime,
  73. 'url' => $url,
  74. 'id' => $id
  75. );
  76. $barr = $this->pushdata($rarr);
  77. $barr['cmdurl'] = ''.$type.':'.$url;
  78. if($runtime==0)$runtime = time();
  79. if($queuelogid>0){
  80. m('log')->update(array(
  81. 'url' => $url,
  82. 'remark'=> '['.$type.']'.date('Y-m-d H:i:s', $runtime).'',
  83. ),$queuelogid);
  84. }
  85. return $barr;
  86. }
  87. /**
  88. * 执行shell命令
  89. */
  90. public function pushcmd($cmd)
  91. {
  92. if(contain(PHP_OS,'WIN')){
  93. $cmdshell = $this->cmdshell[0];
  94. }else{
  95. $cmdshell = $this->cmdshell[1];
  96. }
  97. $qianz = explode(' ', $cmd);
  98. $qianz = $qianz[0];
  99. //$boa = false;
  100. //foreach($cmdshell as $sell)if(contain($qianz, $sell))$boa = true;
  101. //if(!$boa)return returnerror('非法操作');
  102. $boa = false;
  103. foreach($this->cmdshell[2] as $sell)if(contain($cmd, $sell))$boa = true;
  104. if(!$boa)return returnerror('无效参数');
  105. $id = rand(1,99999);
  106. $rarr[] = array(
  107. 'qtype' => 'cmd',
  108. 'runtime' => '0',
  109. 'url' => escapeshellcmd($cmd),
  110. 'id' => $id
  111. );
  112. return $this->pushdata($rarr);
  113. }
  114. /**
  115. * 推送数据过去
  116. */
  117. public function pushdata($rarr)
  118. {
  119. if(is_array($rarr))$rarr = json_encode($rarr);
  120. $url = 'http://'.$this->rockqueue_host.':'.$this->rockqueue_port.'/?atype=send&data='.urlencode($rarr).'';
  121. $reqult = c('curl')->setTimeout(3)->getcurl($url);
  122. if($reqult){
  123. return returnsuccess($reqult);
  124. }else{
  125. return returnerror('服务端配置不能用');
  126. }
  127. //return c('socket')->udppush($rarr, $this->rockqueue_host, $this->rockqueue_port);
  128. }
  129. /**
  130. * 推送类型
  131. */
  132. public function pushtype($type, $url, $can=array())
  133. {
  134. $can['qtype'] = $type;
  135. $can['url'] = $url;
  136. $rarr[] = $can;
  137. return $this->pushdata($rarr);
  138. }
  139. /**
  140. * 发送腾讯云存储
  141. * 调用:c('rockqueue')->sendfile(文件Id);
  142. */
  143. public function sendfile($fileid, $runtime=0)
  144. {
  145. return $this->push('qcloudCos,run', array('fileid'=>$fileid), $runtime);
  146. }
  147. public function senddown($fileid)
  148. {
  149. return $this->push('qcloudCos,down', array('fileid'=>$fileid));
  150. }
  151. /**
  152. * 在信呼文件管理平台上删除对应文件
  153. * 调用:c('rockqueue')->delfile(文件编号);
  154. */
  155. public function delfile($fileid)
  156. {
  157. return $this->push('qcloudCos,del', array('fileid'=>$fileid));
  158. }
  159. }
粤ICP备19079148号