TransController.php 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  1. <?php
  2. namespace app\commands;
  3. use yii\console\Controller;
  4. use app\common\components\Unzip;
  5. use Smalot\PdfParser\Parser;
  6. use Yii;
  7. class TransController extends Controller
  8. {
  9. public $logFile;
  10. public $transferDir;
  11. public $transferUrl;
  12. public $ossConfig;
  13. public $transferConfig;
  14. public $attachmentConfig;
  15. public $imgConfig;
  16. public $oss;
  17. public function init()
  18. {
  19. parent::init();
  20. $this->logFile = Yii::getAlias('@runtime').DIRECTORY_SEPARATOR.'logs'.DIRECTORY_SEPARATOR.'cmdtransfer_'.date("Ymd").'.log';
  21. $this->transferDir = UPLOAD_PATH.'transfer'.DIRECTORY_SEPARATOR;
  22. $this->transferUrl = UPLOAD_URL.'transfer/';
  23. $ossConfigResult = Yii::$app->db->createCommand("select * from {{%config}} where name='oss'")->queryOne();
  24. $this->ossConfig = string2array($ossConfigResult['value']);
  25. $transferConfigResult = Yii::$app->db->createCommand("select * from {{%config}} where name='transfer'")->queryOne();
  26. $this->transferConfig = string2array($transferConfigResult['value']);
  27. $attachmentConfigResult = Yii::$app->db->createCommand("select * from {{%config}} where name='attachment'")->queryOne();
  28. $this->attachmentConfig = string2array($attachmentConfigResult['value']);
  29. $imgConfigResult = Yii::$app->db->createCommand("select * from {{%config}} where name='imageconfig'")->queryOne();
  30. $this->imgConfig = string2array($imgConfigResult['value']);
  31. if($this->ossConfig['OPEN_OSS'])
  32. {
  33. $this->oss = new \app\common\components\Oss();
  34. $initResult = $this->oss->init($this->ossConfig['OPEN_INTERNAL']);
  35. if($initResult['error'])
  36. {
  37. exit("OSS初始化出错");
  38. }
  39. }
  40. }
  41. public function actionIndex()
  42. {
  43. $doc = Yii::$app->db->createCommand("select * from {{%doc}} where (trans_status=0 or trans_status=99) and is_delete=0 order by id desc limit 1")->queryOne();
  44. if(!empty($doc))
  45. {
  46. if($doc['trans_status']==99&&TIMESTAMP-$doc['transfer_start_time']>=$this->transferConfig['timeout'])
  47. {
  48. Yii::$app->db->createCommand("update {{%doc}} set trans_status=88 where id=".$doc['id'])->execute();
  49. $this->_saveLog('timeout',$doc);
  50. }
  51. else
  52. {
  53. //只对待转换文档处理
  54. if($doc['trans_status']==0)$this->_dotrans($doc['id']);
  55. }
  56. }
  57. }
  58. //执行转换
  59. private function _dotrans($id)
  60. {
  61. set_time_limit(0);
  62. $status = 5;//默认进入待自审状态
  63. $audit_bak = [];//审核备注
  64. $doc = Yii::$app->db->createCommand("select * from (select * from {{%doc}} where id=$id) a left join {{%doc_data}} b on a.id=b.id")->queryOne();
  65. $this->_saveLog('start',$doc);
  66. //获取用户组权限
  67. if($doc['user_id'])
  68. {
  69. $user = Yii::$app->db->createCommand("select * from {{%user}} where user_id=".$doc['user_id']."")->queryOne();
  70. if(!empty($user))
  71. {
  72. $userGroupLevel = Yii::$app->db->createCommand("select * from {{%user_group_level}} where id=".$user['user_group_level'])->queryOne();
  73. if($userGroupLevel)
  74. {
  75. $levelSettings = string2array($userGroupLevel['settings']);
  76. //免自审
  77. if($levelSettings['doc_auto_selfaudit']==1)
  78. {
  79. $status = 0;
  80. }
  81. //免审核,直接发布
  82. if($levelSettings['doc_auto_audit']==1)
  83. {
  84. $status = 1;
  85. }
  86. }
  87. }
  88. }
  89. //合辑,资源免转换
  90. if(in_array($doc['doc_type'],[2,3,4]))
  91. {
  92. Yii::$app->db->createCommand("update {{%doc}} set trans_status=1,transfer_end_time=".time().",status=$status where id=$id")->execute();
  93. $this->_saveLog('end',$doc);
  94. }
  95. else
  96. {
  97. $downResult = $this->_locateFile(getFileUrl($doc['file']),$doc['ext']);
  98. if($downResult['error']==0)
  99. {
  100. $transferFolder = $downResult['data'][0];
  101. $transferFile = $downResult['data'][1];
  102. $relativeFolder = $downResult['data'][2];
  103. $relativeFile = $downResult['data'][3];
  104. Yii::$app->db->createCommand("update {{%doc}} set trans_status=99,transfer_start_time=".time()." where id=$id")->execute();
  105. //图片
  106. if(in_array($doc['ext'],array('jpg','jpeg','gif','png','bmp'))){
  107. $imgResult = $this->_doimg($transferFolder,$transferFile);
  108. $imgcode = $imgResult['data']['imgcode'];
  109. Yii::$app->db->createCommand("update {{%doc}} set trans_status=1,transfer_end_time=".time().",status=$status,page_num = 1,preview_num=1,thumb='".$doc['file']."' where id=".$id)->execute();
  110. Yii::$app->db->createCommand("update {{%doc_data}} set imgcode='".json_encode($imgcode)."' where id=".$id)->execute();
  111. //封面图信息
  112. if(!empty($doc['file']))
  113. {
  114. Yii::$app->db->createCommand("insert into {{%attachment}} set status=1,file_path='".$doc['file']."',hash='".md5($doc['file'])."',is_used=1,upload_time=".time().",file_name='".basename($doc['file'])."'")->execute();
  115. Yii::$app->db->createCommand("insert into {{%attachment_index}} set table_name='doc',field='thumb',data_id=".$id.",a_id=".Yii::$app->db->getLastInsertID()."")->execute();
  116. }
  117. $result = array('error'=>0,'msg'=>'处理成功');
  118. }
  119. else if(in_array($doc['ext'],array('zip','rar')))
  120. {
  121. $packfiles = $this->_uncompress($transferFile,$id);
  122. if(!empty($packfiles)&&is_array($packfiles))
  123. {
  124. Yii::$app->db->createCommand("update {{%doc_data}} set packfiles = '".addslashes(array2string($packfiles))."' where id=".$id)->execute();
  125. Yii::$app->db->createCommand("update {{%doc}} set trans_status=1,transfer_end_time=".time().",status=$status where id=".$id)->execute();
  126. $result = array('error'=>0,'msg'=>'处理成功');
  127. }
  128. else
  129. {
  130. $result = array('error'=>1,'msg'=>'解压失败');
  131. $audit_bak[] = "解压失败";
  132. }
  133. }
  134. else if(in_array($doc['ext'],array('mp3','mp4','flv','wmv')))
  135. {
  136. $mediaResult = $this->_domedia($transferFolder,$transferFile);
  137. if($mediaResult['error']==0)
  138. {
  139. Yii::$app->db->createCommand("update {{%doc}} set trans_status=1,transfer_end_time=".time().", status=$status, page_num = ".$mediaResult['data']['page_num'].", preview_num=".$mediaResult['data']['preview_num']." where id=".$id)->execute();
  140. Yii::$app->db->createCommand("update {{%doc_data}} set h5code='".json_encode($mediaResult['data']['h5code'])."' where id=".$id)->execute();
  141. $result = array('error'=>0,'msg'=>'处理成功');
  142. }
  143. else
  144. {
  145. $result = array('error'=>1,'msg'=>'音视频处理失败');
  146. $audit_bak[] = "音视频处理失败";
  147. }
  148. }
  149. else if(in_array(strtolower($doc['ext']),array('ppt','pptx','dps'))&&$this->transferConfig['trans_type']=='localkiivo'&&$this->transferConfig['pptimg'])//使用知沃云ppt转图片
  150. {
  151. $pptResult = $this->_doppt($transferFolder,$transferFile,getFileUrl($doc['file']));
  152. if($pptResult['error']==0)
  153. {
  154. $page_num = $pptResult['data']['page_num'];
  155. $imgcode = $pptResult['data']['imgcode'];
  156. $preview_num = $pptResult['data']['preview_num'];
  157. if(empty($doc['thumb'])){
  158. Yii::$app->db->createCommand("update {{%doc}} set page_num = $page_num,preview_num=$preview_num, thumb='".$imgcode[0]."', trans_status=1,transfer_end_time=".time().", status=$status where id=".$id)->execute();
  159. }
  160. else
  161. {
  162. Yii::$app->db->createCommand("update {{%doc}} set page_num = $page_num,preview_num=$preview_num, trans_status=1,transfer_end_time=".time().", status=$status where id=".$id)->execute();
  163. }
  164. Yii::$app->db->createCommand("update {{%doc_data}} set imgcode='".json_encode($imgcode)."' where id=".$id)->execute();
  165. //封面图信息
  166. if(!empty($imgcode[0])&&empty($doc['thumb']))
  167. {
  168. Yii::$app->db->createCommand("insert into {{%attachment}} set status=1,file_path='".$imgcode[0]."',hash='".md5($imgcode[0])."',is_used=1,upload_time=".time().",file_name='".basename($imgcode[0])."'")->execute();
  169. Yii::$app->db->createCommand("insert into {{%attachment_index}} set table_name='doc',field='thumb',data_id=".$id.",a_id=".Yii::$app->db->getLastInsertID()."")->execute();
  170. }
  171. $result = array('error'=>0,'msg'=>'处理成功');
  172. }
  173. else
  174. {
  175. $result = $pptResult;
  176. }
  177. }
  178. else
  179. {
  180. //只有在本地转换或本地+知沃云转换时候,需要转成PDF
  181. if($this->transferConfig['trans_type']=='local'||$this->transferConfig['trans_type']=='localkiivo')
  182. {
  183. if(in_array(strtolower($doc['ext']),array('pdf')))
  184. {
  185. $pdfFile = $transferFile;
  186. }
  187. else
  188. {
  189. $pdfFile = $this->_toPdf($transferFolder,$transferFile);
  190. }
  191. }
  192. else//云转换
  193. {
  194. $pdfFile = $transferFile;
  195. }
  196. if($pdfFile)
  197. {
  198. if($this->transferConfig['trans_type']=='local'||$this->transferConfig['trans_type']=='localkiivo')
  199. {
  200. //如果设定为转成图片
  201. if($this->transferConfig['dist_type']=='img')
  202. {
  203. $pdfPageInfo = $this->_pdfPageInfo($pdfFile);
  204. $page_num = $pdfPageInfo['data']['pageNum'];
  205. $h5code = [];
  206. $fullcontent = '';
  207. $content = '';
  208. //重新计算预览页数
  209. $transferPage = $pdfPageInfo['data']['transferPage'];
  210. $preview_num = $this->_getPreviewNum($page_num,$transferPage);
  211. $imgResult = $this->_pdf2img($pdfFile,$transferPage);
  212. }
  213. else if($this->transferConfig['dist_type']=='h5')
  214. {
  215. $pdf2htmlResult = $this->_pdf2html($pdfFile);
  216. if($pdf2htmlResult['error']==0)
  217. {
  218. $page_num = $pdf2htmlResult['data']['pageNum'];
  219. $h5code = $pdf2htmlResult['data']['h5code'];
  220. $preview_num = $pdf2htmlResult['data']['preview_num'];
  221. $fullcontent = $pdf2htmlResult['data']['fullcontent'];
  222. $content = beforeProcessHtml($pdf2htmlResult['data']['content']);
  223. $imgResult = $this->_pdf2img($pdfFile);
  224. }
  225. else
  226. {
  227. //转换html失败,尝试转图片
  228. $page_num = $pdf2htmlResult['data']['pageNum'];
  229. $h5code = [];
  230. $fullcontent = '';
  231. $content = '';
  232. //重新计算预览页数
  233. $transferPage = $pdf2htmlResult['data']['transferPage'];
  234. $preview_num = $this->_getPreviewNum($page_num,$transferPage);
  235. $imgResult = $this->_pdf2img($pdfFile,$transferPage);
  236. }
  237. }
  238. else if($this->transferConfig['dist_type']=='all')
  239. {
  240. $pdf2htmlResult = $this->_pdf2html($pdfFile);
  241. if($pdf2htmlResult['error']==0)
  242. {
  243. $page_num = $pdf2htmlResult['data']['pageNum'];
  244. $h5code = $pdf2htmlResult['data']['h5code'];
  245. $preview_num = $pdf2htmlResult['data']['preview_num'];
  246. $fullcontent = $pdf2htmlResult['data']['fullcontent'];
  247. $content = beforeProcessHtml($pdf2htmlResult['data']['content']);
  248. $transferPage = $pdf2htmlResult['data']['transferPage'];
  249. $imgResult = $this->_pdf2img($pdfFile,$transferPage);
  250. }
  251. else
  252. {
  253. //转换html失败,尝试转图片
  254. $page_num = $pdf2htmlResult['data']['pageNum'];
  255. $h5code = [];
  256. $fullcontent = '';
  257. $content = '';
  258. //重新计算预览页数
  259. $transferPage = $pdf2htmlResult['data']['transferPage'];
  260. $preview_num = $this->_getPreviewNum($page_num,$transferPage);
  261. $imgResult = $this->_pdf2img($pdfFile,$transferPage);
  262. }
  263. }
  264. }
  265. else
  266. {
  267. //云转换
  268. $imgResult = $this->_doRemoteTrans($pdfFile);
  269. $page_num = $imgResult['data']['pageNum'];
  270. $h5code = [];
  271. $fullcontent = '';
  272. $content = '';
  273. $transferPage = $imgResult['data']['transferPage'];
  274. $preview_num = $this->_getPreviewNum($page_num,$transferPage);
  275. }
  276. if($imgResult['error']==0)
  277. {
  278. //生成封面
  279. $imgcode = $imgResult['data']['imgcode'];
  280. //页数超限
  281. if($page_num>50)
  282. {
  283. $status = 6;
  284. $audit_bak[] = '页数超限(超过50页需重点审核)';
  285. }
  286. //pdf二审
  287. if($doc['ext']=='pdf'){
  288. $status = 7;
  289. $audit_bak[] = 'PDF需要二审';
  290. }
  291. if(!empty($fullcontent))
  292. {
  293. //敏感词检测
  294. $sesResult = sensitive($fullcontent);
  295. if(!empty($sesResult))
  296. {
  297. if($sesResult['log2'])
  298. {
  299. $status = 8;//非法资源
  300. $audit_bak[] = $sesResult['log2'];
  301. }
  302. if($sesResult['log1'])
  303. {
  304. $status = 8;//非法资源
  305. $audit_bak[] = $sesResult['log1'];
  306. }
  307. if($sesResult['log3'])
  308. {
  309. $status = 8;//非法资源
  310. $audit_bak[] = $sesResult['log3'];
  311. }
  312. }
  313. }
  314. if(empty($doc['thumb']))
  315. {
  316. Yii::$app->db->createCommand("update {{%doc}} set page_num = $page_num,preview_num=$preview_num, thumb='".$imgcode[0]."', trans_status=1,transfer_end_time=".time().", status=$status where id=".$id)->execute();
  317. }
  318. else
  319. {
  320. Yii::$app->db->createCommand("update {{%doc}} set page_num = $page_num,preview_num=$preview_num, trans_status=1,transfer_end_time=".time().", status=$status where id=".$id)->execute();
  321. }
  322. Yii::$app->db->createCommand("update {{%doc_data}} set h5code='".json_encode($h5code)."',imgcode='".json_encode($imgcode)."',content='".addslashes($content)."' where id=".$id)->execute();
  323. //封面图信息
  324. if(!empty($imgcode[0])&&empty($doc['thumb']))
  325. {
  326. Yii::$app->db->createCommand("insert into {{%attachment}} set status=1,file_path='".$imgcode[0]."',hash='".md5($imgcode[0])."',is_used=1,upload_time=".time().",file_name='".basename($imgcode[0])."'")->execute();
  327. Yii::$app->db->createCommand("insert into {{%attachment_index}} set table_name='doc',field='thumb',data_id=".$id.",a_id=".Yii::$app->db->getLastInsertID()."")->execute();
  328. }
  329. $result = array('error'=>0,'msg'=>'处理成功');
  330. }
  331. else
  332. {
  333. $result = $imgResult;
  334. }
  335. }
  336. else
  337. {
  338. $result = array('error'=>1,'msg'=>'文件转化成PDF失败');
  339. }
  340. }
  341. }
  342. else
  343. {
  344. $result = array('error'=>1,'msg'=>'文件下载到本地失败');
  345. }
  346. }
  347. //标记失败状态
  348. if($result['error']==1)
  349. {
  350. Yii::$app->db->createCommand("update {{%doc}} set trans_status=88 where id=$id")->execute();
  351. }
  352. else
  353. {
  354. //转换日志
  355. Yii::$app->db->createCommand("delete from {{%doc_auditlog}} where doc_id=$id")->execute();
  356. if(is_array($audit_bak))foreach($audit_bak as $abak)
  357. {
  358. Yii::$app->db->createCommand("insert into {{%doc_auditlog}} set doc_id=$id,log='".$abak."',create_time=".time()."")->execute();
  359. }
  360. $this->_saveLog('end',$doc);
  361. //免审核,直接上架
  362. if($levelSettings['doc_auto_audit']==1&&$status==1)
  363. {
  364. //此处写上架代码
  365. $this->_autoAudit($id);
  366. }
  367. }
  368. }
  369. //转化成pdf
  370. private function _toPdf($transferFolder,$transferFile)
  371. {
  372. $baseFile = basename($transferFile);
  373. $pdfFile = str_replace(".".fileext($baseFile),".pdf",$baseFile);
  374. $targetFile = $transferFolder.$pdfFile;
  375. //云转换
  376. if($this->transferConfig['trans_type']=='localkiivo'&&in_array(fileext($transferFile),['doc','docx','wps','ppt','pptx','dps']))
  377. {
  378. $fileUrl = $this->transferUrl.str_replace(DIRECTORY_SEPARATOR,'/',str_replace($this->transferDir,'',$transferFile));
  379. $data = ['file'=>$fileUrl,'appkey'=>$this->transferConfig['cloudpwd'],'action'=>'topdf'];
  380. $options['time_out'] = 300;
  381. $result = https_request($this->transferConfig['cloudurl'],$options,$data);
  382. $result = json_decode($result,true);
  383. $pdf = $result['data']['pdf'];
  384. if(!empty($pdf)&&file_put_contents($targetFile,file_get_contents($pdf))){
  385. return $targetFile;
  386. }
  387. else
  388. {
  389. return false;
  390. }
  391. }
  392. else
  393. {
  394. $command = "cd $transferFolder&&/opt/libreoffice7.1/program/soffice --headless --invisible --convert-to pdf $baseFile";
  395. if(!empty($command))
  396. {
  397. //file_put_contents($this->logFile,$command."\r\n",FILE_APPEND);
  398. passthru($command,$return);
  399. if(file_exists($targetFile)&&$return==0)
  400. {
  401. return $targetFile;
  402. }
  403. else
  404. {
  405. return false;
  406. }
  407. }
  408. else
  409. {
  410. return false;
  411. }
  412. }
  413. }
  414. //将pdf转化成h5
  415. private function _pdf2html($pdf)
  416. {
  417. $workPath = $this->_initWorkPath($pdf);
  418. $pageNum = $this->_getPdfPageNum($pdf);
  419. $pdfcontent = '';
  420. if($pageNum>=1)
  421. {
  422. if($this->transferConfig['fullpage'])
  423. {
  424. $transferPage = $pageNum;
  425. }
  426. else
  427. {
  428. if($this->transferConfig['transferpage_type']=='page')
  429. {
  430. $transferPage = $this->transferConfig['transferpage'];
  431. }
  432. else
  433. {
  434. $transferPagePercent = (float)$this->transferConfig['transferpage']/100;
  435. $transferPage = ceil($transferPagePercent*$pageNum);
  436. }
  437. }
  438. if($this->transferConfig['transfermaxpage']>0&&$transferPage>$this->transferConfig['transfermaxpage'])
  439. {
  440. $transferPage = $this->transferConfig['transfermaxpage'];
  441. }
  442. if($transferPage>$pageNum)$transferPage = $pageNum;
  443. if($transferPage==0)$transferPage = 1;
  444. $toHtmlCmd = "pdf2htmlEX -l $transferPage --split-pages 1 --dest-dir ".$workPath." --css-filename pdf.css --embed-css 0 --fit-width 800 --optimize-text 1 --process-outline 0 --use-cropbox 0 --page-filename page%d.html ".$pdf;
  445. //file_put_contents($this->logFile,$toHtmlCmd."\r\n",FILE_APPEND);
  446. passthru($toHtmlCmd,$return);
  447. if($return==0)
  448. {
  449. @unlink($workPath.'base.min.css');
  450. @unlink($workPath.'fancy.min.css');
  451. $h5code = [];
  452. $allFileList = [];
  453. $allFileList[] = $workPath.'pdf.css';
  454. for($i=1;$i<=$transferPage;$i++)
  455. {
  456. $allFileList[] = $workPath.'page'.$i.'.html';
  457. //如果页数太多,只取10页内容
  458. if($pageNum>10&&$i<=10)
  459. {
  460. $pdfcontent .= strip_tags(file_get_contents($workPath.'page'.$i.'.html'));
  461. }
  462. }
  463. if(is_array($allFileList))foreach ($allFileList as $unitFile)
  464. {
  465. if($this->ossConfig['OPEN_OSS'])
  466. {
  467. $result = $this->oss->Upload($unitFile,str_replace($this->transferDir,'',$unitFile));
  468. if($result['error']==0)
  469. {
  470. if($this->attachmentConfig['absolute_url'])
  471. {
  472. $h5code[] = str_replace($this->ossConfig['OSS_INTERNAL_ENDPOINT'],$this->ossConfig['OSS_ENDPOINT'],$result['data']['url']);
  473. }
  474. else
  475. {
  476. $h5code[] = $result['data']['path'];
  477. }
  478. }
  479. else
  480. {
  481. return [
  482. 'error' => 1,
  483. 'msg' => $result['msg']
  484. ];
  485. }
  486. }
  487. else
  488. {
  489. $relativeFile = str_replace($this->transferDir,'',$unitFile);
  490. $targetFile = UPLOAD_PATH.$relativeFile;
  491. dir_create(dirname($targetFile));
  492. if(copy($unitFile,$targetFile))
  493. {
  494. if($this->attachmentConfig['absolute_url'])
  495. {
  496. $h5code[] = UPLOAD_URL.str_replace(DIRECTORY_SEPARATOR,'/',$relativeFile);
  497. }
  498. else
  499. {
  500. $h5code[] = str_replace(DIRECTORY_SEPARATOR,'/',$relativeFile);
  501. }
  502. }
  503. else
  504. {
  505. return [
  506. 'error' => 1,
  507. 'msg' => '复制失败'
  508. ];
  509. }
  510. }
  511. }
  512. //获取文字内容
  513. if($this->transferConfig['gettext'])
  514. {
  515. //如果页数少,直接用插件读取内容
  516. if($pageNum<=10)
  517. {
  518. $parser = new Parser();
  519. $pdfParer = $parser->parseFile($pdf);
  520. $pdfcontent = $pdfParer->getText();
  521. }
  522. $fullcontent = $pdfcontent;
  523. if(!empty($pdfcontent)&&$this->transferConfig['textlength']>0)
  524. {
  525. $pdfcontent = str_cut($pdfcontent,0,$this->transferConfig['textlength'],'');
  526. }
  527. }
  528. else
  529. {
  530. $pdfcontent = '';
  531. }
  532. if($this->transferConfig['previewfullpage'])
  533. {
  534. $preview_num = $pageNum;
  535. }
  536. else
  537. {
  538. if($this->transferConfig['previewmaxpage_type']=='page')
  539. {
  540. $preview_num = $this->transferConfig['previewmaxpage'];
  541. }
  542. else
  543. {
  544. $previewPagePercent = (float)$this->transferConfig['previewmaxpage']/100;
  545. $preview_num = ceil($previewPagePercent*$pageNum);
  546. }
  547. }
  548. if($preview_num>$pageNum)$preview_num = $pageNum;
  549. if($preview_num==0)$preview_num = 1;
  550. if($this->transferConfig['transfermaxpage']>0&&$preview_num>$this->transferConfig['transfermaxpage']){
  551. $preview_num = $this->transferConfig['transfermaxpage'];
  552. }
  553. if($preview_num>$transferPage)$preview_num = $transferPage;
  554. return [
  555. 'error' => 0,
  556. 'msg' => '处理成功',
  557. 'data'=>['h5code'=>$h5code,'pageNum'=>$pageNum,'transferPage'=>$transferPage,'preview_num'=>$preview_num,'fullcontent'=>$fullcontent,'content'=>$pdfcontent]
  558. ];
  559. }
  560. else
  561. {
  562. return [
  563. 'error' => 1,
  564. 'msg' => '转化html失败'
  565. ];
  566. }
  567. }
  568. else
  569. {
  570. return [
  571. 'error' => 1,
  572. 'msg' => '文档页数异常'
  573. ];
  574. }
  575. }
  576. //将PDF转换成图片
  577. private function _pdf2img($pdf,$transferPage=0)
  578. {
  579. $result = [];
  580. if(!extension_loaded('imagick'))
  581. {
  582. $result = array('error'=>1,'msg'=>'imagick加载失败');
  583. }
  584. if(!file_exists($pdf))
  585. {
  586. $result = array('error'=>1,'msg'=>'pdf不存在');
  587. }
  588. if(!is_readable($pdf))
  589. {
  590. $result = array('error'=>1,'msg'=>'pdf不可读');
  591. }
  592. if(empty($result))
  593. {
  594. $workPath = $this->_initWorkPath($pdf);//dirname($pdf).DIRECTORY_SEPARATOR.md5(basename($pdf)).DIRECTORY_SEPARATOR;
  595. dir_create($workPath);
  596. $imgcode = [];
  597. $im = new \Imagick();
  598. $im->setResolution(300, 300);
  599. $im->setCompressionQuality(100);
  600. //$page = 0;//只读取首页
  601. //$im->readImage($pdf."[".$page."]");
  602. if($transferPage==0)
  603. {
  604. $page = 0;//只读取首页
  605. $im->readImage($pdf."[".$page."]");
  606. }
  607. else
  608. {
  609. $im->readImage($pdf);
  610. }
  611. foreach ($im as $key => $var)
  612. {
  613. if($transferPage>0&&($key+1)>$transferPage)continue;
  614. $var->setImageFormat('png');
  615. $unitFile = $workPath.$key.'.png';
  616. if(@$var->writeImage($unitFile) == true)
  617. {
  618. //打水印
  619. if($this->attachmentConfig['open_watermark']&&!empty($this->imgConfig['watermark']))
  620. {
  621. $watermark = $this->_locateWatermark();
  622. set_img_water($unitFile,$watermark,$this->attachmentConfig['watermark_quality'],$this->attachmentConfig['watermark_pos']);
  623. }
  624. if($this->ossConfig['OPEN_OSS'])
  625. {
  626. $result = $this->oss->Upload($unitFile,str_replace($this->transferDir,'',$unitFile));
  627. if($result['error']==0)
  628. {
  629. if($this->attachmentConfig['absolute_url'])
  630. {
  631. $imgcode[] = str_replace($this->ossConfig['OSS_INTERNAL_ENDPOINT'],$this->ossConfig['OSS_ENDPOINT'],$result['data']['url']);
  632. }
  633. else
  634. {
  635. $imgcode[] = $result['data']['path'];
  636. }
  637. }
  638. else
  639. {
  640. return [
  641. 'error' => 1,
  642. 'msg' => $result['msg']
  643. ];
  644. }
  645. }
  646. else
  647. {
  648. $targetFile = str_replace($this->transferDir,'',$unitFile);
  649. dir_create(dirname(UPLOAD_PATH.$targetFile));
  650. if(copy($unitFile,UPLOAD_PATH.$targetFile))
  651. {
  652. if($this->attachmentConfig['absolute_url'])
  653. {
  654. $imgcode[] = UPLOAD_URL.str_replace(DIRECTORY_SEPARATOR,'/',$targetFile);
  655. }
  656. else
  657. {
  658. $imgcode[] = str_replace(DIRECTORY_SEPARATOR,'/',$targetFile);
  659. }
  660. }
  661. else
  662. {
  663. return [
  664. 'error' => 1,
  665. 'msg' => '复制图片失败'
  666. ];
  667. }
  668. }
  669. }
  670. else
  671. {
  672. return array('error'=>1,'msg'=>'PDF转图片失败');
  673. }
  674. }
  675. $im->destroy();
  676. return array('error'=>0,'msg'=>'PDF转图片成功','data'=>['imgcode'=>$imgcode]);
  677. }
  678. else
  679. {
  680. return $result;
  681. }
  682. }
  683. //获取预览页数
  684. private function _getPreviewNum($pageNum,$transferPage)
  685. {
  686. if($this->transferConfig['previewfullpage'])
  687. {
  688. $preview_num = $pageNum;
  689. }
  690. else
  691. {
  692. if($this->transferConfig['previewmaxpage_type']=='page')
  693. {
  694. $preview_num = $this->transferConfig['previewmaxpage'];
  695. }
  696. else
  697. {
  698. $previewPagePercent = (float)$this->transferConfig['previewmaxpage']/100;
  699. $preview_num = ceil($previewPagePercent*$pageNum);
  700. }
  701. }
  702. if($preview_num>$pageNum)$preview_num = $pageNum;
  703. if($preview_num==0)$preview_num = 1;
  704. if($this->transferConfig['transfermaxpage']>0&&$preview_num>$this->transferConfig['transfermaxpage']){
  705. $preview_num = $this->transferConfig['transfermaxpage'];
  706. }
  707. if($preview_num>$transferPage)$preview_num = $transferPage;
  708. return $preview_num;
  709. }
  710. private function _getPdfPageNum($file) {
  711. if(!file_exists($file))
  712. {
  713. return $this->_getPdfPageNum($file);
  714. }
  715. else
  716. {
  717. exec(sprintf('gs -q -dNODISPLAY -c "(%s) (r) file runpdfbegin pdfpagecount = quit"', $file), $res, $ret);
  718. if(0 == $ret) {
  719. return (int) $res[0];
  720. }
  721. return 0;
  722. }
  723. }
  724. //获取PDF总页数和转换页数
  725. private function _pdfPageInfo($pdf)
  726. {
  727. $pageNum = $this->_getPdfPageNum($pdf);
  728. if($pageNum>=1)
  729. {
  730. if($this->transferConfig['fullpage'])
  731. {
  732. $transferPage = $pageNum;
  733. }
  734. else
  735. {
  736. if($this->transferConfig['transferpage_type']=='page')
  737. {
  738. $transferPage = $this->transferConfig['transferpage'];
  739. }
  740. else
  741. {
  742. $transferPagePercent = (float)$this->transferConfig['transferpage']/100;
  743. $transferPage = ceil($transferPagePercent*$pageNum);
  744. }
  745. }
  746. if($this->transferConfig['transfermaxpage']>0&&$transferPage>$this->transferConfig['transfermaxpage'])
  747. {
  748. $transferPage = $this->transferConfig['transfermaxpage'];
  749. }
  750. if($transferPage>$pageNum)$transferPage = $pageNum;
  751. if($transferPage==0)$transferPage = 1;
  752. return [
  753. 'error' => 0,
  754. 'msg' => '获取pdf页数成功',
  755. 'data'=>['pageNum'=>$pageNum,'transferPage'=>$transferPage]
  756. ];
  757. }
  758. else
  759. {
  760. return [
  761. 'error' => 1,
  762. 'msg' => '文档页数异常'
  763. ];
  764. }
  765. }
  766. //PPT云转换
  767. private function _doppt($transferFolder,$transferFile)
  768. {
  769. $ext = fileext($transferFile);
  770. $workPath = $this->_initWorkPath($transferFile);
  771. $fileUrl = $this->transferUrl.str_replace(DIRECTORY_SEPARATOR,'/',str_replace($this->transferDir,'',$transferFile));
  772. $data = ['file'=>$fileUrl,'appkey'=>$this->transferConfig['cloudpwd'],'action'=>'ppt2img'];
  773. $options['time_out'] = 300;
  774. $pptresult = https_request($this->transferConfig['cloudurl'],$options,$data);
  775. $pptresult = json_decode($pptresult,true);
  776. if($pptresult['error']==0)
  777. {
  778. $images = $pptresult['data']['imgs'];
  779. $imgcode = [];
  780. if(is_array($images))foreach($images as $image)
  781. {
  782. $imgfile = $workPath.basename($image);
  783. if(file_put_contents($imgfile,file_get_contents($image)))
  784. {
  785. $unitFile = $imgfile;
  786. //打水印
  787. if($this->attachmentConfig['open_watermark']&&!empty($this->imgConfig['watermark']))
  788. {
  789. $watermark = $this->_locateWatermark();
  790. set_img_water($unitFile,$watermark,$this->attachmentConfig['watermark_quality'],$this->attachmentConfig['watermark_pos']);
  791. }
  792. if($this->ossConfig['OPEN_OSS'])
  793. {
  794. $result = $this->oss->Upload($unitFile,str_replace($this->transferDir,'',$unitFile));
  795. if($result['error']==0)
  796. {
  797. if($this->attachmentConfig['absolute_url'])
  798. {
  799. $imgcode[] = str_replace($this->ossConfig['OSS_INTERNAL_ENDPOINT'],$this->ossConfig['OSS_ENDPOINT'],$result['data']['url']);
  800. }
  801. else
  802. {
  803. $imgcode[] = $result['data']['path'];
  804. }
  805. }
  806. else
  807. {
  808. return [
  809. 'error' => 1,
  810. 'msg' => $result['msg']
  811. ];
  812. }
  813. }
  814. else
  815. {
  816. $relativeFile = str_replace($this->transferDir,'',$unitFile);
  817. $targetFile = UPLOAD_PATH.$relativeFile;
  818. dir_create(dirname($targetFile));
  819. if(copy($unitFile,$targetFile))
  820. {
  821. if($this->attachmentConfig['absolute_url'])
  822. {
  823. $imgcode[] = UPLOAD_URL.str_replace(DIRECTORY_SEPARATOR,'/',$relativeFile);
  824. }
  825. else
  826. {
  827. $imgcode[] = str_replace(DIRECTORY_SEPARATOR,'/',$relativeFile);
  828. }
  829. }
  830. else
  831. {
  832. return [
  833. 'error' => 1,
  834. 'msg' => 'PPT单元页上传失败'
  835. ];
  836. }
  837. }
  838. }
  839. }
  840. $page_num = count($imgcode);
  841. if($this->transferConfig['previewfullpage'])
  842. {
  843. $preview_num = $page_num;
  844. }
  845. else
  846. {
  847. if($this->transferConfig['previewmaxpage_type']=='page')
  848. {
  849. $preview_num = $this->transferConfig['previewmaxpage'];
  850. }
  851. else
  852. {
  853. $previewPagePercent = (float)$this->transferConfig['previewmaxpage']/100;
  854. $preview_num = ceil($previewPagePercent*$page_num);
  855. }
  856. }
  857. //预览页数不应该迟超过转换页数上线
  858. if($this->transferConfig['transfermaxpage']>0&&$preview_num>$this->transferConfig['transfermaxpage']){
  859. $preview_num = $this->transferConfig['transfermaxpage'];
  860. }
  861. if($preview_num>$page_num)$preview_num = $page_num;
  862. if($preview_num==0)$preview_num = 1;
  863. return [
  864. 'error' => 0,
  865. 'msg' => '处理成功',
  866. 'data'=>['imgcode'=>$imgcode,'page_num'=>$page_num,'preview_num'=>$preview_num]
  867. ];
  868. }
  869. else
  870. {
  871. return array('error'=>1,'msg'=>'PPT远程处理失败');
  872. }
  873. }
  874. private function _domedia($transferFolder,$transferFile)
  875. {
  876. $ext = fileext($transferFile);
  877. $workPath = $this->_initWorkPath($transferFile);
  878. $videoInfo = $this->_getVideoInfo($transferFile);
  879. //预览和截断时间(秒)
  880. if($videoInfo['all_time']<=30)
  881. {
  882. $preview_num = 30;
  883. }
  884. else
  885. {
  886. if($this->transferConfig['previewfullmedia'])
  887. {
  888. $preview_num = $videoInfo['all_time'];
  889. }
  890. else
  891. {
  892. if($this->transferConfig['mediatime_type']=='seconds')
  893. {
  894. $preview_num = $this->transferConfig['mediatime'];
  895. }
  896. else
  897. {
  898. $previewMediaPercent = (float)$this->transferConfig['mediatime']/100;
  899. $preview_num = ceil($previewMediaPercent*$videoInfo['all_time']);
  900. }
  901. }
  902. }
  903. if($preview_num>$videoInfo['all_time'])$preview_num = $videoInfo['all_time'];
  904. $cuttedFile = $workPath.md5(basename($transferFile)).'_'.$preview_num.'.'.$ext;
  905. $cuttedDir = dirname($cuttedFile);
  906. $cutto = $this->changeTimeType($preview_num);
  907. dir_create($cuttedDir);
  908. $cutVideoCmd = "/usr/bin/ffmpeg -i $transferFile -vcodec copy -acodec copy -ss 00:00:00 -to $cutto $cuttedFile -y";
  909. passthru($cutVideoCmd,$return);
  910. if($return==0)
  911. {
  912. $unitFile = $cuttedFile;
  913. $h5code = [];
  914. if($this->ossConfig['OPEN_OSS'])
  915. {
  916. $result = $this->oss->Upload($unitFile,str_replace($this->transferDir,'',$unitFile));
  917. if($result['error']==0)
  918. {
  919. if($this->attachmentConfig['absolute_url'])
  920. {
  921. $h5code[] = str_replace($this->ossConfig['OSS_INTERNAL_ENDPOINT'],$this->ossConfig['OSS_ENDPOINT'],$result['data']['url']);
  922. }
  923. else
  924. {
  925. $h5code[] = $result['data']['path'];
  926. }
  927. }
  928. else
  929. {
  930. return [
  931. 'error' => 1,
  932. 'msg' => $result['msg']
  933. ];
  934. }
  935. }
  936. else
  937. {
  938. $relativeFile = str_replace($this->transferDir,'',$unitFile);
  939. $targetFile = UPLOAD_PATH.$relativeFile;
  940. dir_create(dirname($targetFile));
  941. if(copy($unitFile,$targetFile))
  942. {
  943. if($this->attachmentConfig['absolute_url'])
  944. {
  945. $h5code[] = UPLOAD_URL.str_replace(DIRECTORY_SEPARATOR,'/',$relativeFile);
  946. }
  947. else
  948. {
  949. $h5code[] = str_replace(DIRECTORY_SEPARATOR,'/',$relativeFile);
  950. }
  951. }
  952. else
  953. {
  954. return [
  955. 'error' => 1,
  956. 'msg' => '音视频文件上传失败'
  957. ];
  958. }
  959. }
  960. return [
  961. 'error' => 0,
  962. 'msg' => '处理成功',
  963. 'data'=>['h5code'=>$h5code,'page_num'=>$videoInfo['all_time'],'preview_num'=>$preview_num]
  964. ];
  965. }
  966. else
  967. {
  968. return array('error'=>1,'msg'=>'音视频裁剪失败');
  969. }
  970. }
  971. //获取文档总页数和转换页数
  972. private function _doRemoteTrans($file)
  973. {
  974. $workPath = $this->_initWorkPath($file);//dirname($pdf).DIRECTORY_SEPARATOR.md5(basename($pdf)).DIRECTORY_SEPARATOR;
  975. dir_create($workPath);
  976. $imgcode = [];
  977. if($this->transferConfig['OSS_ACCESS_KEY'])
  978. {
  979. //腾讯云
  980. $object = str_replace($this->transferDir,'',$file);
  981. $protocol = str_replace("://","",SITE_PROTOCOL);
  982. $ossClient = new \Qcloud\Cos\Client(
  983. array(
  984. 'region' => $this->transferConfig['OSS_REGION'],
  985. 'schema' => $protocol, //协议头部,默认为http
  986. 'credentials'=> array(
  987. 'secretId' => $this->transferConfig['OSS_ACCESS_KEY'] ,
  988. 'secretKey' => $this->transferConfig['OSS_ACCESS_SECRET'])));
  989. $fileContent = fopen($file, "rb");
  990. if ($fileContent) {
  991. $ossresult = $ossClient->putObject(array(
  992. 'Bucket' => $this->transferConfig['OSS_BUCKET'],
  993. 'Key' => $object,
  994. 'Body' => $fileContent));
  995. //远程文件地址
  996. $remoteFileUrl = SITE_PROTOCOL.$ossresult['Location'];
  997. }
  998. $pageInfo = $this->_getRemoteFilePage($remoteFileUrl);
  999. if($remoteFileUrl)
  1000. {
  1001. for($i=1;$i<=$pageInfo['data']['transferPage'];$i++)
  1002. {
  1003. $tempUrl = $remoteFileUrl.'?ci-process=doc-preview&page='.$i;
  1004. $requestData = https_request($tempUrl);
  1005. //写图片到平台存储
  1006. $unitFile = $workPath.$i.'.jpg';
  1007. if(file_put_contents($unitFile,$requestData))
  1008. {
  1009. //打水印
  1010. if($this->attachmentConfig['open_watermark']&&!empty($this->imgConfig['watermark']))
  1011. {
  1012. $watermark = $this->_locateWatermark();
  1013. set_img_water($unitFile,$watermark,$this->attachmentConfig['watermark_quality'],$this->attachmentConfig['watermark_pos']);
  1014. }
  1015. if($this->ossConfig['OPEN_OSS'])
  1016. {
  1017. $result = $this->oss->Upload($unitFile,str_replace($this->transferDir,'',$unitFile));
  1018. if($result['error']==0)
  1019. {
  1020. if($this->attachmentConfig['absolute_url'])
  1021. {
  1022. $imgcode[] = str_replace($this->ossConfig['OSS_INTERNAL_ENDPOINT'],$this->ossConfig['OSS_ENDPOINT'],$result['data']['url']);
  1023. }
  1024. else
  1025. {
  1026. $imgcode[] = $result['data']['path'];
  1027. }
  1028. }
  1029. else
  1030. {
  1031. return [
  1032. 'error' => 1,
  1033. 'msg' => $result['msg']
  1034. ];
  1035. }
  1036. }
  1037. else
  1038. {
  1039. $targetFile = str_replace($this->transferDir,'',$unitFile);
  1040. dir_create(dirname(UPLOAD_PATH.$targetFile));
  1041. if(copy($unitFile,UPLOAD_PATH.$targetFile))
  1042. {
  1043. if($this->attachmentConfig['absolute_url'])
  1044. {
  1045. $imgcode[] = UPLOAD_URL.str_replace(DIRECTORY_SEPARATOR,'/',$targetFile);
  1046. }
  1047. else
  1048. {
  1049. $imgcode[] = str_replace(DIRECTORY_SEPARATOR,'/',$targetFile);
  1050. }
  1051. }
  1052. else
  1053. {
  1054. return [
  1055. 'error' => 1,
  1056. 'msg' => '复制图片失败'
  1057. ];
  1058. }
  1059. }
  1060. }
  1061. }
  1062. //删除转换容器中的文件
  1063. $ossClient->deleteObject(array(
  1064. 'Bucket' => $this->transferConfig['OSS_BUCKET'], //存储桶名称
  1065. 'Key' => $object
  1066. ));
  1067. return [
  1068. 'error' => 0,
  1069. 'msg' => '远程转换成功',
  1070. 'data'=>['pageNum'=>$pageInfo['data']['pageNum'],'transferPage'=>$pageInfo['data']['transferPage'],'imgcode'=>$imgcode]
  1071. ];
  1072. }
  1073. else
  1074. {
  1075. return array('error'=>1,'msg'=>'上传到对象存储失败');
  1076. }
  1077. }
  1078. else
  1079. {
  1080. return array('error'=>1,'msg'=>'远程转换配置错误');
  1081. }
  1082. }
  1083. //获取远程文件总页数
  1084. private function _getRemoteFilePage($remoteFileUrl)
  1085. {
  1086. $max_page = 1000;
  1087. for($i=1;$i<=$max_page;$i++)
  1088. {
  1089. $tempUrl = $remoteFileUrl.'?ci-process=doc-preview&page='.$i;
  1090. $requestData = https_request($tempUrl);
  1091. if($this->iss_xml($requestData))
  1092. {
  1093. //格式化XML
  1094. libxml_disable_entity_loader(true);
  1095. $requestObj = simplexml_load_string($requestData, 'SimpleXMLElement', LIBXML_NOCDATA);
  1096. if($requestObj->Code=='NoSuchPage')
  1097. {
  1098. $pageNum = ($i-1);
  1099. if($this->transferConfig['fullpage'])
  1100. {
  1101. $transferPage = $pageNum;
  1102. }
  1103. else
  1104. {
  1105. if($this->transferConfig['transferpage_type']=='page')
  1106. {
  1107. $transferPage = $this->transferConfig['transferpage'];
  1108. }
  1109. else
  1110. {
  1111. $transferPagePercent = (float)$this->transferConfig['transferpage']/100;
  1112. $transferPage = ceil($transferPagePercent*$pageNum);
  1113. }
  1114. }
  1115. if($this->transferConfig['transfermaxpage']>0&&$transferPage>$this->transferConfig['transfermaxpage'])
  1116. {
  1117. $transferPage = $this->transferConfig['transfermaxpage'];
  1118. }
  1119. if($transferPage>$pageNum)$transferPage = $pageNum;
  1120. if($transferPage==0)$transferPage = 1;
  1121. return [
  1122. 'error' => 0,
  1123. 'msg' => '获取页数成功',
  1124. 'data'=>['pageNum'=>$pageNum,'transferPage'=>$transferPage]
  1125. ];
  1126. }
  1127. }
  1128. }
  1129. }
  1130. //判断是否是XML
  1131. function iss_xml($content){ //判断返回的内容是不是 xml 格式
  1132. $xml_parser = xml_parser_create();
  1133. $res = xml_parse($xml_parser, $content, true);
  1134. xml_parser_free($xml_parser); return $res;
  1135. }
  1136. //获取文件信息
  1137. private function _getVideoInfo($file) {
  1138. $command = " ffmpeg -i $file 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//";
  1139. ob_start();
  1140. passthru($command);
  1141. $info = ob_get_contents();
  1142. ob_end_clean();
  1143. $data = array();
  1144. $duration = explode(":",$info);
  1145. $duration_in_seconds = $duration[0]*3600 + $duration[1]*60+ round($duration[2]);//转化为秒
  1146. $data['all_time'] = $duration_in_seconds;
  1147. $data['size'] = filesize($file); //文件大小
  1148. return $data;
  1149. }
  1150. function changeTimeType($seconds)
  1151. {
  1152. if ($seconds > 3600) {
  1153. $hours = intval($seconds / 3600);
  1154. $time = $hours . ":" . gmstrftime('%M:%S', $seconds);
  1155. } else {
  1156. $time = gmstrftime('%H:%M:%S', $seconds);
  1157. }
  1158. return $time;
  1159. }
  1160. //解压缩压缩包文件
  1161. private function _uncompress($transferFile,$id)
  1162. {
  1163. $ext = fileext($transferFile);
  1164. $workPath = $this->_initWorkPath($transferFile);
  1165. if($ext=='zip')
  1166. {
  1167. $z = new Unzip();
  1168. $z->unzip($transferFile,$workPath, true, true);
  1169. }
  1170. else if($ext=='rar')
  1171. {
  1172. $rar_file = rar_open($transferFile) or die("Failed to open Rar archive");
  1173. $entries = rar_list($rar_file);
  1174. foreach ($entries as $entry) {
  1175. $entry->extract($workPath);
  1176. }
  1177. rar_close($rar_file);
  1178. }
  1179. $filelist = $this->scanDir($workPath,$this->transferDir);
  1180. if($this->transferConfig['previewpack'])
  1181. {
  1182. $this->initPackFile($filelist,$id);
  1183. }
  1184. return $filelist;
  1185. }
  1186. //初始化压缩包待转换数据
  1187. function initPackFile($arr,$id)
  1188. {
  1189. $packexts = $this->transferConfig['packexts']?explode("|",$this->transferConfig['packexts']):['ppt','pptx','doc','docx','xls','xlsx','pdf','jpeg','jpg','png','gif','bmp','txt'];
  1190. Yii::$app->db->createCommand("delete from {{%doc_packfile}} where doc_id=$id")->execute();
  1191. if(is_array($arr))foreach($arr as $key=>$val) {
  1192. if(is_array($arr[$key])) {
  1193. $this->initPackFile($arr[$key],$id);
  1194. }
  1195. else
  1196. {
  1197. if(!in_array(fileext($arr[$key]),$packexts))continue;
  1198. $unitFile = $this->transferDir.$arr[$key];
  1199. if($this->ossConfig['OPEN_OSS'])
  1200. {
  1201. $result = $this->oss->Upload($unitFile,str_replace($this->transferDir,'',$unitFile));
  1202. if($result['error']==0)
  1203. {
  1204. if($this->attachmentConfig['absolute_url'])
  1205. {
  1206. $realPath = str_replace($this->ossConfig['OSS_INTERNAL_ENDPOINT'],$this->ossConfig['OSS_ENDPOINT'],$result['data']['url']);
  1207. }
  1208. else
  1209. {
  1210. $realPath = $result['data']['path'];
  1211. }
  1212. }
  1213. }
  1214. else
  1215. {
  1216. $relativeFile = str_replace($this->transferDir,'',$unitFile);
  1217. $targetFile = UPLOAD_PATH.$relativeFile;
  1218. $targetPath = dirname($targetFile);
  1219. dir_create($targetPath);
  1220. if(copy($unitFile,$targetFile))
  1221. {
  1222. if($this->attachmentConfig['absolute_url'])
  1223. {
  1224. $realPath = UPLOAD_URL.str_replace(DIRECTORY_SEPARATOR,'/',$relativeFile);
  1225. }
  1226. else
  1227. {
  1228. $realPath = str_replace(DIRECTORY_SEPARATOR,'/',$relativeFile);
  1229. }
  1230. }
  1231. }
  1232. if(!empty($realPath))
  1233. {
  1234. $ext = fileext($arr[$key]);
  1235. Yii::$app->db->createCommand("insert into {{%doc_packfile}} set doc_id=$id,name='".addslashes(basename($arr[$key],'.'.$ext))."',ext='".$ext."',path='".addslashes($arr[$key])."',path_md5='".md5($arr[$key])."',realpath='".addslashes($realPath)."'")->execute();
  1236. }
  1237. }
  1238. }
  1239. }
  1240. //获取压缩包目录树(全路径)
  1241. function scanDir($pathName,$transferDir) {
  1242. //将结果保存在result变量中
  1243. $result = array();
  1244. $temp = array();
  1245. //判断传入的变量是否是目录
  1246. if(!is_dir($pathName) || !is_readable($pathName)) {
  1247. return null;
  1248. }
  1249. $pathName = rtrim($pathName,'/');
  1250. //取出目录中的文件和子目录名,使用scandir函数
  1251. $allFiles = scandir($pathName);
  1252. //遍历他们
  1253. foreach($allFiles as $fileName) {
  1254. //判断是否是.和..因为这两个东西神马也不是。。。
  1255. if(in_array($fileName, array('.', '..'))) {
  1256. continue;
  1257. }
  1258. //路径加文件名
  1259. $fullName = $pathName.'/'.$fileName;
  1260. //如果是目录的话就继续遍历这个目录
  1261. if(is_dir($fullName)) {
  1262. //将这个目录中的文件信息存入到数组中
  1263. $result[$fullName] = $this->scanDir($fullName,$transferDir);
  1264. }else {
  1265. //如果是文件就先存入临时变量
  1266. $temp[] = $fullName;
  1267. }
  1268. }
  1269. //取出文件
  1270. if($temp) {
  1271. foreach($temp as $f) {
  1272. $result[] = $f;
  1273. }
  1274. }
  1275. $result = string2array(str_replace($transferDir,'',array2string($result)));
  1276. return $result;
  1277. }
  1278. //处理图片
  1279. private function _doimg($transferFolder,$transferFile)
  1280. {
  1281. $workPath = $this->_initWorkPath($transferFile);
  1282. $newname = time();
  1283. $unitFile = $workPath.$newname.'.'.fileext($transferFile);
  1284. copy($transferFile,$unitFile);
  1285. //$resize = new \app\common\components\ResizeImage($transferFile, 2000, 2000,$newname, 0,0,$workPath);
  1286. //$unitFile = $resize->dstimg;
  1287. //打水印
  1288. if($this->attachmentConfig['open_watermark']&&!empty($this->imgConfig['watermark']))
  1289. {
  1290. $watermark = $this->_locateWatermark();
  1291. set_img_water($unitFile,$watermark,$this->attachmentConfig['watermark_quality'],$this->attachmentConfig['watermark_pos']);
  1292. }
  1293. if($this->ossConfig['OPEN_OSS'])
  1294. {
  1295. $result = $this->oss->Upload($unitFile,str_replace($this->transferDir,'',$unitFile));
  1296. if($result['error']==0)
  1297. {
  1298. if($this->attachmentConfig['absolute_url'])
  1299. {
  1300. $imgcode[] = str_replace($this->ossConfig['OSS_INTERNAL_ENDPOINT'],$this->ossConfig['OSS_ENDPOINT'],$result['data']['url']);
  1301. }
  1302. else
  1303. {
  1304. $imgcode[] = $result['data']['path'];
  1305. }
  1306. }
  1307. else
  1308. {
  1309. return [
  1310. 'error' => 1,
  1311. 'msg' => $result['msg']
  1312. ];
  1313. }
  1314. }
  1315. else
  1316. {
  1317. $relativeFile = str_replace($this->transferDir,'',$unitFile);
  1318. $targetFile = UPLOAD_PATH.$relativeFile;
  1319. $targetPath = dirname(UPLOAD_PATH.$relativeFile);
  1320. dir_create($targetPath);
  1321. if(copy($unitFile,$targetFile))
  1322. {
  1323. if($this->attachmentConfig['absolute_url'])
  1324. {
  1325. $imgcode[] = UPLOAD_URL.str_replace(DIRECTORY_SEPARATOR,'/',$relativeFile);
  1326. }
  1327. else
  1328. {
  1329. $imgcode[] = str_replace(DIRECTORY_SEPARATOR,'/',$relativeFile);
  1330. }
  1331. }
  1332. else
  1333. {
  1334. return [
  1335. 'error' => 1,
  1336. 'msg' => '转换图片失败'
  1337. ];
  1338. }
  1339. }
  1340. return array('error'=>0,'msg'=>'图片转换成功','data'=>['imgcode'=>$imgcode]);
  1341. }
  1342. //本地化水印图片
  1343. private function _locateWatermark()
  1344. {
  1345. $watermarkPath = UPLOAD_PATH.'watermark'.DIRECTORY_SEPARATOR;
  1346. dir_create($watermarkPath);
  1347. $watermark = $watermarkPath.basename($this->imgConfig['watermark']);
  1348. //本地存储水印
  1349. if(!file_exists($watermark))
  1350. {
  1351. file_put_contents($watermark,file_get_contents(getFileUrl($this->imgConfig['watermark'])));
  1352. }
  1353. return $watermark;
  1354. }
  1355. //根据临时转换文件路径,生成目标子目录
  1356. private function _initWorkPath($transferFile)
  1357. {
  1358. $ext = fileext($transferFile);
  1359. $basename = basename($transferFile,'.'.$ext);
  1360. $workPath = dirname($transferFile).DIRECTORY_SEPARATOR.md5($basename).DIRECTORY_SEPARATOR;
  1361. dir_create($workPath);
  1362. return $workPath;
  1363. }
  1364. //下载文件到转码目录
  1365. private function _locateFile($file,$ext)
  1366. {
  1367. if(!check_remote_file_exists($file))return array('error'=>1,'msg'=>'文件不存在');
  1368. $result = parse_url($file);
  1369. $result['path'] = str_replace('/upload/','',$result['path']);
  1370. $relativeFile = ltrim(str_replace('/',DIRECTORY_SEPARATOR,$result['path']),DIRECTORY_SEPARATOR);
  1371. $relativeFolder = dirname($relativeFile).DIRECTORY_SEPARATOR;
  1372. $transferFile = $this->transferDir.$relativeFile;
  1373. $transferFolder = dirname($transferFile).DIRECTORY_SEPARATOR;
  1374. dir_create($transferFolder);
  1375. if(file_exists($transferFile))
  1376. {
  1377. return array('error'=>0,'msg'=>'下载成功','data'=>[$transferFolder,$transferFile,$relativeFolder,$relativeFile]);
  1378. }
  1379. if($this->ossConfig['OPEN_OSS'])
  1380. {
  1381. $downresult = $this->oss->downLoad($transferFile,$relativeFile);
  1382. if($downresult['error']==0)
  1383. {
  1384. return array('error'=>0,'msg'=>'下载成功','data'=>[$transferFolder,$transferFile,$relativeFolder,$relativeFile]);
  1385. }
  1386. else
  1387. {
  1388. return array('error'=>1,'msg'=>$downresult['msg']);
  1389. }
  1390. }
  1391. else
  1392. {
  1393. if(is_net_file($file))
  1394. {
  1395. file_put_contents($transferFile,file_get_contents($file));
  1396. }
  1397. else
  1398. {
  1399. file_put_contents($transferFile,file_get_contents(UPLOAD_URL.ltrim($result['path'],'/')));
  1400. }
  1401. return array('error'=>0,'msg'=>'下载成功','data'=>[$transferFolder,$transferFile,$relativeFolder,$relativeFile]);
  1402. }
  1403. }
  1404. //写日志
  1405. private function _saveLog($title,$doc)
  1406. {
  1407. file_put_contents($this->logFile,$doc['title']."(".$title."-".get_date(time(),'Y-m-d H:i:s').")\r\n",FILE_APPEND);
  1408. }
  1409. //自动上架
  1410. private function _autoAudit($id)
  1411. {
  1412. $doc = Yii::$app->db->createCommand("select * from {{%doc}} where id=".$id)->queryOne();
  1413. $docData = Yii::$app->db->createCommand("select * from {{%doc_data}} where id=".$id)->queryOne();
  1414. //把表从草稿表复制到正式表
  1415. $docFields = [];
  1416. if(is_array($doc))foreach($doc as $k=>$v)
  1417. {
  1418. if($k=='id'||$k=='status')continue;
  1419. if($k=='description')
  1420. {
  1421. //文档摘要处理
  1422. if((empty($doc['description'])||$doc['title']==$doc['description'])&&!empty($docData['content']))
  1423. {
  1424. $length = $this->transferConfig['desclength']>0?$this->transferConfig['desclength']:'350';
  1425. $description = str_cut(removeHtml($docData['content'],0),0,$length);
  1426. }
  1427. else
  1428. {
  1429. $description = $doc['description'];
  1430. }
  1431. $docFields[] = "description ='".$description."'";
  1432. }
  1433. else
  1434. {
  1435. $docFields[] = "$k ='".$v."'";
  1436. }
  1437. }
  1438. $docFields[] = "status=1";
  1439. $result = Yii::$app->db->createCommand("insert into {{%doc_real}} set ".join(",",$docFields))->execute();
  1440. $newId = getLastInsertId();
  1441. if($result)
  1442. {
  1443. $docDataFields= [];
  1444. if(is_array($docData))foreach($docData as $k=>$v)
  1445. {
  1446. if($k=='id'||$k=='hash')continue;
  1447. if($k=='file'&&$doc['doc_type']==1)
  1448. {
  1449. $hash = randString(8);
  1450. $docDataFields[] = "hash='".$hash."'";
  1451. $docDataFields[] = "file='".sys_auth($docData['file'],'ENCODE',$hash)."'";
  1452. }
  1453. else
  1454. {
  1455. $docDataFields[] = "$k ='".$v."'";
  1456. }
  1457. }
  1458. $docDataFields[] = "id='".$newId."'";
  1459. if($doc['doc_type']==1&&!in_array($doc['ext'],['rar','zip','mp3','mp4']))$docDataFields[] = "content=''";
  1460. $result1 = Yii::$app->db->createCommand("insert into {{%doc_real_data}} set ".join(",",$docDataFields))->execute();
  1461. if($result1)
  1462. {
  1463. //更新附件关联信息,将草稿表的记录改成正式表
  1464. $sql = "update {{%attachment_index}} set data_id=$newId,table_name='doc_real' where table_name='doc' and field='thumb' and data_id=$id";
  1465. Yii::$app->db->createCommand($sql)->execute();
  1466. $sql = "update {{%attachment_index}} set data_id=$newId,table_name='doc_real_data' where table_name='doc_data' and field='file' and data_id=$id";
  1467. Yii::$app->db->createCommand($sql)->execute();
  1468. //更新tag标签.将草稿表的记录改成正式表
  1469. $sql = "update {{%tag_data}} set data_id=$newId,table_name='doc_real' where table_name='doc' and data_id=$id";
  1470. Yii::$app->db->createCommand($sql)->execute();
  1471. //更新审核记录表
  1472. $sql = "update {{%doc_auditlog}} set doc_real_id=$newId where doc_id=$id";
  1473. Yii::$app->db->createCommand($sql)->execute();
  1474. //更新压缩包细节目录
  1475. $sql = "update {{%doc_packfile}} set doc_real_id=$newId where doc_id=$id";
  1476. Yii::$app->db->createCommand($sql)->execute();
  1477. //更新合辑
  1478. if($doc['doc_type']==2)
  1479. {
  1480. $sql = "update {{%doc_col}} set col_id=$newId where col_id=$id";
  1481. Yii::$app->db->createCommand($sql)->execute();
  1482. }
  1483. $sql = "delete from {{%doc}} where id=$id";
  1484. $dResult = Yii::$app->db->createCommand($sql)->execute();
  1485. $sql = "delete from {{%doc_data}} where id=$id";
  1486. $dResult1 = Yii::$app->db->createCommand($sql)->execute();
  1487. //把草稿删除
  1488. if(!$dResult||!$dResult1)
  1489. {
  1490. return ['error' => 1,'msg' => '操作失败'];
  1491. }
  1492. else
  1493. {
  1494. return ['error' => 0,'msg' => '操作成功'];
  1495. }
  1496. }
  1497. else
  1498. {
  1499. return ['error' => 1,'msg' => '操作失败'];
  1500. }
  1501. }
  1502. else
  1503. {
  1504. return ['error' => 1,'msg' => '操作失败'];
  1505. }
  1506. }
  1507. }