Ueditor.php 13.9 KB
Newer Older
D
v1.2.0  
devil_gong 已提交
1 2 3 4
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
D
devil_gong 已提交
5
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
D
v1.2.0  
devil_gong 已提交
6 7 8 9 10 11 12
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\admin\controller;

13 14
use think\facade\Hook;

D
v1.2.0  
devil_gong 已提交
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
/**
 * 百度编辑器控制器入口
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
class Ueditor extends Common
{
	private $current_action;
	private $current_config;
	private $current_result;

	/**
	 * 构造方法
	 * @author   Devil
	 * @blog     http://gong.gg/
	 * @version  0.0.1
	 * @datetime 2016-12-03T12:39:08+0800
	 */
	public function __construct()
	{
		// 调用父类前置方法
		parent::__construct();
	}

	/**
     * [Index 附件上传入口]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-06T21:31:53+0800
     */
	public function Index()
	{
		// 配置信息
		$this->current_config = config('ueditor_config');
		$this->current_action = input('action');
53 54 55 56 57

		// 非获取配置信息登录校验
		if($this->current_action != 'config')
		{
			// 登录校验
D
devil_gong 已提交
58
			$this->IsLogin();
59 60 61
		}

		// action
D
v1.2.0  
devil_gong 已提交
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
		switch($this->current_action)
		{
			// 配置信息
			case 'config':
				$this->current_result =  json_encode($this->current_config);
				break;

			/* 上传图片 */
			case 'uploadimage':
			/* 上传涂鸦 */
			case 'uploadscrawl':
			/* 上传视频 */
			case 'uploadvideo':
			/* 上传文件 */
			case 'uploadfile':
				$this->ActionUpload();
				break;

			/* 列出图片 */
			case 'listimage':
			/* 列出文件 */
			case 'listfile':
			/* 列出视频 */
			case 'listvideo':
				$this->ActionList();
				break;

			/* 抓取远程文件 */
			case 'catchimage':
				$this->ActionCrawler();
				break;

			/* 删除文件 */
			case 'deletefile':
				$this->DeleteFile();
				break;

			default:
				$this->current_result = json_encode(array(
					'state'=> '请求地址出错'
				));
		}

		// 输出结果
		if(input('callback'))
		{
			if(preg_match("/^[\w_]+$/", input('callback')))
			{
				echo htmlspecialchars(input('callback')) . '(' . $this->current_result . ')';
			} else {
				echo json_encode(array(
					'state'=> 'callback参数不合法'
				));
			}
		} else {
			echo $this->current_result;
		}
		exit();
	}

	/**
	 * 文件删除
	 * @author   Devil
	 * @blog    http://gong.gg/
	 * @version 1.0.0
	 * @date    2018-12-10
	 * @desc    description
	 */
	private function DeleteFile()
	{
		$path = input('path');
		if(!empty($path))
		{
135 136 137 138 139 140 141 142
			// 附件删除前处理钩子
	        $hook_name = 'plugins_controller_attachment_delete_handle_begin';
	        Hook::listen($hook_name, [
	            'hook_name'     	=> $hook_name,
	            'is_backend'    	=> true,
	            'path'				=> &$path,
	        ]);

D
devil_gong 已提交
143
			$path = (__MY_ROOT_PUBLIC__ == '/') ? substr(ROOT_PATH, 0, -1).$path : str_replace(__MY_ROOT_PUBLIC__, ROOT_PATH, $path);
D
v1.2.0  
devil_gong 已提交
144 145 146 147 148 149
			if(file_exists($path))
			{
				if(is_writable($path))
				{
					if(unlink($path))
					{
150 151 152 153 154 155 156 157
						// 附件删除成功后处理钩子
				        $hook_name = 'plugins_controller_attachment_delete_handle_end';
				        Hook::listen($hook_name, [
				            'hook_name'     	=> $hook_name,
				            'is_backend'    	=> true,
				            'path'				=> $path,
				        ]);

D
v1.2.0  
devil_gong 已提交
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
						$this->current_result = json_encode(array(
							'state'=> 'SUCCESS'
						));
					} else {
						$this->current_result = json_encode(array(
							'state'=> '删除成功'
						));
					}
				} else {
					$this->current_result = json_encode(array(
						'state'=> '没有删除权限'
					));
				}
			} else {
				$this->current_result = json_encode(array(
					'state'=> '文件不存在'
				));
			}
		} else {
			$this->current_result = json_encode(array(
				'state'=> '删除文件路径不能为空'
			));
		}
	}

	/**
	 * [ActionUpload 上传配置]
	 * @author   Devil
	 * @blog     http://gong.gg/
	 * @version  0.0.1
	 * @datetime 2017-01-17T22:45:06+0800
	 */
	private function ActionUpload()
	{
192
		$attachment_type = "file";
D
v1.2.0  
devil_gong 已提交
193 194 195 196 197 198 199 200 201
		switch(htmlspecialchars($this->current_action))
		{
			case 'uploadimage':
				$temp_config = array(
						"pathFormat" => $this->current_config['imagePathFormat'],
						"maxSize" => $this->current_config['imageMaxSize'],
						"allowFiles" => $this->current_config['imageAllowFiles']
					);
				$field_name = $this->current_config['imageFieldName'];
202
				$attachment_type = "image";
D
v1.2.0  
devil_gong 已提交
203 204 205 206 207 208 209 210 211 212
				break;

			case 'uploadscrawl':
				$temp_config = array(
						"pathFormat" => $this->current_config['scrawlPathFormat'],
						"maxSize" => $this->current_config['scrawlMaxSize'],
						"allowFiles" => $this->current_config['scrawlAllowFiles'],
						"oriName" => "scrawl.png"
					);
				$field_name = $this->current_config['scrawlFieldName'];
213
				$attachment_type = "base64";
D
v1.2.0  
devil_gong 已提交
214 215 216 217 218 219 220 221 222
				break;

			case 'uploadvideo':
				$temp_config = array(
						"pathFormat" => $this->current_config['videoPathFormat'],
						"maxSize" => $this->current_config['videoMaxSize'],
						"allowFiles" => $this->current_config['videoAllowFiles']
					);
				$field_name = $this->current_config['videoFieldName'];
223
				$attachment_type = "video";
D
v1.2.0  
devil_gong 已提交
224 225 226 227 228 229 230 231 232 233
				break;

			case 'uploadfile':
			default:
				$temp_config = array(
						"pathFormat" => $this->current_config['filePathFormat'],
						"maxSize" => $this->current_config['fileMaxSize'],
						"allowFiles" => $this->current_config['fileAllowFiles']
					);
				$field_name = $this->current_config['fileFieldName'];
234
				$attachment_type = "file";
D
v1.2.0  
devil_gong 已提交
235 236
		}

237 238 239 240 241 242 243 244 245 246
		// 附件上传前处理钩子
        $hook_name = 'plugins_controller_attachment_upload_handle_begin';
        Hook::listen($hook_name, [
            'hook_name'     	=> $hook_name,
            'is_backend'    	=> true,
            'attachment_type'	=> $attachment_type,
            'field_name'    	=> $field_name,
            'temp_config'		=> &$temp_config,
        ]);

D
v1.2.0  
devil_gong 已提交
247
		/* 生成上传实例对象并完成上传 */
248
		$up = new \base\Uploader($field_name, $temp_config, $attachment_type);
D
v1.2.0  
devil_gong 已提交
249 250 251 252 253 254 255 256 257 258 259 260

		/**
		 * 得到上传文件所对应的各个参数,数组结构
		 * array(
		 *     "state" => "",          //上传状态,上传成功时必须返回"SUCCESS"
		 *     "url" => "",            //返回的地址
		 *     "title" => "",          //新文件名
		 *     "original" => "",       //原始文件名
		 *     "type" => ""            //文件类型
		 *     "size" => "",           //文件大小
		 * )
		 */
261 262 263 264 265 266 267 268 269 270 271 272
		$data = $up->getFileInfo();

		// 附件上传成功后处理钩子
        $hook_name = 'plugins_controller_attachment_upload_handle_end';
        Hook::listen($hook_name, [
            'hook_name'     	=> $hook_name,
            'is_backend'    	=> true,
            'attachment_type'	=> $attachment_type,
            'field_name'    	=> $field_name,
            'temp_config'		=> $temp_config,
            'data'          	=> &$data,
        ]);
D
v1.2.0  
devil_gong 已提交
273 274

		// 返回数据
275
		$this->current_result = json_encode($data);
D
v1.2.0  
devil_gong 已提交
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
	}

	/**
	 * [ActionList 文件列表]
	 * @author   Devil
	 * @blog     http://gong.gg/
	 * @version  0.0.1
	 * @datetime 2017-01-17T22:55:16+0800
	 */
	private function ActionList()
	{
		/* 判断类型 */
		switch($this->current_action)
		{
			/* 列出视频 */
			case 'listvideo':
				$allow_files = $this->current_config['videoManagerAllowFiles'];
				$list_size = $this->current_config['videoManagerListSize'];
				$path = $this->current_config['videoManagerListPath'];
				break;
			/* 列出文件 */
			case 'listfile':
				$allow_files = $this->current_config['fileManagerAllowFiles'];
				$list_size = $this->current_config['fileManagerListSize'];
				$path = $this->current_config['fileManagerListPath'];
				break;

			/* 列出图片 */
			case 'listimage':
			default:
				$allow_files = $this->current_config['imageManagerAllowFiles'];
				$list_size = $this->current_config['imageManagerListSize'];
				$path = $this->current_config['imageManagerListPath'];
		}
		$allow_files = substr(str_replace(".", "|", join("", $allow_files)), 1);

		/* 获取参数 */
		$size = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : $list_size;
		$start = isset($_GET['start']) ? htmlspecialchars($_GET['start']) : 0;
		$end = $start + $size;

		/* 获取文件列表 */
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
		$data = array(
			"state" => "no match file",
			"list" => array(),
			"start" => $start,
			"total" => 0,
		);

		// 是否从磁盘获取数据 0否, 1是
		$is_disk_get = 1;

		// 附件列表获取处理钩子
        $hook_name = 'plugins_controller_attachment_get_list_handle';
        Hook::listen($hook_name, [
            'hook_name'     	=> $hook_name,
            'is_backend'    	=> true,
            'is_disk_get'		=> &$is_disk_get,
            'data'				=> &$data,
        ]);

        // 是否从磁盘获取数据
        if($is_disk_get == 1)
        {
        	$path = GetDocumentRoot() . (substr($path, 0, 1) == "/" ? "":"/") . $path;
			$this->GetFilesList($path, $allow_files, $size, $start, $end, $data);
        }
		
		/* 返回数据 */
		$this->current_result = json_encode($data);
	}

	/**
	 * [GetFilesList 获取目录下的指定类型的文件]
	 * @author   Devil
	 * @blog     http://gong.gg/
	 * @version  0.0.1
	 * @datetime 2017-01-17T23:24:59+0800
	 * @param    [string]        $path       	[路径地址]
	 * @param    [string]        $allow_files 	[允许的文件]
	 * @param    [int]         	 $size     		[条数]
	 * @param    [int]         	 $start     	[起始]
	 * @param    [int]         	 $end     		[结束标记]
	 * @param    [array]         $data     		[返回数据]
	 * @return   [array]                     	[数据]
	 */
	private function GetFilesList($path, $allow_files, $size, $start, $end, &$data)
	{
		// 从磁盘获取文件
		$files = $this->GetDirFilesList($path, $allow_files, $files);
D
v1.2.0  
devil_gong 已提交
366 367 368 369

		// 倒序
		//$files = $this->ArrayQuickSort($files);

G
gongfuxiang 已提交
370
		if(is_array($files) && count($files) > 0)
371 372 373 374 375 376 377 378 379 380 381 382 383
		{
			/* 获取指定范围的列表 */
			$len = count($files);
			$list = [];
			for ($i = min($end, $len) - 1; $i < $len && $i >= 0 && $i >= $start; $i--)
			{
				$list[] = $files[$i];
			}

			/* 返回数据 */
			$data = array(
				"state" => "SUCCESS",
				"list" => $list,
D
v1.2.0  
devil_gong 已提交
384 385
				"start" => $start,
				"total" => count($files)
386
			);
D
v1.2.0  
devil_gong 已提交
387
		}
388
	}
