rockqueueChajian.php 4.7 KB

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