rockqueueChajian.php 4.9 KB

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