1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
| <?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 自动下载保存远程图片
*
* @package AutoSaveImage
* @author wjh
* @version 1.0.0
* @link https://blog.wjhwjhn.com
*/
class AutoSaveImage_Plugin implements Typecho_Plugin_Interface
{
//上传文件目录(统一目录,不再按日期新建目录,查询到文件已存在时则不再处理)
const UPLOAD_DIR = '/usr/uploads/image/';
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
//下载保存远程图片
Typecho_Plugin::factory('Mirages_Plugin')->writePost = array('AutoSaveImage_Plugin', 'saveFile');
Typecho_Plugin::factory('Mirages_Plugin')->writePage = array('AutoSaveImage_Plugin', 'saveFile');
/*
如果你不用Mirages插件的话,就把上面的两行换成下面的部分
Typecho_Plugin::factory('Widget_Contents_Post_Edit')->write = array('AutoSaveImage_Plugin', 'saveFile');
Typecho_Plugin::factory('Widget_Contents_Page_Edit')->write = array('AutoSaveImage_Plugin', 'saveFile');
*/
}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate()
{
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form)
{
}
/**
* 下载保存远程图片
*
* @access public
* @param array $post 数据结构体
* @return array
*/
public static function saveFile($post)
{
$options = Typecho_Widget::widget('Widget_Options');
$siteUrl = $options->siteUrl;
$text = $post['text'];
$urls = self::getImageFromText($text);
if(!isset($urls) || count($urls) == 0) return $post;
$save_dir = self::UPLOAD_DIR;
$save_dir = parse_url($save_dir, PHP_URL_PATH);
$uploadDir = Typecho_Common::url($save_dir, __TYPECHO_ROOT_DIR__);
//创建上传目录
if (!is_dir($uploadDir))
{
if (!@mkdir($uploadDir, 0744))
{
throw new Typecho_Widget_Exception(_t('上传目录无法创建, 请手动创建安装目录下的 %s 目录, 并将它的权限设置为可写然后继续尝试', $uploadDir));
}
}
//获取相对路径
$domain = trim($_SERVER['HTTP_HOST']);
foreach ($urls as $url)
{
$tmp_url = strtolower(trim($url));
if (strpos($tmp_url, '://') === false || strpos($tmp_url, $domain) !== false) continue;
$hash = md5($url);
$array = pathinfo($url);
$ext = $array['extension'];
$filename = $hash.'.'.$ext;//生成要保存在本地的文件名
$dst_path = $uploadDir.$filename;//本地保存绝对路径
$dst_file = $save_dir.$filename;//相对保存路径
$dst_url = $siteUrl.$dst_file;//生成的本地文件url
if (!file_exists($dst_path))
{
//不存在文件则下载
self::get_remote_file($url, $dst_path);
}
if (file_exists($dst_path))
{
//有文件了,替换内容
$text = str_ireplace($url, $dst_url, $text);
}
}
$text = str_replace('!thumbnail', '', $text); //石墨外链特殊处理
$post['text'] = $text;
return $post;
}
static function getImageFromText($text)
{
// $patten = '!(http|https)://[a-z0-9\-\.\/]+\.(?:jpe?g|png|gif)!Ui';
$patten = '!(https?:\/\/.*\.(?:png|jpg|bmp|jpeg|gif|webp))!';
preg_match_all($patten, $text, $arr);
$result = [];
if(isset($arr) && count($arr) > 0)
{
foreach ($arr as $key => $value)
{
if(count($value) > 0)
{
foreach ($value as $v)
{
if(!in_array($v,$result))
$result[] = $v;
}
}
}
}
return $result;
}
/**
* 下载远程文件
*
*/
static function get_remote_file($url, $save)
{
if ( trim($save) === '' || trim($url) === '' ) return false;
$fp = fopen ($save, "wb");
if (!$fp) throw new Typecho_Widget_Exception(_t('上传目录无法写入文件:%s', $save));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_REFERER, $url);
$img = curl_exec($ch);
fwrite($fp, $img);
curl_close($ch);
fclose($fp);
return true;
}
}
|