123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- <?php
- class WordPushCommand extends CConsoleCommand {
- public function init() {
- parent::init();
-
- @ini_set('memory_limit', '1024M');
- set_time_limit(0);
- }
-
- public function actionIndex($school_id='',$YII_ENV='development') {
- $pdoParam = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES "utf8";');
- // 连接业务库
- $school_dsn = 'mysql:host='.Yii::app()->params["default_server"]['addr'].';dbname='.Yii::app()->params["default_db"]['name'].';';
- $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";'));
- $school_str = '';
- if ($school_id) {
- $school_str = $school_id;
- } else {
- // 获取所有可用的学校
- $school_obj = $school_dbh->query('SELECT school_id FROM `school` WHERE `status`=0');
- $school_info= $school_obj->fetchAll(PDO::FETCH_ASSOC);
- if (empty($school_info)) {
- exit('未找到可用的学校!');
- }
-
- $school_array = array();
- foreach ($school_info as $school) {
- array_push($school_array, $school['school_id']);
- }
-
- $school_str = implode(',', $school_array);
- }
- if(!$school_str){
- exit('未找到学校数据库链接信息!');
- }
- // 找出学校的链接信息
- $data_obj = $school_dbh->query('SELECT * FROM `database` WHERE `school_id` IN ('.$school_str.')');
- if(!$data_obj){
- exit('未找到学校数据库链接信息!');
- }
- $data_info= $data_obj->fetchAll(PDO::FETCH_ASSOC);
- if (empty($data_info)) {
- exit('未找到学校数据库链接信息!');
- }
-
- // 关闭链接
- $school_dbh = null;
- $my_path = dirname(dirname(dirname(__FILE__)));
- $callback_url = Yii::app()->params['server_callback_url'];
- $coach_server_callback_url = Yii::app()->params['coach_server_callback_url'];
- $mathml_type = Yii::app()->params['formula_type'];
- $xbpaper_callback_url = Yii::app()->params['xbpaper_callback_url']; //校本卷库word回调地址
- $request_url = Yii::app()->params['word_analysis_url'];
- $_callback_url = '';
-
- // echo $mathml_type;die;
- // 发送Word处理
- foreach ($data_info as $data) {
- echo 'STARTING==>'.$data['school_id']. "\n";
-
- $temp_dsn = 'mysql:host='.$data['database_host'].';dbname='.$data['database_name'].';';
- $temp_dbh = new PDO($temp_dsn,$data['database_user'],$data['database_password'],array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES "utf8";'));
-
- $temp_obj = $temp_dbh->query('SELECT * FROM `topic_word` WHERE `exam_group_id`!="" and `status`=0');
- $temp_info= $temp_obj->fetchAll(PDO::FETCH_ASSOC);
- if (empty($temp_info)) {
- echo 'not found any word in school_'.$data['school_id']. "\n";
- } else {
- foreach ($temp_info as $info) {
- if(empty($info['exam_group_id']))
- {
- continue;
- }
- if(false !== strpos($info['word_path'],'http://'))
- {
- $time = time();
- $arr = self::getFile($info['word_path'],'/tem',$info['uploader_id'].$time.'.docx');
- if($arr)
- {
- if (file_exists($arr['save_path']))
- {
- $_callback_url = $callback_url;
- if(!empty($info['exam_group_id']))
- {
- $_callback_url = $coach_server_callback_url;
- }
- //校本卷库
- if($info['type_id']==1){
- $_callback_url = $xbpaper_callback_url;
- }
- $retRes = self::curlWord($request_url,$_callback_url,$info['word_id'],$arr['save_path'],$data['school_id']);
- $json_array = json_decode($retRes,true);
- $temp_dbh = new PDO($temp_dsn,$data['database_user'],$data['database_password'],$pdoParam);
- if (!$json_array || $json_array['errcode'] > 0) {
- $temp_dbh->exec("UPDATE `topic_word` SET `status` = '3',`wrong_reason` = '".$json_array['errmsg']."',`content` ='".$retRes."' WHERE `word_id` ='".$info['word_id']."'");
- } else {
- $temp_dbh->exec("UPDATE `topic_word` SET `status` = '1',`file_queue`='".$json_array['docsbefore']."' WHERE `word_id` ='".$info['word_id']."'");
- }
- $temp_dbh = null;
- }
- }
- @unlink($arr['save_path']);
- }else {
- if (file_exists($my_path . '/' . $info['word_path'])) {
- echo 'Send the word path : ' . $info['word_path'] . "\n";
- $_callback_url = $callback_url;
- if (!empty($info['exam_group_id'])) {
- $_callback_url = $coach_server_callback_url;
- }
- $retRes = self::curlWord($request_url, $_callback_url, $info['word_id'], $my_path . '/' . $info['word_path'], $data['school_id'], $mathml_type);
- $json_array = json_decode($retRes, true);
- if (!$json_array || $json_array['errcode'] > 0) {
- $temp_dbh->exec("UPDATE `topic_word` SET `status` = '3',`wrong_reason` = '" . $json_array['errmsg'] . "',`content` ='" . $retRes . "' WHERE `word_id` ='" . $info['word_id'] . "'");
- } else {
- $temp_dbh->exec("UPDATE `topic_word` SET `status` = '1',`file_queue`='" . $json_array['docsbefore'] . "' WHERE `word_id` ='" . $info['word_id'] . "'");
- }
- } else {
- echo 'lost the word path : ' . $my_path . '/' . $info['word_path'] . "\n";
- }
- }
- }
- }
-
- echo 'ENDING==>'.$data['school_id']. "\n\n";
- }
- }
- // POST word
- public function curlWord($request_url,$word_api_url,$word_id,$postFilePath,$school_id){
- /*if (substr(PHP_VERSION, 0, 3) >= '5.5') {
- $cfile = curl_file_create($postFilePath,'application/vnd.openxmlformats-officedocument.wordprocessingml.document','wordname'); // try adding
- if ($formulaType == 1) {
- $imgdata = array('callback_url'=>$word_api_url.'/wordapi/index/sid/'.$school_id.'/wid/'.$word_id,'file' => $cfile,'type'=>'batch','format'=>'mathml');
- } else {
- $imgdata = array('callback_url'=>$word_api_url.'/wordapi/index/sid/'.$school_id.'/wid/'.$word_id,'file' => $cfile,'type'=>'batch');
- }
- } else {
- if ($formulaType == 1) {
- $imgdata = array('callback_url'=>$word_api_url.'/wordapi/index/sid/'.$school_id.'/wid/'.$word_id,'file' => '@'.$postFilePath,'type'=>'batch','format'=>'mathml');
- } else {
- $imgdata = array('callback_url'=>$word_api_url.'/wordapi/index/sid/'.$school_id.'/wid/'.$word_id,'file' => '@'.$postFilePath,'type'=>'batch');
- }
- }*/
- if (substr(PHP_VERSION, 0, 3) >= '5.5') {
- $pathName = pathinfo($postFilePath);
- $cfile = curl_file_create($postFilePath,'application/vnd.openxmlformats-officedocument.wordprocessingml.document',$pathName['basename']);
- $imgdata = array('callback_url'=>$word_api_url.'/sid/'.$school_id.'/wid/'.$word_id.'/flag/1','sid'=>$school_id,'flag'=>1,'mydata' => $cfile);
- } else {
- $imgdata = array('callback_url'=>$word_api_url.'/sid/'.$school_id.'/wid/'.$word_id.'/flag/1','sid'=>$school_id,'flag'=>1,'mydata' => '@'.$postFilePath);
- }
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $request_url);
- curl_setopt($curl, CURLOPT_USERAGENT,'Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15');
- 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'));
- //curl_setopt($ch, CURLOPT_HEADER, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_POST, true);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $imgdata);
- curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
- $r = curl_exec($curl);
- curl_close($curl);
- return $r;
- }
- protected function getFile($url, $save_dir = '', $filename = '', $type = 0) {
- $file_type = strtolower(substr($url ,strrpos($url ,'.')+1));
- //获取远程文件所采用的方法
- if ($type) {
- $ch = curl_init();
- $timeout = 5;
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
- $content = curl_exec($ch);
- curl_close($ch);
- } else {
- ob_start();
- readfile($url);
- $content = ob_get_contents();
- ob_end_clean();
- }
- //echo $content;
- $size = strlen($content);
- $test_dir = tempnam($save_dir, $filename);
- //文件大小
- $fp2 = @fopen($test_dir, 'w');
- fwrite($fp2, $content);
- fclose($fp2);
- unset($content, $url);
- $path = explode('.', $test_dir);
- @rename($test_dir,$path[0].'.'.$file_type);
- return array(
- 'file_name' => $filename,
- 'save_path' => $path[0].'.'.$file_type,
- 'file_size' => $size
- );
- }
- }
|