WordPushCommand.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. class WordPushCommand extends CConsoleCommand {
  3. public function init() {
  4. parent::init();
  5. @ini_set('memory_limit', '1024M');
  6. set_time_limit(0);
  7. }
  8. public function actionIndex($school_id='',$YII_ENV='development') {
  9. $pdoParam = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES "utf8";');
  10. // 连接业务库
  11. $school_dsn = 'mysql:host='.Yii::app()->params["default_server"]['addr'].';dbname='.Yii::app()->params["default_db"]['name'].';';
  12. $school_dbh = new PDO($school_dsn,Yii::app()->params["default_server"]['username'],Yii::app()->params["default_server"]['password'],array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES "utf8";'));
  13. $school_str = '';
  14. if ($school_id) {
  15. $school_str = $school_id;
  16. } else {
  17. // 获取所有可用的学校
  18. $school_obj = $school_dbh->query('SELECT school_id FROM `school` WHERE `status`=0');
  19. $school_info= $school_obj->fetchAll(PDO::FETCH_ASSOC);
  20. if (empty($school_info)) {
  21. exit('未找到可用的学校!');
  22. }
  23. $school_array = array();
  24. foreach ($school_info as $school) {
  25. array_push($school_array, $school['school_id']);
  26. }
  27. $school_str = implode(',', $school_array);
  28. }
  29. if(!$school_str){
  30. exit('未找到学校数据库链接信息!');
  31. }
  32. // 找出学校的链接信息
  33. $data_obj = $school_dbh->query('SELECT * FROM `database` WHERE `school_id` IN ('.$school_str.')');
  34. if(!$data_obj){
  35. exit('未找到学校数据库链接信息!');
  36. }
  37. $data_info= $data_obj->fetchAll(PDO::FETCH_ASSOC);
  38. if (empty($data_info)) {
  39. exit('未找到学校数据库链接信息!');
  40. }
  41. // 关闭链接
  42. $school_dbh = null;
  43. $my_path = dirname(dirname(dirname(__FILE__)));
  44. $callback_url = Yii::app()->params['server_callback_url'];
  45. $coach_server_callback_url = Yii::app()->params['coach_server_callback_url'];
  46. $mathml_type = Yii::app()->params['formula_type'];
  47. $xbpaper_callback_url = Yii::app()->params['xbpaper_callback_url']; //校本卷库word回调地址
  48. $request_url = Yii::app()->params['word_analysis_url'];
  49. $_callback_url = '';
  50. // echo $mathml_type;die;
  51. // 发送Word处理
  52. foreach ($data_info as $data) {
  53. echo 'STARTING==>'.$data['school_id']. "\n";
  54. $temp_dsn = 'mysql:host='.$data['database_host'].';dbname='.$data['database_name'].';';
  55. $temp_dbh = new PDO($temp_dsn,$data['database_user'],$data['database_password'],array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES "utf8";'));
  56. $temp_obj = $temp_dbh->query('SELECT * FROM `topic_word` WHERE `exam_group_id`!="" and `status`=0');
  57. $temp_info= $temp_obj->fetchAll(PDO::FETCH_ASSOC);
  58. if (empty($temp_info)) {
  59. echo 'not found any word in school_'.$data['school_id']. "\n";
  60. } else {
  61. foreach ($temp_info as $info) {
  62. if(empty($info['exam_group_id']))
  63. {
  64. continue;
  65. }
  66. if(false !== strpos($info['word_path'],'http://'))
  67. {
  68. $time = time();
  69. $arr = self::getFile($info['word_path'],'/tem',$info['uploader_id'].$time.'.docx');
  70. if($arr)
  71. {
  72. if (file_exists($arr['save_path']))
  73. {
  74. $_callback_url = $callback_url;
  75. if(!empty($info['exam_group_id']))
  76. {
  77. $_callback_url = $coach_server_callback_url;
  78. }
  79. //校本卷库
  80. if($info['type_id']==1){
  81. $_callback_url = $xbpaper_callback_url;
  82. }
  83. $retRes = self::curlWord($request_url,$_callback_url,$info['word_id'],$arr['save_path'],$data['school_id']);
  84. $json_array = json_decode($retRes,true);
  85. $temp_dbh = new PDO($temp_dsn,$data['database_user'],$data['database_password'],$pdoParam);
  86. if (!$json_array || $json_array['errcode'] > 0) {
  87. $temp_dbh->exec("UPDATE `topic_word` SET `status` = '3',`wrong_reason` = '".$json_array['errmsg']."',`content` ='".$retRes."' WHERE `word_id` ='".$info['word_id']."'");
  88. } else {
  89. $temp_dbh->exec("UPDATE `topic_word` SET `status` = '1',`file_queue`='".$json_array['docsbefore']."' WHERE `word_id` ='".$info['word_id']."'");
  90. }
  91. $temp_dbh = null;
  92. }
  93. }
  94. @unlink($arr['save_path']);
  95. }else {
  96. if (file_exists($my_path . '/' . $info['word_path'])) {
  97. echo 'Send the word path : ' . $info['word_path'] . "\n";
  98. $_callback_url = $callback_url;
  99. if (!empty($info['exam_group_id'])) {
  100. $_callback_url = $coach_server_callback_url;
  101. }
  102. $retRes = self::curlWord($request_url, $_callback_url, $info['word_id'], $my_path . '/' . $info['word_path'], $data['school_id'], $mathml_type);
  103. $json_array = json_decode($retRes, true);
  104. if (!$json_array || $json_array['errcode'] > 0) {
  105. $temp_dbh->exec("UPDATE `topic_word` SET `status` = '3',`wrong_reason` = '" . $json_array['errmsg'] . "',`content` ='" . $retRes . "' WHERE `word_id` ='" . $info['word_id'] . "'");
  106. } else {
  107. $temp_dbh->exec("UPDATE `topic_word` SET `status` = '1',`file_queue`='" . $json_array['docsbefore'] . "' WHERE `word_id` ='" . $info['word_id'] . "'");
  108. }
  109. } else {
  110. echo 'lost the word path : ' . $my_path . '/' . $info['word_path'] . "\n";
  111. }
  112. }
  113. }
  114. }
  115. echo 'ENDING==>'.$data['school_id']. "\n\n";
  116. }
  117. }
  118. // POST word
  119. public function curlWord($request_url,$word_api_url,$word_id,$postFilePath,$school_id){
  120. /*if (substr(PHP_VERSION, 0, 3) >= '5.5') {
  121. $cfile = curl_file_create($postFilePath,'application/vnd.openxmlformats-officedocument.wordprocessingml.document','wordname'); // try adding
  122. if ($formulaType == 1) {
  123. $imgdata = array('callback_url'=>$word_api_url.'/wordapi/index/sid/'.$school_id.'/wid/'.$word_id,'file' => $cfile,'type'=>'batch','format'=>'mathml');
  124. } else {
  125. $imgdata = array('callback_url'=>$word_api_url.'/wordapi/index/sid/'.$school_id.'/wid/'.$word_id,'file' => $cfile,'type'=>'batch');
  126. }
  127. } else {
  128. if ($formulaType == 1) {
  129. $imgdata = array('callback_url'=>$word_api_url.'/wordapi/index/sid/'.$school_id.'/wid/'.$word_id,'file' => '@'.$postFilePath,'type'=>'batch','format'=>'mathml');
  130. } else {
  131. $imgdata = array('callback_url'=>$word_api_url.'/wordapi/index/sid/'.$school_id.'/wid/'.$word_id,'file' => '@'.$postFilePath,'type'=>'batch');
  132. }
  133. }*/
  134. if (substr(PHP_VERSION, 0, 3) >= '5.5') {
  135. $pathName = pathinfo($postFilePath);
  136. $cfile = curl_file_create($postFilePath,'application/vnd.openxmlformats-officedocument.wordprocessingml.document',$pathName['basename']);
  137. $imgdata = array('callback_url'=>$word_api_url.'/sid/'.$school_id.'/wid/'.$word_id.'/flag/1','sid'=>$school_id,'flag'=>1,'mydata' => $cfile);
  138. } else {
  139. $imgdata = array('callback_url'=>$word_api_url.'/sid/'.$school_id.'/wid/'.$word_id.'/flag/1','sid'=>$school_id,'flag'=>1,'mydata' => '@'.$postFilePath);
  140. }
  141. $curl = curl_init();
  142. curl_setopt($curl, CURLOPT_URL, $request_url);
  143. curl_setopt($curl, CURLOPT_USERAGENT,'Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15');
  144. curl_setopt($curl, CURLOPT_HTTPHEADER,array('User-Agent: Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15','Referer: http://someaddress.tld','Content-Type: multipart/form-data'));
  145. //curl_setopt($ch, CURLOPT_HEADER, false);
  146. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  147. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  148. curl_setopt($curl, CURLOPT_POST, true);
  149. curl_setopt($curl, CURLOPT_POSTFIELDS, $imgdata);
  150. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  151. $r = curl_exec($curl);
  152. curl_close($curl);
  153. return $r;
  154. }
  155. protected function getFile($url, $save_dir = '', $filename = '', $type = 0) {
  156. $file_type = strtolower(substr($url ,strrpos($url ,'.')+1));
  157. //获取远程文件所采用的方法
  158. if ($type) {
  159. $ch = curl_init();
  160. $timeout = 5;
  161. curl_setopt($ch, CURLOPT_URL, $url);
  162. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  163. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  164. $content = curl_exec($ch);
  165. curl_close($ch);
  166. } else {
  167. ob_start();
  168. readfile($url);
  169. $content = ob_get_contents();
  170. ob_end_clean();
  171. }
  172. //echo $content;
  173. $size = strlen($content);
  174. $test_dir = tempnam($save_dir, $filename);
  175. //文件大小
  176. $fp2 = @fopen($test_dir, 'w');
  177. fwrite($fp2, $content);
  178. fclose($fp2);
  179. unset($content, $url);
  180. $path = explode('.', $test_dir);
  181. @rename($test_dir,$path[0].'.'.$file_type);
  182. return array(
  183. 'file_name' => $filename,
  184. 'save_path' => $path[0].'.'.$file_type,
  185. 'file_size' => $size
  186. );
  187. }
  188. }