Files
Pichome/dzz/onlyoffice_view/classes/thumb.php

196 lines
7.7 KiB
PHP
Raw Normal View History

2022-06-25 14:48:20 +08:00
<?php
2024-01-31 01:00:33 +08:00
2022-06-25 14:48:20 +08:00
namespace dzz\onlyoffice_view\classes;
use \core as C;
use \DB as DB;
use \IO as IO;
2024-01-31 01:00:33 +08:00
2022-06-25 14:48:20 +08:00
require_once libfile('class/xml');
2024-01-31 01:00:33 +08:00
require_once(DZZ_ROOT . './dzz/onlyoffice_view/jwt/jwtmanager.php');
class thumb
{
2022-06-25 14:48:20 +08:00
public $onlyofficethumbext = '';
public $onlyDocumentUrl = '';
public $onlyDocumentdocUrl = '';
2024-01-31 01:00:33 +08:00
public $onlyDocumentdocSecret = '';
2022-06-25 14:48:20 +08:00
2024-01-31 01:00:33 +08:00
public function run($meta)
{
global $_G;
if (strpos($meta['realpath'], ':') === false) {
2022-06-25 14:48:20 +08:00
$bz = 'dzz';
2024-01-31 01:00:33 +08:00
} else {
2022-06-25 14:48:20 +08:00
$patharr = explode(':', $meta['realpath']);
$bz = $patharr[0];
$did = $patharr[1];
}
2024-01-31 01:00:33 +08:00
if (!is_numeric($did) || $did < 2) {
2022-06-25 14:48:20 +08:00
$bz = 'dzz';
}
2024-01-31 01:00:33 +08:00
$app = C::t('app_market')->fetch_by_identifier('onlyoffice_view', 'dzz');
$onlyofficedata = unserialize($app['extra']);
2022-06-25 14:48:20 +08:00
2024-01-31 01:00:33 +08:00
$this->onlyDocumentUrl = $onlyofficedata['DocumentUrl'];
$this->onlyDocumentdocSecret = $onlyofficedata['secret'];
$this->onlyDocumentdocUrl = $onlyofficedata['FileUrl'] ? $onlyofficedata['FileUrl'] : getglobal('siteurl');
2022-06-25 14:48:20 +08:00
$onlyofficethumbext = getglobal('config/onlyofficeviewextlimit');
2024-01-31 01:00:33 +08:00
$this->onlyofficethumbext = explode(',', $onlyofficedata['exts']);
2022-06-25 14:48:20 +08:00
2024-01-31 01:00:33 +08:00
if (!in_array($meta['ext'], $this->onlyofficethumbext)) {
2022-06-25 14:48:20 +08:00
return '';
2024-01-31 01:00:33 +08:00
} else {
if($meta['aid']){
$attach = IO::getMeta('attach::'.$meta['aid']);
}else{
$attach = IO::getMeta($meta['rid']);
}
$attach['stream'] = IO::getFileUri($attach['path']);
if($meta['aid']) $attach['stream'] = $this->onlyDocumentdocUrl . 'index.php?mod=io&op=getStream&hash=' . VERHASH . '&path=' . dzzencode('attach::'.$meta['aid']);
else $attach['stream'] = $this->onlyDocumentdocUrl . 'index.php?mod=io&op=getStream&hash=' . VERHASH . '&path=' . dzzencode($attach['rid'] . '_3', '', 0, 0);
if ($url = $this->getThumb($attach)) {
return array($url);
2022-06-25 14:48:20 +08:00
}
2024-01-31 01:00:33 +08:00
}
2022-06-25 14:48:20 +08:00
}
2024-01-31 01:00:33 +08:00
function textEncode($data)
{
$mime = \dzz_mime::get_type($data['ext']);
list($pre) = explode('/', $mime);
if ($pre != 'text') return $data['stream'];
2022-06-25 14:48:20 +08:00
$str = file_get_contents($data['stream']);
require_once DZZ_ROOT . './dzz/class/class_encode.php';
$p = new \Encode_Core();
2024-01-31 01:00:33 +08:00
$code = $p->get_encoding($str);
if ($code != CHARSET) $str = diconv($str, $code, CHARSET);
if($data['aid'])$cachekey = 'cache/' .md5($data['aid']) . '.' . $data['ext'];
else $cachekey = 'cache/' . $data['rid'] . '.' . $data['ext'];
$file = getglobal('setting/attachdir') . $cachekey;
if (file_put_contents($file, $data['stream'])) {
$data['stream'] = getglobal('localurl') . getglobal('setting/attachurl') . $cachekey;
2022-06-25 14:48:20 +08:00
}
return $data['stream'];
}
2024-01-31 01:00:33 +08:00
function getThumb($data, $width = 993, $height = 1043)
{
global $_G;
$stream=$this->textEncode($data);
//print_r($stream);die;
$post_data = array(
'async' => false,
'filetype' => $data['ext'],
'key' => ($data['aid']) ? md5($data['aid']):$data['rid'],
'outputtype' => 'png',
'thumbnail' => array(
'aspect' => true,
'first' => 1,
'height' => $height,
'width' => $width
),
'title' => $data['name'],
'url' => $stream
);
if ($this->onlyDocumentdocSecret) {
$post_data['token'] = jwtEncode($post_data, $this->onlyDocumentdocSecret);
}
$conversion_url = $this->getCUrl();
$ret = ($this->getConvertUrl($conversion_url, json_encode($post_data)));
if ($url = $ret['FileUrl']) {
$target = 'pichomethumb/' . date('Ym') . '/' . date('d') .'/'.($data['aid'] ? md5($data['aid']):md5($data['rid'])) . '_original.png';
$png = getglobal('setting/attachdir') .$target;
$dir = dirname($png);
dmkdir($dir, 0777, false);
if (file_put_contents($png, curl_file_get_contents($url)) != false) {
$defaultspace = $_G['setting']['defaultspacesetting'];
//如果原文件位置不在本地,则将转换完成文件迁移到对应位置
if ($defaultspace['bz'] != 'dzz') {
$cloudpath = $defaultspace['bz'].':'.$defaultspace['did'] . ':/' .$target;
//组合云端保存位置
$filepath = \IO::moveThumbFile($cloudpath, 'dzz::'.$target);
if (!isset($filepath['error'])) {
@unlink($png);
return $target;
}
} else {
return $target;
}
2022-06-25 14:48:20 +08:00
}
2024-01-31 01:00:33 +08:00
} else {
runlog('onlyoffice', $conversion_url . '===' . print_r($post_data,true) . '===' . print_r($ret, true));
2022-06-25 14:48:20 +08:00
}
return false;
}
2024-01-31 01:00:33 +08:00
private function curl_get($url)
2022-06-25 14:48:20 +08:00
{
2024-01-31 01:00:33 +08:00
$ch = curl_init($url);
2022-06-25 14:48:20 +08:00
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
2024-01-31 01:00:33 +08:00
$content = curl_exec($ch);
2022-06-25 14:48:20 +08:00
curl_close($ch);
return $content;
}
2024-01-31 01:00:33 +08:00
private function getCUrl()
{
2022-06-25 14:48:20 +08:00
global $_SERVER;
2024-01-31 01:00:33 +08:00
$onlyDocumentUrl = rtrim(str_replace('web-apps/apps/api/documents/api.js', '', $this->onlyDocumentUrl), '/') . '/ConvertService.ashx';
2022-06-25 14:48:20 +08:00
return $onlyDocumentUrl;
}
2024-01-31 01:00:33 +08:00
public function getConvertUrl($posturl, $post_data)
{
2022-06-25 14:48:20 +08:00
//CURLOPT_URL 是指提交到哪里相当于表单里的“action”指定的路径
//$url = "http://local.jumei.com/DemoIndex/curl_pos/";
//$posturl.='?'.http_build_query($post_data);
$ch = curl_init();
// 设置变量
curl_setopt($ch, CURLOPT_URL, $posturl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//执行结果是否被返回0是返回1是不返回
//curl_setopt($ch, CURLOPT_HEADER, 0);//参数设置是否显示头部信息1为显示0为不显示
//伪造网页来源地址,伪造来自百度的表单提交
//curl_setopt($ch, CURLOPT_REFERER, '');
//表单数据是正规的表单设置值为非0
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
//设置curl执行超时时间最大是多少
//使用数组提供post数据时CURL组件大概是为了兼容@filename这种上传文件的写法
//默认把content_type设为了multipart/form-data。虽然对于大多数web服务器并
//没有影响,但是还是有少部分服务器不兼容。本文得出的结论是,在没有需要上传文件的
//情况下尽量对post提交的数据进行http_build_query然后发送出去能实现更好的兼容性更小的请求数据包。
curl_setopt($ch, CURLOPT_POSTFIELDS, ($post_data));
//执行并获取结果
if (!$r = curl_exec($ch)) {
2024-01-31 01:00:33 +08:00
return (array('error' => curl_error($ch)));
2022-06-25 14:48:20 +08:00
}
curl_close($ch);
2024-01-31 01:00:33 +08:00
return $ret = xml2array($r, true, 'utf-8');
2022-06-25 14:48:20 +08:00
}
}