D
v1.2.0  
devil_gong 已提交
389

390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
	/**
	 * 遍历获取目录下的指定类型的文件
	 * @author   Devil
	 * @blog     http://gong.gg/
	 * @version  0.0.1
	 * @datetime 2017-01-17T23:24:59+0800
	 * @param    [string]        $path       	[路径地址]
	 * @param    [string]        $allow_files 	[允许的文件]
	 * @param    [array]         &$files     	[数据]
	 * @return   [array]                     	[数据]
	 */
	private function GetDirFilesList($path, $allow_files, &$files = array())
	{
		if(!is_dir($path)) return null;
		if(substr($path, strlen($path) - 1) != '/') $path .= '/';
		$handle = opendir($path);
		$document_root = GetDocumentRoot();
		while(false !== ($file = readdir($handle)))
D
v1.2.0  
devil_gong 已提交
408
		{
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
			if($file != '.' && $file != '..')
			{
				$path2 = $path . $file;
				if(is_dir($path2))
				{
					$this->GetDirFilesList($path2, $allow_files, $files);
				} else {
					if(preg_match("/\.(".$allow_files.")$/i", $file))
					{
						$files[] = array(
							'url'=> substr($path2, strlen($document_root)),
							'mtime'=> filemtime($path2)
						);
					}
				}
			}
D
v1.2.0  
devil_gong 已提交
425
		}
426
		return $files;
D
v1.2.0  
devil_gong 已提交
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
	}

	/**
	 * 文件快速排序
	 * @author   Devil
	 * @blog    http://gong.gg/
	 * @version 1.0.0
	 * @date    2018-12-25
	 * @desc    description
	 * @param  [array] $data [需要排序的数据(选择一个基准元素,将待排序分成小和打两罐部分,以此类推递归的排序划分两罐部分)]
	 * @return [array]       [排序好的数据]
	 */
	private function ArrayQuickSort($data)
	{
		if(!empty($data) && is_array($data))
		{
			$len = count($data);
			if($len <= 1) return $data;

			$base = $data[0];
			$left_array = array();
			$right_array = array();
			for($i=1; $i<$len; $i++)
			{
				if($base['mtime'] < $data[$i]['mtime'])
				{
					$left_array[] = $data[$i];
				} else {
					$right_array[] = $data[$i];
				}
			}
			if(!empty($left_array)) $left_array = $this->ArrayQuickSort($left_array);
			if(!empty($right_array)) $right_array = $this->ArrayQuickSort($right_array);

			return array_merge($left_array, array($base), $right_array);
		}
	}

	/**
	 * [ActionCrawler 抓取远程文件]
	 * @author   Devil
	 * @blog     http://gong.gg/
	 * @version  0.0.1
	 * @datetime 2017-01-17T23:08:29+0800
	 */
	private function ActionCrawler()
	{
		$temp_config = array(
				"pathFormat" => $this->current_config['catcherPathFormat'],
				"maxSize" => $this->current_config['catcherMaxSize'],
				"allowFiles" => $this->current_config['catcherAllowFiles'],
				"oriName" => "remote.png"
			);
		$field_name = $this->current_config['catcherFieldName'];

		/* 抓取远程图片 */
		$list = array();
		$source = isset($_POST[$field_name]) ? $_POST[$field_name] : $_GET[$field_name];
		foreach($source as $imgUrl)
		{
			$item = new \base\Uploader($imgUrl, $temp_config, "remote");
			$info = $item->getFileInfo();
			array_push($list, array(
				"state" => $info["state"],
				"url" => $info["url"],
				"size" => $info["size"],
				"title" => htmlspecialchars($info["title"]),
				"original" => htmlspecialchars($info["original"]),
				"source" => htmlspecialchars($imgUrl)
			));
		}

499 500 501 502 503 504 505 506
		// 附件抓取远程文件取结束处理钩子
        $hook_name = 'plugins_controller_attachment_crawler_handle_end';
        Hook::listen($hook_name, [
            'hook_name'     	=> $hook_name,
            'is_backend'    	=> true,
            'list'				=> &$list,
        ]);

D
v1.2.0  
devil_gong 已提交
507 508 509 510 511 512 513 514
		/* 返回抓取数据 */
		$this->current_result = json_encode(array(
				'state'=> count($list) ? 'SUCCESS':'ERROR',
				'list'=> $list
			));
	}
}
?>