Ueditor.php
47.3 KB
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
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
<?php
/**
* tpshop
* ============================================================================
* 版权所有 2015-2027 深圳搜豹网络科技有限公司,并保留所有权利。
* 网站地址: http://www.tp-shop.cn
* ----------------------------------------------------------------------------
* 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
* 不允许对程序代码以任何形式任何目的的再发布。
* ============================================================================
* Author: 当燃
* Date: 2015-09-17
*/
namespace app\admin\controller;
use qcloudcos\Conf;
use qcloudcos\Myqcloudcos;
use common\util\File;
use think\log;
use think\Image;
use think\Request;
use think\Validate;
/**
* Class UeditorController
* @package Admin\Controller
*/
class Ueditor extends Base
{
private $sub_name = array('date', 'Y/m-d');
private $savePath = 'temp/';
private $savePath1 = '';
public function __construct()
{
parent::__construct();
date_default_timezone_set("Asia/Shanghai");
$this->savePath = I('GET.savepath','temp').'/';
$this->savePath1 = I('GET.savepath1','');
error_reporting(E_ERROR | E_WARNING);
}
public function index(){
$CONFIG2 = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents("./public/plugins/Ueditor/php/config.json")), true);
$action = $_GET['action'];
switch ($action) {
case 'config':
$result = json_encode($CONFIG2);
break;
/* 上传图片 */
case 'uploadimage':
$fieldName = $CONFIG2['imageFieldName'];
$result = $this->upFile($fieldName);
break;
/* 上传涂鸦 */
case 'uploadscrawl':
$config = array(
"pathFormat" => $CONFIG2['scrawlPathFormat'],
"maxSize" => $CONFIG2['scrawlMaxSize'],
"allowFiles" => $CONFIG2['scrawlAllowFiles'],
"oriName" => "scrawl.png"
);
$fieldName = $CONFIG2['scrawlFieldName'];
$base64 = "base64";
$result = $this->upBase64($config,$fieldName);
break;
/* 上传视频 */
case 'uploadvideo':
$fieldName = $CONFIG2['videoFieldName'];
$result = $this->upFile($fieldName);
break;
/* 上传文件 */
case 'uploadfile':
$fieldName = $CONFIG2['fileFieldName'];
$result = $this->upFile($fieldName);
break;
/* 列出图片 */
case 'listimage':
$allowFiles = $CONFIG2['imageManagerAllowFiles'];
$listSize = $CONFIG2['imageManagerListSize'];
$path = $CONFIG2['imageManagerListPath'];
$get =$_GET;
$result =$this->fileList($allowFiles,$listSize,$get);
break;
/* 列出文件 */
case 'listfile':
$allowFiles = $CONFIG2['fileManagerAllowFiles'];
$listSize = $CONFIG2['fileManagerListSize'];
$path = $CONFIG2['fileManagerListPath'];
$get = $_GET;
$result = $this->fileList($allowFiles,$listSize,$get);
break;
/* 抓取远程文件 */
case 'catchimage':
$config = array(
"pathFormat" => $CONFIG2['catcherPathFormat'],
"maxSize" => $CONFIG2['catcherMaxSize'],
"allowFiles" => $CONFIG2['catcherAllowFiles'],
"oriName" => "remote.png"
);
$fieldName = $CONFIG2['catcherFieldName'];
/* 抓取远程图片 */
$list = array();
isset($_POST[$fieldName]) ? $source = $_POST[$fieldName] : $source = $_GET[$fieldName];
foreach($source as $imgUrl){
$info = json_decode($this->saveRemote($config,$imgUrl),true);
array_push($list, array(
"state" => $info["state"],
"url" => $info["url"],
"size" => $info["size"],
"title" => htmlspecialchars($info["title"]),
"original" => htmlspecialchars($info["original"]),
"source" => htmlspecialchars($imgUrl)
));
}
$result = json_encode(array(
'state' => count($list) ? 'SUCCESS':'ERROR',
'list' => $list
));
break;
default:
$result = json_encode(array(
'state' => '请求地址出错'
));
break;
}
/* 输出结果 */
if(isset($_GET["callback"])){
if(preg_match("/^[\w_]+$/", $_GET["callback"])){
echo htmlspecialchars($_GET["callback"]).'('.$result.')';
}else{
echo json_encode(array(
'state' => 'callback参数不合法'
));
}
}else{
echo $result;
}
}
public function getContent()
{
echo '<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<script src="/public/plugins/Ueditor/ueditor.parse.js" type="text/javascript"></script>
<script>' . " uParse('.content',{
'highlightJsUrl':'/public/plugins/Ueditor/third-party/SyntaxHighlighter/shCore.js',
'highlightCssUrl':/public/plugins/Ueditor/third-party/SyntaxHighlighter/shCoreDefault.css'
})</script>";
$myEditor = $this->request->param('myEditor');
$content = htmlspecialchars(stripslashes($myEditor));
echo "<div class='content'>" . htmlspecialchars_decode($content) . "</div>";
}
/***
public function fileUp()
{
$config = array(
"savePath" => 'File/',
"maxSize" => 20000000, // 单位B
"exts" => explode(",", 'zip,rar,doc,docx,zip,pdf,txt,ppt,pptx,xls,xlsx'),
"subName" => $this->sub_name,
);
$upload = new Upload($config);
$info = $upload->upload();
if ($info) {
$state = "SUCCESS";
} else {
$state = "ERROR" . $upload->getError();
}
$return_data['url'] = $info['upfile']['urlpath'];
$return_data['fileType'] = $info['upfile']['ext'];
$return_data['original'] = $info['upfile']['name'];
$return_data['state'] = $state;
$this->ajaxReturn($return_data,'JSON');
}***/
//上传文件
private function upFile($fieldName){
$file = request()->file('file');
if(empty($file)){
$file = request()->file('upfile');
}
$result = true;
if (true !== $result || empty($file)) {
$state = "ERROR" . $result;
return json_encode(['state' =>$state]);
}else{
// 移动到框架应用根目录/public/uploads/ 目录下
$this->savePath = $this->savePath.date('Y').'/'.date('m-d').'/';
// 使用自定义的文件保存规则
$info = $file->rule(function ($file) {
return md5(mt_rand());
})->move('public/upload/'.$this->savePath);
//保存到存储云
vendor('qcloudcos.myqcloudcos');
$resfolder=Myqcloudcos::statFolder('wxd',UPLOAD_PATH. $this->savePath);
if ($resfolder && $resfolder['code']!=0)//不存在创建
{
Myqcloudcos::createFolder('wxd',UPLOAD_PATH. $this->savePath);
}
//上传到腾讯云
$localpath=ROOT_PATH.'/public/upload/'.$this->savePath.$info->getSaveName();
$ypath='/'.UPLOAD_PATH.$this->savePath.$info->getSaveName();
$res=Myqcloudcos::upload('wxd',$localpath,$ypath);
if($res && $res['code']==0){
}
}
if($info){
$data = array(
'state' => 'SUCCESS',
'url' => QCLOUD_IMGURL.'/public/upload/'.$this->savePath.$info->getSaveName(),
'title' => $info->getFilename(),
'original' => $info->getFilename(),
'type' => '.' . $info->getExtension(),
'size' => $info->getSize(),
);
}else{
$data = array('state' => 'ERROR'.$file->getError());
}
return json_encode($data);
}
/**
* 获取远程图片
*/
public function getRemoteImage()
{
header("Content-Type: text/html; charset=utf-8");
//远程抓取图片配置
$config = array(
"savePath" => UPLOAD_PATH . 'remote/' . date('Y') . '/' . date('m') . '/', //保存路径
"allowFiles" => array(".gif", ".png", ".jpg", ".jpeg", ".bmp"), //文件允许格式
"maxSize" => 20000000,
);
$upfile = $this->request->param('upfile');
$uri = htmlspecialchars($upfile);
$uri = str_replace("&", "&", $uri);
$this->getRemoteImage2($uri, $config);
}
//抓取远程图片
private function saveRemote($config,$fieldName){
$imgUrl = htmlspecialchars($fieldName);
$imgUrl = str_replace("&","&",$imgUrl);
//http开头验证
if(strpos($imgUrl,"http") !== 0){
$data=array(
'state' => '链接不是http链接',
);
return json_encode($data);
}
//获取请求头并检测死链
$heads = get_headers($imgUrl);
if(!(stristr($heads[0],"200") && stristr($heads[0],"OK"))){
$data=array(
'state' => '链接不可用',
);
return json_encode($data);
}
//格式验证(扩展名验证和Content-Type验证)
$fileType = strtolower(strrchr($imgUrl,'.'));
if(!in_array($fileType,$config['allowFiles']) || stristr($heads['Content-Type'],"image")){
$data=array(
'state' => '链接contentType不正确',
);
return json_encode($data);
}
//打开输出缓冲区并获取远程图片
ob_start();
$context = stream_context_create(
array('http' => array(
'follow_location' => false // don't follow redirects
))
);
readfile($imgUrl,false,$context);
$img = ob_get_contents();
ob_end_clean();
preg_match("/[\/]([^\/]*)[\.]?[^\.\/]*$/",$imgUrl,$m);
$dirname = './public/upload/remote/';
$file['oriName'] = $m ? $m[1] : "";
$file['filesize'] = strlen($img);
$file['ext'] = strtolower(strrchr($config['oriName'],'.'));
$file['name'] = uniqid().$file['ext'];
$file['fullName'] = $dirname.$file['name'];
$fullName = $file['fullName'];
$fname='/public/upload/remote/'.$file['name'];
//检查文件大小是否超出限制
if($file['filesize'] >= ($config["maxSize"])){
$data=array(
'state' => '文件大小超出网站限制',
);
return json_encode($data);
}
//创建目录失败
if(!file_exists($dirname) && !mkdir($dirname,0777,true)){
$data=array(
'state' => '目录创建失败',
);
return json_encode($data);
}else if(!is_writeable($dirname)){
$data=array(
'state' => '目录没有写权限',
);
return json_encode($data);
}
//移动文件
if(!(file_put_contents($fullName, $img) && file_exists($fullName))){ //移动失败
$data=array(
'state' => '写入文件内容错误',
);
return json_encode($data);
}else{
//保存到存储云
vendor('qcloudcos.myqcloudcos');
$resfolder=Myqcloudcos::statFolder('wxd',UPLOAD_PATH. $this->savePath);
if ($resfolder && $resfolder['code']!=0)//不存在创建
{
Myqcloudcos::createFolder('wxd',UPLOAD_PATH. $this->savePath);
}
//上传到腾讯云
$localpath=ROOT_PATH.$fname;
$ypath='/'.UPLOAD_PATH.$fname;
$res=Myqcloudcos::upload('wxd',$localpath,$ypath);
//移动成功
$data=array(
'state' => 'SUCCESS',
'url' => "https://mshopimg.yolipai.net".$fname,
'title' => $file['name'],
'original' => $file['oriName'],
'type' => $file['ext'],
'size' => $file['filesize'],
);
}
return json_encode($data);
}
/**
* 远程抓取
* @param $uri
* @param $config
*/
public function getRemoteImage2($uri, $config)
{
//忽略抓取时间限制
set_time_limit(0);
//ue_separate_ue ue用于传递数据分割符号
$imgUrls = explode("ue_separate_ue", $uri);
$tmpNames = array();
foreach ($imgUrls as $imgUrl) {
//http开头验证
if (strpos($imgUrl, "http") !== 0) {
array_push($tmpNames, "https error");
continue;
}
//sae环境 不兼容
if (!defined('SAE_TMP_PATH')) {
//获取请求头
$heads = get_headers($imgUrl);
//死链检测
if (!(stristr($heads[0], "200") && stristr($heads[0], "OK"))) {
array_push($tmpNames, "get_headers error");
continue;
}
}
//格式验证(扩展名验证和Content-Type验证)
$fileType = strtolower(strrchr($imgUrl, '.'));
if (!in_array($fileType, $config['allowFiles']) || stristr($heads['Content-Type'], "image")) {
array_push($tmpNames, "Content-Type error");
continue;
}
//打开输出缓冲区并获取远程图片
ob_start();
$context = stream_context_create(
array(
'http' => array(
'follow_location' => false // don't follow redirects
)
)
);
//请确保php.ini中的fopen wrappers已经激活
readfile($imgUrl, false, $context);
$img = ob_get_contents();
ob_end_clean();
//大小验证
$uriSize = strlen($img); //得到图片大小
$allowSize = 1024 * $config['maxSize'];
if ($uriSize > $allowSize) {
array_push($tmpNames, "maxSize error");
continue;
}
$savePath = $config['savePath'];
if (!defined('SAE_TMP_PATH')) {
//非SAE
//创建保存位置
if (!file_exists($savePath)) {
mkdir($savePath, 0777, true);
}
//写入文件
$tmpName = $savePath . rand(1, 10000) . time() . strrchr($imgUrl, '.');
try {
File::writeFile($tmpName, $img, "a");
array_push($tmpNames, '/' . $tmpName);
} catch (\Exception $e) {
array_push($tmpNames, "error");
}
} else {
//SAE
$Storage = new \SaeStorage();
$domain = C('SaeStorage');
$destFileName = 'remote/' . date('Y') . '/' . date('m') . '/' . rand(1, 10000) . time() . strrchr($imgUrl, '.');
$result = $Storage->write($domain, $destFileName, $img, -1);
Log::write('$destFileName:' . $destFileName);
if ($result) {
array_push($tmpNames, $result);
} else {
array_push($tmpNames, "not supported");
}
}
}
/**
* 返回数据格式
* {
* 'url' : '新地址一ue_separate_ue新地址二ue_separate_ue新地址三',
* 'srcUrl': '原始地址一ue_separate_ue原始地址二ue_separate_ue原始地址三',
* 'tip' : '状态提示'
* }
*/
$return_data['url'] = implode("ue_separate_ue", $tmpNames);
$return_data['tip'] = '远程图片抓取成功!';
$return_data['srcUrl'] = $uri;
$this->ajaxReturn($return_data);
}
/**
* 无需移植
* @function getMovie
*/
public function getMovie()
{
$key = C("tudouSearchKey");
$type = I('post.videoType');
$html = file_get_contents('http://api.tudou.com/v3/gw?method=item.search&appKey=myKey&format=json&kw=' .
$key . '&pageNo=1&pageSize=20&channelId=' . $type . '&inDays=7&media=v&sort=s');
echo $html;
}
/**
* @function imageManager
*/
public function imageManager()
{
header("Content-Type: text/html; charset=utf-8");
//需要遍历的目录列表,最好使用缩略图地址,否则当网速慢时可能会造成严重的延时
$paths = array(UPLOAD_PATH.$this->savePath."/".$this->savePath1."/");
$action = $this->request->param('action');
$action = htmlspecialchars($action);
if ($action == "get") {
if (!defined('SAE_TMP_PATH')) {
$files = array();
include_once 'application/common/util/File.class.php';
foreach ($paths as $path) {
$tmp = File::getFiles($path);
if ($tmp) {
$files = array_merge($files, $tmp);
}
}
if (!count($files)) return;
rsort($files, SORT_STRING);
$str = "";
foreach ($files as $file) {
$str .= '/' . $file . "ue_separate_ue";
}
echo $str;
} else {
// SAE环境下
$st = new \SaeStorage(); // 实例化
/*
* getList:获取指定domain下的文件名列表
* return: 执行成功时返回文件列表数组,否则返回false
* 参数:存储域,路径前缀,返回条数,起始条数
*/
$num = 0;
while ($ret = $st->getList(C('SaeStorage'), null, 100, $num)) {
foreach ($ret as $file) {
if (preg_match("/\.(gif|jpeg|jpg|png|bmp)$/i", $file))
echo $st->getUrl('upload', $file) . "ue_separate_ue";
$num++;
}
}
}
}
}
/*
* 处理base64编码的图片上传
* 例如:涂鸦图片上传
*/
private function upBase64($config,$fieldName){
$base64Data = $_POST[$fieldName];
$img = base64_decode($base64Data);
$dirname = './public/upload/scrawl/';
$file['filesize'] = strlen($img);
$file['oriName'] = $config['oriName'];
$file['ext'] = strtolower(strrchr($config['oriName'],'.'));
$file['name'] = uniqid().$file['ext'];
$file['fullName'] = $dirname.$file['name'];
$fullName = $file['fullName'];
//检查文件大小是否超出限制
if($file['filesize'] >= ($config["maxSize"])){
$data=array(
'state' => '文件大小超出网站限制',
);
return json_encode($data);
}
//创建目录失败
if(!file_exists($dirname) && !mkdir($dirname,0777,true)){
$data=array(
'state' => '目录创建失败',
);
return json_encode($data);
}else if(!is_writeable($dirname)){
$data=array(
'state' => '目录没有写权限',
);
return json_encode($data);
}
//移动文件
if(!(file_put_contents($fullName, $img) && file_exists($fullName))){ //移动失败
$data=array(
'state' => '写入文件内容错误',
);
}else{
//保存到存储云
vendor('qcloudcos.myqcloudcos');
$resfolder=Myqcloudcos::statFolder('wxd',UPLOAD_PATH. $this->savePath);
if ($resfolder && $resfolder['code']!=0)//不存在创建
{
Myqcloudcos::createFolder('wxd',UPLOAD_PATH. $this->savePath);
}
//上传到腾讯云
$localpath=ROOT_PATH.$fname;
$ypath='/'.UPLOAD_PATH.$fname;
$res=Myqcloudcos::upload('wxd',$localpath,$ypath);
//移动成功
$data=array(
'state' => 'SUCCESS',
'url' => "https://mshopimg.yolipai.net".substr($file['fullName'],1),
'title' => $file['name'],
'original' => $file['oriName'],
'type' => $file['ext'],
'size' => $file['filesize'],
);
}
return json_encode($data);
}
/**
* @function imageUp
*/
public function imageUp()
{
// 上传图片框中的描述表单名称,
$pictitle = I('pictitle');
$dir = I('dir');
$title = htmlspecialchars($pictitle , ENT_QUOTES);
$path = htmlspecialchars($dir, ENT_QUOTES);
$tablename=I('tablename');
$indexid=I('indexid');
$imgname=I('imgname');
$curid=I('curid');
$saveFileName="";
$addoldimg=I('addoldimg');
//$input_file ['upfile'] = $info['Filedata']; 一个是上传插件里面来的, 另外一个是文章编辑器里面来的
// 获取表单上传文件
$file = request()->file('Filedata');
if(empty($file))
$file = request()->file('file'); //图库-新
if(empty($file))
$file = request()->file('upfile');
$result = $this->validate(
['file2' => $file],
['file2'=>'image','file2'=>'fileSize:2000000'],
['file2.image' => '上传文件必须为图片','file2.fileSize' => '上传文件过大']
);
if(true !== $result){
$state = "ERROR" . $result;
}else {
// 移动到框架应用根目录/public/uploads/ 目录下
if (empty($this->savePath1))
$this->savePath = $this->savePath . date('Y') . '/' . date('m-d') . '/';
else
$this->savePath = $this->savePath . $this->savePath1 . '/' . date('Y') . '/' . date('m-d') . '/';
$info = $file->rule(function ($file) {
$saveFileName = md5(mt_rand());
return $saveFileName; // 使用自定义的文件保存规则
})->move('public/upload/' . $this->savePath);
if ($info) {
$saveFileName = $this->savePath . $info->getSaveName();
$upfilepath = ROOT_PATH . "public/upload/" . $saveFileName;
} else {
}
//
if ($tablename == "marketing" || I('savepath') == "Marketing") //上传到有礼派资源图片上传(调用接口)
{
$saveFileName = $this->savePath . $info->getSaveName();
$upfilepath = ROOT_PATH . "/public/upload/" . $saveFileName;
$img_data = [
'img' => $upfilepath,
'imgname' => basename($saveFileName),
];
$post_data = image_form_data_splice($img_data['img'], $img_data['imgname']);
if (!empty($post_data)) {
$post_data .= FORM_HYPHENS . FORM_BOUNDARY . FORM_HYPHENS;
}
$req_headers = [
'Content-Type: multipart/form-data; boundary=' . FORM_BOUNDARY,
];
$ylpres = httpRequest(YLPZY_URL . "/accounts/imgs", 'POST', $post_data, $req_headers);
$data1 = json_decode($ylpres, true);
mlog($ylpres, "imageUp");
if ($data1['code'] == 0) {
$state = "SUCCESS";
$return_data['url'] = $data1['data']['download_url'];
} else {
$return_data['url'] = "";
$state = "ERROR";
}
}
else if ($tablename == "store_wxcard")
{
$saveFileName = $this->savePath . $info->getSaveName();
$upfilepath = ROOT_PATH . "/public/upload/" . $saveFileName;
$size = getimagesize($upfilepath);
$filetype = explode('/', $size['mime']);
if (class_exists('\CURLFile')) {
$data['buffer'] = new \CURLFile($upfilepath, $size['mime'], basename($saveFileName));
} else {
$data = array(
'buffer' => '@' . realpath($saveFileName) . ";type=" . $filetype[1] . ";filename=" . basename($saveFileName)
);
}
$access_token = m_get_access_token(null,getAdmStoId());
$url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?type=image&access_token=".$access_token;
$wxres=wx_https_request($url ,'post', 'json',$data);
mlog(json_encode($wxres),"wxcard/".getAdmStoId());
if ($wxres['url']) {
$state = "SUCCESS";
$return_data['url'] =$wxres['url'];
}
else{
$return_data['url'] = "";
$state = "ERROR";
}
}
else {
//保存到存储云
vendor ('qcloudcos.myqcloudcos');
$resfolder=Myqcloudcos::statFolder('wxd',UPLOAD_PATH. $this->savePath);
if ($resfolder && $resfolder['code']!=0)//不存在创建
{
Myqcloudcos::createFolder('wxd',UPLOAD_PATH. $this->savePath);
}
//查询表取旧图,删除旧图
if ($curid != "0" && !empty($curid) ) {
$searchtable = D($tablename)->where($indexid,$curid)->find();
$oldimg=$searchtable[$imgname];
$soldimg=dirname($oldimg)."/s_".basename($oldimg);
if (!empty($oldimg)) {
mdelFile(ROOT_PATH . $oldimg);
mdelFile(ROOT_PATH . $soldimg);
if ($tablename!= "goods" && $tablename != "goods_images") {
//腾讯云
$delres=Myqcloudcos::delFile('wxd',$oldimg);
$delres=Myqcloudcos::delFile('wxd',$soldimg);
}
}
$data[$imgname]='/public/upload/'.$this->savePath.$info->getSaveName();
if($indexid!='store_id')
$data['store_id']=getAdmStoId();
if ($tablename!="goods")//商品的时候 更新相册
{
$r = M($tablename)->where($indexid,$curid)->save($data);
//$goodimgdata['image_url']='/public/upload/'.$this->savePath.$info->getSaveName();
//D('goods_images')->where(array('goods_id'=>$curid,'ismain'=>1))->save($goodimgdata);
}
}
else {
if (!empty($addoldimg)) {
$addoldimg=urldecode($addoldimg);
$saddoldimg=dirname($addoldimg)."/s_".basename($addoldimg);
mdelFile(ROOT_PATH . $addoldimg);
mdelFile(ROOT_PATH . $saddoldimg);
if ($tablename!= "goods" && $tablename != "goods_images") {
//腾讯云
$delres=Myqcloudcos::delFile('wxd',$addoldimg);
$delres=Myqcloudcos::delFile('wxd',$saddoldimg);
}
}
}
//echo print_r($info,true);
if ($info)
$state = "SUCCESS";
else
$state = "ERROR" . $file->getError();
$saveFileName=$this->savePath.$info->getSaveName();
m_cut_img(basename($saveFileName),ROOT_PATH.'/public/upload/'.dirname($saveFileName).'/');
//商品图片才加水印
if ($tablename=="goods" || $tablename=="goods_images") {
//图片水印处理
$water = tpCache('water', getAdmStoId());
$original_img = "./public/upload/" . $saveFileName;
$image = \think\Image::open($original_img);
if ($water['is_mark'] == 1) {
if ($water['mark_type'] == 'img' && !empty($water['mark_img'])) {
try {
$img = file_get_contents('https://mshopimg.yolipai.net'.$water['mark_img']);
$ext = strrchr($water['mark_img'],'.');
$pt='/public/upload/logo/' . date('Y') . '/' . date('m-d') . '/';
if (!is_dir(ROOT_PATH.$pt))
mkdir(ROOT_PATH.$pt,0777,true);
$f10=$pt.time().mt_rand().".".$ext;
file_put_contents(ROOT_PATH.$f10,$img);
if(file_exists(ROOT_PATH.$f10)) {
$image->open($original_img)->water("." . $f10, $water['mark_position'], $water['mark_degree'])->save($original_img);
}
mdelFile(ROOT_PATH.$f10);
} catch (Exception $e) {
return NOIMG;
}
} else {
//检查字体文件是否存在
if (file_exists('./SIMYOU.TTF')) {
$image->open($original_img)->text($water['mark_text'], './SIMYOU.TTF', 20, '#000000', $water['mark_position'])->save($original_img);
}
}
}
//商品表的主表生成小图
if($tablename=="goods" || $tablename=="goods_images") {
//生成小图
m_cut_img(basename($saveFileName), ROOT_PATH . '/public/upload/' . dirname($saveFileName) . '/', 400, 1);
$slocalpath=ROOT_PATH.'/'.UPLOAD_PATH.dirname($saveFileName).'/s_'.basename($saveFileName);
$sypath='/'.UPLOAD_PATH.dirname($saveFileName).'/s_'.basename($saveFileName);
$res=Myqcloudcos::upload('wxd',$slocalpath,$sypath);
}
}
//上传到腾讯云
$localpath=ROOT_PATH.'/'.UPLOAD_PATH.dirname($saveFileName).'/'.basename($saveFileName);
$ypath='/'.UPLOAD_PATH.dirname($saveFileName).'/'.basename($saveFileName);
$res=Myqcloudcos::upload('wxd',$localpath,$ypath);
mlog("上传失败的错误:".json_encode($res),"imageUp/".getAdmStoId());
if ($res && $res['code']==0)//成功
{
//mdelFile($localpath);
$return_data['url'] = '/public/upload/'.$this->savePath.$info->getSaveName();
}else{
$state = "ERROR" . "云上传失败";
}
}
}
$return_data['title'] = $title;
$return_data['original'] = ''; // 这里好像没啥用 暂时注释起来
$return_data['state'] = $state;
$return_data['path'] = $path;
//print_r($return_data);
$this->ajaxReturn($return_data,'json');
}
/**
* @function imageUp1
*/
public function imageUp1()
{
// 上传图片框中的描述表单名称,
$pictitle = I('pictitle');
$dir = I('dir');
$title = htmlspecialchars($pictitle , ENT_QUOTES);
$path = htmlspecialchars($dir, ENT_QUOTES);
$localpath1=I('savepath');
$tablename=I('tablename');
$store_id=getAdmStoId();
if(!empty($_SESSION['manager_id']) && $tablename=='manager')
{
$manager=D('manager_admin')->where('manager_id',$_SESSION['manager_id'])->find();
if($manager)
{
$store_id=0;
}
}
// $indexid=I('indexid');
// $imgname=I('imgname');
// $curid=I('curid');
$saveFileName="";
// $addoldimg=I('addoldimg');
$groupid=I('groupid');
//$input_file ['upfile'] = $info['Filedata']; 一个是上传插件里面来的, 另外一个是文章编辑器里面来的
// 获取表单上传文件
$file = request()->file('Filedata');
if(empty($file))
$file = request()->file('file'); //图库-新
if(empty($file))
$file = request()->file('upfile');
$result = $this->validate(
['file2' => $file],
['file2'=>'image','file2'=>'fileSize:2000000'],
['file2.image' => '上传文件必须为图片','file2.fileSize' => '上传文件过大']
);
if(true !== $result){
$state = "ERROR" . $result;
}else {
// 移动到框架应用根目录/public/uploads/ 目录下
if (empty($this->savePath1))
$this->savePath = $this->savePath . date('Y') . '/' . date('m-d') . '/';
else
$this->savePath = $this->savePath . $this->savePath1 . '/' . date('Y') . '/' . date('m-d') . '/';
$info = $file->rule(function ($file) {
$saveFileName = md5(mt_rand());//$_FILES['info']['name'];
return $saveFileName; // 使用自定义的文件保存规则
})->move('public/upload/' . $this->savePath);
if ($info) {
$saveFileName = $this->savePath . $info->getSaveName();
$upfilepath = ROOT_PATH . "public/upload/" . $saveFileName;
} else {
}
if(!empty($_FILES['file']['name']))
{
$file_ext=pathinfo(basename($_FILES['file']['name']))['filename'];//图片原名
}
//
if ($tablename == "marketing" || I('savepath') == "Marketing") //上传到有礼派资源图片上传(调用接口)
{
if (I('savepath') == "Marketing")//编辑器接口
{
$saveFileName = $this->savePath . $info->getSaveName();
$upfilepath = ROOT_PATH . "/public/upload/" . $saveFileName;
$img_data = [
'img' => $upfilepath,
'imgname' => basename($saveFileName),
];
$post_data = image_form_data_splice($img_data['img'], $img_data['imgname']);
if (!empty($post_data)) {
$post_data .= FORM_HYPHENS . FORM_BOUNDARY . FORM_HYPHENS;
}
$req_headers = [
'Content-Type: multipart/form-data; boundary=' . FORM_BOUNDARY,
];
$ylpres = httpRequest(YLPZY_URL . "/accounts/imgs", 'POST', $post_data, $req_headers);
$data1 = json_decode($ylpres, true);
mlog($ylpres, "imageUp");
if ($data1['code'] == 0) {
$state = "SUCCESS";
$return_data['url'] = $data1['data']['download_url']."|"."/public/upload/" . $saveFileName;
} else {
$return_data['url'] = "";
$state = "ERROR";
}
} else {
$state = "SUCCESS";
$return_data['url'] = "/public/upload/" . $saveFileName;
}
}
else {
//保存到存储云
vendor ('qcloudcos.myqcloudcos');
$resfolder=Myqcloudcos::statFolder('wxd',UPLOAD_PATH. $this->savePath);
if ($resfolder && $resfolder['code']!=0)//不存在创建
{
Myqcloudcos::createFolder('wxd',UPLOAD_PATH. $this->savePath);
}
//查询表取旧图,删除旧图(07.08.12 注释)
// if ($curid != "0" && !empty($curid) ) {
// $searchtable = D($tablename)->where($indexid,$curid)->find();
// $oldimg=$searchtable[$imgname];
// $soldimg=dirname($oldimg)."/s_".basename($oldimg);
// if (!empty($oldimg)) {
// mdelFile(ROOT_PATH . $oldimg);
// mdelFile(ROOT_PATH . $soldimg);
// //腾讯云
// $delres=Myqcloudcos::delFile('wxd',$oldimg);
// $delres=Myqcloudcos::delFile('wxd',$soldimg);
// }
// $data[$imgname]='/public/upload/'.$this->savePath.$info->getSaveName();
// $data['store_id']=getAdmStoId();
// $r = D($tablename)->where($indexid,$curid)->save($data);
// if ($tablename=="goods")//商品的时候 更新相册
// {
// $goodimgdata['image_url']='/public/upload/'.$this->savePath.$info->getSaveName();
// D('goods_images')->where(array('goods_id'=>$curid,'ismain'=>1))->save($goodimgdata);
//
// }
// }
// else {
//
// if (!empty($addoldimg)) {
// $addoldimg=urldecode($addoldimg);
// $saddoldimg=dirname($addoldimg)."/s_".basename($addoldimg);
// mdelFile(ROOT_PATH . $addoldimg);
// mdelFile(ROOT_PATH . $saddoldimg);
// //腾讯云
// $delres=Myqcloudcos::delFile('wxd',$addoldimg);
// $delres=Myqcloudcos::delFile('wxd',$saddoldimg);
//
// }
// }
//echo print_r($info,true);
if ($info)
$state = "SUCCESS";
else
$state = "ERROR" . $file->getError();
$saveFileName=$this->savePath.$info->getSaveName();
m_cut_img(basename($saveFileName),ROOT_PATH.'/public/upload/'.dirname($saveFileName).'/');
//商品图片才加水印
if ($tablename=="goods" || $tablename=="goods_images") {
//图片水印处理
$water = tpCache('water', $store_id);
$original_img = "./public/upload/" . $saveFileName;
$image = \think\Image::open($original_img);
if ($water['is_mark'] == 1) {
if ($water['mark_type'] == 'img' && !empty($water['mark_img'])) {
try {
$img = file_get_contents('https://mshopimg.yolipai.net'.$water['mark_img']);
$ext = strrchr($water['mark_img'],'.');
$pt='/public/upload/logo/' . date('Y') . '/' . date('m-d') . '/';
if (!is_dir(ROOT_PATH.$pt))
mkdir(ROOT_PATH.$pt,0777);
$f10=$pt.time().mt_rand().".".$ext;
file_put_contents(ROOT_PATH.$f10,$img);
if(file_exists(ROOT_PATH.$f10)) {
$image->open($original_img)->water("." . $water['mark_img'], $water['mark_position'], $water['mark_degree'])->save($original_img);
}
mdelFile(ROOT_PATH.$f10);
} catch (Exception $e) {
return NOIMG;
}
} else {
//检查字体文件是否存在
if (file_exists('./SIMYOU.TTF')) {
$image->open($original_img)->text($water['mark_text'], './SIMYOU.TTF', 20, '#000000', $water['mark_position'])->save($original_img);
}
}
}
//商品表的主表生成小图
if($tablename=="goods") {
//生成小图
m_cut_img(basename($saveFileName), ROOT_PATH . '/public/upload/' . dirname($saveFileName) . '/', 400, 1);
$slocalpath=ROOT_PATH.'/'.UPLOAD_PATH.dirname($saveFileName).'/s_'.basename($saveFileName);
$sypath='/'.UPLOAD_PATH.dirname($saveFileName).'/s_'.basename($saveFileName);
$res=Myqcloudcos::upload('wxd',$slocalpath,$sypath);
}
}
//上传到腾讯云
$localpath=ROOT_PATH.'/'.UPLOAD_PATH.dirname($saveFileName).'/'.basename($saveFileName);
$ypath='/'.UPLOAD_PATH.dirname($saveFileName).'/'.basename($saveFileName);
$res=Myqcloudcos::upload('wxd',$localpath,$ypath);
mlog(json_encode($res),"imageUp");
if ($res && $res['code']==0)//成功
{
//mdelFile($localpath);
}
$return_data['url'] = '/public/upload/'.$this->savePath.$info->getSaveName();
}
}
//存入图片库-数据库
$filename=$filename1=$return_data['url'];
$filename= str_replace('../','',$filename);
$filename= trim($filename,'.');
$filename= trim($filename,'/');
if(file_exists(ROOT_PATH.$filename)) {
$size = getimagesize($filename);
$filetype = explode('/', $size['mime']);
if ($filetype[0] != 'image') {
return false;
exit;
}
$filesize = filesize($filename);
$sfilename = "s_" . basename($filename);
$sdir = dirname($filename);
$newsfilename = $sdir . "/" . $sfilename;
if ($tablename != "goods") {
$file_info = array('filename' => $filename1, //国片相对于网站根目录的路径
'content-type' => $filetype[1], //文件类型
'filelength' => $filesize //图文大小
);
$wechat = D('wx_user')->where('store_id', $store_id)->find();
if ($store_id==0 || $wechat) {
// $jssdk = new \app\mobile\logic\Jssdk($wechat['appid'], $wechat['appsecret']);
// $access_token = $jssdk->get_access_token($wechat['appid'], $wechat['appsecret']);
// $url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$access_token}&type=image";
//
// $real_path = "{$_SERVER['DOCUMENT_ROOT']}{$file_info['filename']}";
// // $data = array("media" => "@{$real_path}", 'form-data' => $file_info);
//
// if (class_exists('\CURLFile')) {
// $data['media'] = new \CURLFile($real_path, $size['mime'], basename($filename1));
// } else {
// $data = array(
// 'media' => '@' . realpath($filename) . ";type=" . $filetype[1] . ";filename=" . basename($filename1)
// );
// }
// //上传至公众号
// $wxres ='';//wx_https_request($url, 'post', 'json', $data);
//
//
// if ($wxres['media_id']) {
//
// $savedate['media_id'] = $wxres['media_id'];
// $savedate['url'] = $wxres['url'];
//
//
// $info = array(
// 'status' => 1,
// 'media_id' => $wxres['media_id'],
// 'msg' => 0,
// );
//
// }
$savedate['store_id'] = $store_id;
$savedate['imgname'] = $file_ext;//basename($filename);
$savedate['img'] = $filename1;
$savedate['localpath'] = $localpath1;
$savedate['wid'] = $size[0];
$savedate['hei'] = $size[1];
$savedate['groupid']=$groupid;
$savedate['addtime'] = time();
$saveid=D('store_wximglist')->insertGetId($savedate);
//mdelFile($filename);//删除本地图片
$return_data['id']=$saveid;
$return_data['imgname']=$file_ext;
}
}
}
//basename($filename);
$return_data['title'] = $title;
$return_data['original'] = ''; // 这里好像没啥用 暂时注释起来
$return_data['state'] = $state;
$return_data['path'] = $path;
//var_dump($return_data);
mlog(json_encode($return_data),"newimg");
$this->ajaxReturn($return_data,'json');
}
/*--删除文件--*/
public function delfile(){
$f=I('file');
mdelFile($f);
echo "1"; exit();
}
/*---获取腾讯云服务器的域名---*/
public function gethosturl(){
return QCLOUD_IMGURL;
}
public function audio(){
$file = request()->file('tupfile');
$data['size']=round($file->getSize()/1024,2);
$data['name']=$file->getFilename();
// 移动到框架应用根目录/public/uploads/ 目录下
$this->savePath = $this->savePath. "/".$this->savePath1."/";
// 使用自定义的文件保存规则
$info = $file->rule(function ($file) {
return md5(mt_rand());
}) ->move('public/upload/'.$this->savePath);
//保存到存储云
vendor('qcloudcos.myqcloudcos');
$resfolder=Myqcloudcos::statFolder('wxd',UPLOAD_PATH. $this->savePath);
if ($resfolder && $resfolder['code']!=0)//不存在创建
{
Myqcloudcos::createFolder('wxd',UPLOAD_PATH. $this->savePath);
}
//上传到腾讯云
$localpath=ROOT_PATH.'/public/upload/'.$this->savePath.$info->getSaveName();
$ypath='/'.UPLOAD_PATH.$this->savePath.$info->getSaveName();
$res=Myqcloudcos::upload('wxd',$localpath,$ypath);
if($res && $res['code']==0){}
$data['url']="/public/upload/".$this->savePath.$info->getSaveName();
$data['store_id']=$this->savePath1;
$data['add_time']=time();
M("audio")->save($data);
//移动成功
$data=array(
'state' => 'SUCCESS',
'url' => "https://mshopimg.yolipai.net/public/upload/".$this->savePath.$info->getSaveName(),
'title' =>$info->getSaveName(),
'type' => $file->getExtension(),
'path'=>"/public/upload/".$this->savePath.$info->getSaveName(),
);
return json($data);
}
}