-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.php
896 lines (829 loc) · 30.8 KB
/
Plugin.php
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
<?php
/**
* NewSmms 新SMMS图床
*
* @package NewSmms
* @author ranlychan
* @version 1.0.0
* @link https://www.ranlychan.com
* @dependence 1.0-*
* @date 2023-3-1
*/
include_once 'smms.class.php';
class NewSmmsForTypecho_Plugin extends Widget_Upload implements Typecho_Plugin_Interface
{
//上传文件目录
const UPLOAD_DIR = '/usr/uploads' ;
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
Typecho_Plugin::factory('Widget_Upload')->uploadHandle = array('NewSmmsForTypecho_Plugin', 'uploadHandle');
Typecho_Plugin::factory('Widget_Upload')->modifyHandle = array('NewSmmsForTypecho_Plugin', 'modifyHandle');
Typecho_Plugin::factory('Widget_Upload')->deleteHandle = array('NewSmmsForTypecho_Plugin', 'deleteHandle');
Typecho_Plugin::factory('Widget_Upload')->attachmentHandle = array('NewSmmsForTypecho_Plugin', 'attachmentHandle');
Typecho_Plugin::factory('Widget_Upload')->attachmentDataHandle = array('NewSmmsForTypecho_Plugin', 'attachmentDataHandle');
return _t('插件激活成功,记得设置~');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return string
*/
public static function deactivate(){
return _t('插件禁用成功');
}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
$smmsHost= new Typecho_Widget_Helper_Form_Element_Radio('smmsHost',
array( Smms::API_URL_1 => 'sm.ms', Smms::API_URL_2 => 'smms.app'),
Smms::API_URL_2, // 默认选中项
'服务域名', // radio组标题
_t('sm.ms服务国外,smms.app服务国内')); // 说明文字
$form->addInput($smmsHost);
$smmsToken = new Typecho_Widget_Helper_Form_Element_Password('smmsToken', NULL, NULL,
_t('API Token:'),
_t('去https://smms.app/注册登录账号后,点击右上角User->Dashboard,新页面点击左侧API Token,得到Secret Token 禁用插件后需要重写填写,但原来上传的图片不受影响')
);
$form->addInput($smmsToken->addRule('required',_t('您必须填写API Token'))
->addRule(array('NewSmmsForTypecho_Plugin', 'validate'), _t('验证不通过,请核对API Token是否正确')));
$smmsUploadExts = new Typecho_Widget_Helper_Form_Element_Text('smmsUploadExts', NULL,
'jpg,jpge,png,gif,svg,webp,tiff,bmp',
_t('自定义上传拓展名:'),
_t('自定义需要上传到smms图床的图片的拓展名,不在此列的将上传本地而不是图床。文件拓展名间请以英文半角逗号\',\'间隔')
);
$form->addInput($smmsUploadExts->addRule('required',_t('您必须填写自定义上传拓展名')));
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 上传文件处理函数
*
* @access public
* @param array $file 上传的文件
* @return mixed
*/
public static function uploadHandle($file)
{
// 文件名检查
if (empty($file['name'])) {
return false;
}
// 获取扩展名并检查
$ext = self::getSafeName($file['name']);
if (!self::checkFileType($ext) || Typecho_Common::isAppEngine()) {
return false;
}
//获取文件名及文件路径
$fileName = sprintf('%u', crc32(uniqid())) . '.' . $ext;
$uploadFileTemp = self::getUploadFile($file);
// 如果没有临时文件,则退出
if (!isset($uploadFileTemp)) {
return false;
}
if(self::isImgType($ext)){
// 上传到图床
try {
$curl_file = new \CURLFile(realpath($uploadFileTemp),"",$fileName);
$smms = self::smmsInit();
$data = $smms->upload($curl_file);
$path = $data['path']; // 相对路径
//返回相对存储路径,存于数据库[prefix]_contents表的text属性内
return array(
'name' => $file['name'], // 附件名称
'path' => $path, // 附件存储相对路径
'size' => $file['size'], // 附件比特大小
'type' => $ext, // 附件扩展后缀名
'mime' => self::mimeContentType($path), // mime类型
/* 下面是自定义的扩展元数据 */
'hash_algo' => 'hash', // 计算hash值所用的算法
'hash' => $data['hash'],//hash_file('md5',$uploadFileTemp), // 附件hash值
'url' => $data['url'], // 附件绝对路径
'source' => 'smms' // 附件存储位置默认本机 loc:本机, smms:这个图床
);
} catch (Exception $e) {
return false;
}
} else {
// 上传到本地
return self::pUploadHandle($file);
}
}
/**
* 修改文件处理函数
*
* @access public
* @param array $content 老文件
* @param array $file 新上传的文件
* @return mixed
*/
public static function modifyHandle($content, $file)
{
switch ($content['attachment']->source){
case "smms": // smms图床存储
return self::uploadHandle($file);
case "loc": // 本机存储
default: // 默认也为本机存储的处理方式以兼容旧的数据
return self::pModifyHandle($content, $file);
}
}
/**
* 删除文件
*
* @access public
* @param array $content 文件相关信息
* @return string
*/
public static function deleteHandle(array $content): bool
{
switch ($content['attachment']->source) {
case "smms": // smms图床存储
try {
$smms = self::smmsInit();
$hash = $content['attachment']->hash;
$result = $smms->delete($hash);
// 返回布尔值,确保与父类方法兼容
return $result ? true : false;
} catch (Exception $e) {
return false;
}
case "loc": // 本机存储
default: // 默认也为本机存储的处理方式以兼容旧的数据
// pDeleteHandle 返回值也需要确保是布尔值
return self::pDeleteHandle($content);
}
}
/**
* 获取实际文件绝对访问路径
*
* @access public
* @param array $content 单个文件的相关信息列表
* @return string
*/
public static function attachmentHandle(array $content): string
{
// 根据 attachment 的存储位置(本地还是图床等)来获取绝对路径
switch ($content['attachment']->source) {
case "smms": // smms 图床存储
// 确保返回的是字符串
return (string) $content['attachment']->url;
case "loc": // 本机存储
default: // 默认也为本机存储的处理方式以兼容旧的数据
// pAttachmentHandle 也要确保返回的是字符串
return (string) self::pAttachmentHandle($content);
}
}
/**
* 获取实际文件数据
*
* @access public
* @param array $content
* @return string
*/
public static function attachmentDataHandle(array $content): string
{
switch ($content['attachment']->source) {
case "smms": // smms 图床存储
// 使用 file_get_contents 读取文件内容,并确保返回字符串
return (string) file_get_contents($content['attachment']->url);
case "loc": // 本机存储
default: // 默认也为本机存储的处理方式以兼容旧的数据
// pAttachmentDataHandle 也要确保返回字符串
return (string) self::pAttachmentDataHandle($content);
}
}
/**
* 验证Smms Api Token
*
* @access public
*
* @return boolean
*/
public static function validate()
{
$host = Typecho_Request::getInstance()->smmsHost;
$token = Typecho_Request::getInstance()->smmsToken;
try{
$smms = new Smms($token,$host);
$_usrProfile = $smms->getUserProfile();
return !empty($_usrProfile);
}
catch(Exception $e){
return false;
}
}
/**
* 初始化Smms
*
* @access public
* @return object
*/
public static function smmsInit()
{
$options = Typecho_Widget::widget('Widget_Options')->plugin('NewSmmsForTypecho');
return new Smms($options->smmsToken, $options->smmsHost);
}
/**
* 获取上传文件
*
* @param array $file 上传的文件
* @access private
* @return string
*/
private static function getUploadFile($file)
{
return isset($file['tmp_name']) ? $file['tmp_name'] : (isset($file['bytes']) ? $file['bytes'] : (isset($file['bits']) ? $file['bits'] : ''));
}
/**
* 获取安全的文件名
*
* @param string $name
* @static
* @access private
* @return string
*/
private static function getSafeName(&$name)
{
$name = str_replace(array('"', '<', '>'), '', $name);
$name = str_replace('\\', '/', $name);
$name = false === strpos($name, '/') ? ('a' . $name) : str_replace('/', '/a', $name);
$info = pathinfo($name);
$name = substr($info['basename'], 1);
return isset($info['extension']) ? strtolower($info['extension']) : '';
}
/**
*获取文件上传目录
* @access private
* @return string
*/
private static function getUploadDir()
{
if(defined('__TYPECHO_UPLOAD_DIR__'))
{
return __TYPECHO_UPLOAD_DIR__;
}
else{
return self::UPLOAD_DIR;
}
}
/**
*获取文件Mime类型,处理掉异常
* @access private
* @return string
*/
private static function mimeContentType($fileName){
//TODO:避免该方法
//避免Typecho mime-content-type引发的异常
/*异常详情:
*Warning</b>: mime_content_type(/path/filename.jpg) [<a href='function.mime-content-type'>function.mime-content-type</a>]:
*failed to open stream: No such file or directory in <b>/webroot/var/Typecho/Common.php</b> on line <b>1058</b>
*/
@$mime = Typecho_Common::mimeContentType($fileName);
if(!$mime){
return self::getMime($fileName);
}
else{
return $mime;
}
}
/**
*获取文件Mime类型
* @access private
* @return string
*/
private static function getMime($fileName){
$mimeTypes = array(
'ez' => 'application/andrew-inset',
'csm' => 'application/cu-seeme',
'cu' => 'application/cu-seeme',
'tsp' => 'application/dsptype',
'spl' => 'application/x-futuresplash',
'hta' => 'application/hta',
'cpt' => 'image/x-corelphotopaint',
'hqx' => 'application/mac-binhex40',
'nb' => 'application/mathematica',
'mdb' => 'application/msaccess',
'doc' => 'application/msword',
'dot' => 'application/msword',
'bin' => 'application/octet-stream',
'oda' => 'application/oda',
'ogg' => 'application/ogg',
'prf' => 'application/pics-rules',
'key' => 'application/pgp-keys',
'pdf' => 'application/pdf',
'pgp' => 'application/pgp-signature',
'ps' => 'application/postscript',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'rss' => 'application/rss+xml',
'rtf' => 'text/rtf',
'smi' => 'application/smil',
'smil' => 'application/smil',
'wp5' => 'application/wordperfect5.1',
'xht' => 'application/xhtml+xml',
'xhtml' => 'application/xhtml+xml',
'zip' => 'application/zip',
'cdy' => 'application/vnd.cinderella',
'mif' => 'application/x-mif',
'xls' => 'application/vnd.ms-excel',
'xlb' => 'application/vnd.ms-excel',
'cat' => 'application/vnd.ms-pki.seccat',
'stl' => 'application/vnd.ms-pki.stl',
'ppt' => 'application/vnd.ms-powerpoint',
'pps' => 'application/vnd.ms-powerpoint',
'pot' => 'application/vnd.ms-powerpoint',
'sdc' => 'application/vnd.stardivision.calc',
'sda' => 'application/vnd.stardivision.draw',
'sdd' => 'application/vnd.stardivision.impress',
'sdp' => 'application/vnd.stardivision.impress',
'smf' => 'application/vnd.stardivision.math',
'sdw' => 'application/vnd.stardivision.writer',
'vor' => 'application/vnd.stardivision.writer',
'sgl' => 'application/vnd.stardivision.writer-global',
'sxc' => 'application/vnd.sun.xml.calc',
'stc' => 'application/vnd.sun.xml.calc.template',
'sxd' => 'application/vnd.sun.xml.draw',
'std' => 'application/vnd.sun.xml.draw.template',
'sxi' => 'application/vnd.sun.xml.impress',
'sti' => 'application/vnd.sun.xml.impress.template',
'sxm' => 'application/vnd.sun.xml.math',
'sxw' => 'application/vnd.sun.xml.writer',
'sxg' => 'application/vnd.sun.xml.writer.global',
'stw' => 'application/vnd.sun.xml.writer.template',
'sis' => 'application/vnd.symbian.install',
'wbxml' => 'application/vnd.wap.wbxml',
'wmlc' => 'application/vnd.wap.wmlc',
'wmlsc' => 'application/vnd.wap.wmlscriptc',
'wk' => 'application/x-123',
'dmg' => 'application/x-apple-diskimage',
'bcpio' => 'application/x-bcpio',
'torrent' => 'application/x-bittorrent',
'cdf' => 'application/x-cdf',
'vcd' => 'application/x-cdlink',
'pgn' => 'application/x-chess-pgn',
'cpio' => 'application/x-cpio',
'csh' => 'text/x-csh',
'deb' => 'application/x-debian-package',
'dcr' => 'application/x-director',
'dir' => 'application/x-director',
'dxr' => 'application/x-director',
'wad' => 'application/x-doom',
'dms' => 'application/x-dms',
'dvi' => 'application/x-dvi',
'pfa' => 'application/x-font',
'pfb' => 'application/x-font',
'gsf' => 'application/x-font',
'pcf' => 'application/x-font',
'pcf.Z' => 'application/x-font',
'gnumeric' => 'application/x-gnumeric',
'sgf' => 'application/x-go-sgf',
'gcf' => 'application/x-graphing-calculator',
'gtar' => 'application/x-gtar',
'tgz' => 'application/x-gtar',
'taz' => 'application/x-gtar',
'gz' => 'application/x-gtar',
'hdf' => 'application/x-hdf',
'phtml' => 'application/x-httpd-php',
'pht' => 'application/x-httpd-php',
'php' => 'application/x-httpd-php',
'phps' => 'application/x-httpd-php-source',
'php3' => 'application/x-httpd-php3',
'php3p' => 'application/x-httpd-php3-preprocessed',
'php4' => 'application/x-httpd-php4',
'ica' => 'application/x-ica',
'ins' => 'application/x-internet-signup',
'isp' => 'application/x-internet-signup',
'iii' => 'application/x-iphone',
'jar' => 'application/x-java-archive',
'jnlp' => 'application/x-java-jnlp-file',
'ser' => 'application/x-java-serialized-object',
'class' => 'application/x-java-vm',
'js' => 'application/x-javascript',
'chrt' => 'application/x-kchart',
'kil' => 'application/x-killustrator',
'kpr' => 'application/x-kpresenter',
'kpt' => 'application/x-kpresenter',
'skp' => 'application/x-koan',
'skd' => 'application/x-koan',
'skt' => 'application/x-koan',
'skm' => 'application/x-koan',
'ksp' => 'application/x-kspread',
'kwd' => 'application/x-kword',
'kwt' => 'application/x-kword',
'latex' => 'application/x-latex',
'lha' => 'application/x-lha',
'lzh' => 'application/x-lzh',
'lzx' => 'application/x-lzx',
'frm' => 'application/x-maker',
'maker' => 'application/x-maker',
'frame' => 'application/x-maker',
'fm' => 'application/x-maker',
'fb' => 'application/x-maker',
'book' => 'application/x-maker',
'fbdoc' => 'application/x-maker',
'wmz' => 'application/x-ms-wmz',
'wmd' => 'application/x-ms-wmd',
'com' => 'application/x-msdos-program',
'exe' => 'application/x-msdos-program',
'bat' => 'application/x-msdos-program',
'dll' => 'application/x-msdos-program',
'msi' => 'application/x-msi',
'nc' => 'application/x-netcdf',
'pac' => 'application/x-ns-proxy-autoconfig',
'nwc' => 'application/x-nwc',
'o' => 'application/x-object',
'oza' => 'application/x-oz-application',
'pl' => 'application/x-perl',
'pm' => 'application/x-perl',
'p7r' => 'application/x-pkcs7-certreqresp',
'crl' => 'application/x-pkcs7-crl',
'qtl' => 'application/x-quicktimeplayer',
'rpm' => 'audio/x-pn-realaudio-plugin',
'shar' => 'application/x-shar',
'swf' => 'application/x-shockwave-flash',
'swfl' => 'application/x-shockwave-flash',
'sh' => 'text/x-sh',
'sit' => 'application/x-stuffit',
'sv4cpio' => 'application/x-sv4cpio',
'sv4crc' => 'application/x-sv4crc',
'tar' => 'application/x-tar',
'tcl' => 'text/x-tcl',
'tex' => 'text/x-tex',
'gf' => 'application/x-tex-gf',
'pk' => 'application/x-tex-pk',
'texinfo' => 'application/x-texinfo',
'texi' => 'application/x-texinfo',
'~' => 'application/x-trash',
'%' => 'application/x-trash',
'bak' => 'application/x-trash',
'old' => 'application/x-trash',
'sik' => 'application/x-trash',
't' => 'application/x-troff',
'tr' => 'application/x-troff',
'roff' => 'application/x-troff',
'man' => 'application/x-troff-man',
'me' => 'application/x-troff-me',
'ms' => 'application/x-troff-ms',
'ustar' => 'application/x-ustar',
'src' => 'application/x-wais-source',
'wz' => 'application/x-wingz',
'crt' => 'application/x-x509-ca-cert',
'fig' => 'application/x-xfig',
'au' => 'audio/basic',
'snd' => 'audio/basic',
'mid' => 'audio/midi',
'midi' => 'audio/midi',
'kar' => 'audio/midi',
'mpga' => 'audio/mpeg',
'mpega' => 'audio/mpeg',
'mp2' => 'audio/mpeg',
'mp3' => 'audio/mpeg',
'm3u' => 'audio/x-mpegurl',
'sid' => 'audio/prs.sid',
'aif' => 'audio/x-aiff',
'aiff' => 'audio/x-aiff',
'aifc' => 'audio/x-aiff',
'gsm' => 'audio/x-gsm',
'wma' => 'audio/x-ms-wma',
'wax' => 'audio/x-ms-wax',
'ra' => 'audio/x-realaudio',
'rm' => 'audio/x-pn-realaudio',
'ram' => 'audio/x-pn-realaudio',
'pls' => 'audio/x-scpls',
'sd2' => 'audio/x-sd2',
'wav' => 'audio/x-wav',
'pdb' => 'chemical/x-pdb',
'xyz' => 'chemical/x-xyz',
'bmp' => 'image/x-ms-bmp',
'gif' => 'image/gif',
'ief' => 'image/ief',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'pcx' => 'image/pcx',
'png' => 'image/png',
'svg' => 'image/svg+xml',
'svgz' => 'image/svg+xml',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'wbmp' => 'image/vnd.wap.wbmp',
'ras' => 'image/x-cmu-raster',
'cdr' => 'image/x-coreldraw',
'pat' => 'image/x-coreldrawpattern',
'cdt' => 'image/x-coreldrawtemplate',
'djvu' => 'image/x-djvu',
'djv' => 'image/x-djvu',
'ico' => 'image/x-icon',
'art' => 'image/x-jg',
'jng' => 'image/x-jng',
'psd' => 'image/x-photoshop',
'pnm' => 'image/x-portable-anymap',
'pbm' => 'image/x-portable-bitmap',
'pgm' => 'image/x-portable-graymap',
'ppm' => 'image/x-portable-pixmap',
'rgb' => 'image/x-rgb',
'xbm' => 'image/x-xbitmap',
'xpm' => 'image/x-xpixmap',
'xwd' => 'image/x-xwindowdump',
'igs' => 'model/iges',
'iges' => 'model/iges',
'msh' => 'model/mesh',
'mesh' => 'model/mesh',
'silo' => 'model/mesh',
'wrl' => 'x-world/x-vrml',
'vrml' => 'x-world/x-vrml',
'csv' => 'text/comma-separated-values',
'css' => 'text/css',
'323' => 'text/h323',
'htm' => 'text/html',
'html' => 'text/html',
'uls' => 'text/iuls',
'mml' => 'text/mathml',
'asc' => 'text/plain',
'txt' => 'text/plain',
'text' => 'text/plain',
'diff' => 'text/plain',
'rtx' => 'text/richtext',
'sct' => 'text/scriptlet',
'wsc' => 'text/scriptlet',
'tm' => 'text/texmacs',
'ts' => 'text/texmacs',
'tsv' => 'text/tab-separated-values',
'jad' => 'text/vnd.sun.j2me.app-descriptor',
'wml' => 'text/vnd.wap.wml',
'wmls' => 'text/vnd.wap.wmlscript',
'xml' => 'text/xml',
'xsl' => 'text/xml',
'h++' => 'text/x-c++hdr',
'hpp' => 'text/x-c++hdr',
'hxx' => 'text/x-c++hdr',
'hh' => 'text/x-c++hdr',
'c++' => 'text/x-c++src',
'cpp' => 'text/x-c++src',
'cxx' => 'text/x-c++src',
'cc' => 'text/x-c++src',
'h' => 'text/x-chdr',
'c' => 'text/x-csrc',
'java' => 'text/x-java',
'moc' => 'text/x-moc',
'p' => 'text/x-pascal',
'pas' => 'text/x-pascal',
'***' => 'text/x-pcs-***',
'shtml' => 'text/x-server-parsed-html',
'etx' => 'text/x-setext',
'tk' => 'text/x-tcl',
'ltx' => 'text/x-tex',
'sty' => 'text/x-tex',
'cls' => 'text/x-tex',
'vcs' => 'text/x-vcalendar',
'vcf' => 'text/x-vcard',
'dl' => 'video/dl',
'fli' => 'video/fli',
'gl' => 'video/gl',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpe' => 'video/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'mxu' => 'video/vnd.mpegurl',
'dif' => 'video/x-dv',
'dv' => 'video/x-dv',
'lsf' => 'video/x-la-asf',
'lsx' => 'video/x-la-asf',
'mng' => 'video/x-mng',
'asf' => 'video/x-ms-asf',
'asx' => 'video/x-ms-asf',
'wm' => 'video/x-ms-wm',
'wmv' => 'video/x-ms-wmv',
'wmx' => 'video/x-ms-wmx',
'wvx' => 'video/x-ms-wvx',
'avi' => 'video/x-msvideo',
'movie' => 'video/x-sgi-movie',
'ice' => 'x-conference/x-cooltalk',
'vrm' => 'x-world/x-vrml',
'rar' => 'application/x-rar-compressed',
'cab' => 'application/vnd.ms-cab-compressed'
);
$part = explode('.', $fileName);
$size = count($part);
if ($size > 1) {
$ext = $part[$size - 1];
if (isset($mimeTypes[$ext])) {
return $mimeTypes[$ext];
}
}
return 'application/octet-stream';
}
/**
* 检查文件拓展是否为常见图片
*
* @access private
* @param string $ext 扩展名
* @return boolean
*/
public static function isImgType($ext){
try {
$options = Typecho_Widget::widget('Widget_Options')->plugin('NewSmmsForTypecho');
$smmsUploadExtsArr = explode(',',strtolower($options->smmsUploadExts));
return in_array(strtolower($ext), $smmsUploadExtsArr);
} catch (Exception $e) {
try {
return in_array(strtolower($ext), array('jpg','jpge','png','gif','svg','webp','tiff','bmp'));
} catch (Exception $e) {
return false;
}
}
}
/**
* 上传文件处理函数,如果需要实现自己的文件哈希或者特殊的文件系统,请在options表里把uploadHandle改成自己的函数
*
* @access public
* @param array $file 上传的文件
* @return mixed
*/
public static function pUploadHandle($file)
{
if (empty($file['name'])) {
return false;
}
$ext = self::getSafeName($file['name']);
if (!self::checkFileType($ext) || Typecho_Common::isAppEngine()) {
return false;
}
$date = new Typecho_Date();
$path = Typecho_Common::url(defined('__TYPECHO_UPLOAD_DIR__') ? __TYPECHO_UPLOAD_DIR__ : self::UPLOAD_DIR,
defined('__TYPECHO_UPLOAD_ROOT_DIR__') ? __TYPECHO_UPLOAD_ROOT_DIR__ : __TYPECHO_ROOT_DIR__)
. '/' . $date->year . '/' . $date->month;
//创建上传目录
if (!is_dir($path)) {
if (!self::makeUploadDir($path)) {
return false;
}
}
//获取文件名
$fileName = sprintf('%u', crc32(uniqid())) . '.' . $ext;
$path = $path . '/' . $fileName;
if (isset($file['tmp_name'])) {
//移动上传文件
if (!@move_uploaded_file($file['tmp_name'], $path)) {
return false;
}
} else if (isset($file['bytes'])) {
//直接写入文件
if (!file_put_contents($path, $file['bytes'])) {
return false;
}
} else {
return false;
}
if (!isset($file['size'])) {
$file['size'] = filesize($path);
}
//返回相对存储路径
return array(
'name' => $file['name'],
'path' => (defined('__TYPECHO_UPLOAD_DIR__') ? __TYPECHO_UPLOAD_DIR__ : self::UPLOAD_DIR)
. '/' . $date->year . '/' . $date->month . '/' . $fileName,
'size' => $file['size'],
'type' => $ext,
'mime' => Typecho_Common::mimeContentType($path)
);
}
/**
* 修改文件处理函数,如果需要实现自己的文件哈希或者特殊的文件系统,请在options表里把modifyHandle改成自己的函数
*
* @access public
* @param array $content 老文件
* @param array $file 新上传的文件
* @return mixed
*/
public static function pModifyHandle($content, $file)
{
if (empty($file['name'])) {
return false;
}
$ext = self::getSafeName($file['name']);
if ($content['attachment']->type != $ext || Typecho_Common::isAppEngine()) {
return false;
}
$path = Typecho_Common::url($content['attachment']->path,
defined('__TYPECHO_UPLOAD_ROOT_DIR__') ? __TYPECHO_UPLOAD_ROOT_DIR__ : __TYPECHO_ROOT_DIR__);
$dir = dirname($path);
//创建上传目录
if (!is_dir($dir)) {
if (!self::makeUploadDir($dir)) {
return false;
}
}
if (isset($file['tmp_name'])) {
@unlink($path);
//移动上传文件
if (!@move_uploaded_file($file['tmp_name'], $path)) {
return false;
}
} else if (isset($file['bytes'])) {
@unlink($path);
//直接写入文件
if (!file_put_contents($path, $file['bytes'])) {
return false;
}
} else {
return false;
}
if (!isset($file['size'])) {
$file['size'] = filesize($path);
}
//返回相对存储路径
return array(
'name' => $content['attachment']->name,
'path' => $content['attachment']->path,
'size' => $file['size'],
'type' => $content['attachment']->type,
'mime' => $content['attachment']->mime
);
}
/**
* 删除文件
*
* @access public
* @param array $content 文件相关信息
* @return string
*/
public static function pDeleteHandle(array $content)
{
return !Typecho_Common::isAppEngine()
&& @unlink(__TYPECHO_ROOT_DIR__ . '/' . $content['attachment']->path);
}
/**
* 获取实际文件绝对访问路径
*
* @access public
* @param array $content 文件相关信息
* @return string
*/
public static function pAttachmentHandle(array $content)
{
$options = Typecho_Widget::widget('Widget_Options');
return Typecho_Common::url($content['attachment']->path,
defined('__TYPECHO_UPLOAD_URL__') ? __TYPECHO_UPLOAD_URL__ : $options->siteUrl);
}
/**
* 获取实际文件数据
*
* @access public
* @param array $content
* @return string
*/
public static function pAttachmentDataHandle(array $content)
{
return file_get_contents(Typecho_Common::url($content['attachment']->path,
defined('__TYPECHO_UPLOAD_ROOT_DIR__') ? __TYPECHO_UPLOAD_ROOT_DIR__ : __TYPECHO_ROOT_DIR__));
}
/**
* 创建上传路径
*
* @access protected
* @param string $path 路径
* @return boolean
*/
protected static function makeUploadDir($path)
{
$path = preg_replace("/\\\+/", '/', $path);
$current = rtrim($path, '/');
$last = $current;
while (!is_dir($current) && false !== strpos($path, '/')) {
$last = $current;
$current = dirname($current);
}
if ($last == $current) {
return true;
}
if (!@mkdir($last)) {
return false;
}
$stat = @stat($last);
$perms = $stat['mode'] & 0007777;
@chmod($last, $perms);
return self::makeUploadDir($path);
}
}