Ueditor.class.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. /**
  3. * Ueditor插件
  4. * @author Nintendov
  5. */
  6. namespace Org\Util;
  7. class Ueditor{
  8. //public $uid;//要操作的用户id 如有登录需要则去掉注释
  9. private $output;//要输出的数据
  10. private $st;
  11. private $rootpath = '/Uploads';
  12. public function __construct($uid = ''){
  13. //uid 为空则导入当前会话uid
  14. //if(''===$uid) $this->uid = session('uid');
  15. \Vin\FileStorage::connect(STORAGE_TYPE);
  16. //导入设置
  17. $CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents(CONF_PATH."ueditor.json")), true);
  18. $action = htmlspecialchars($_GET['action']);
  19. switch($action){
  20. case 'config':
  21. $result = json_encode($CONFIG);
  22. break;
  23. case 'uploadimage':
  24. $config = array(
  25. "pathFormat" => $CONFIG['imagePathFormat'],
  26. "maxSize" => $CONFIG['imageMaxSize'],
  27. "allowFiles" => $CONFIG['imageAllowFiles']
  28. );
  29. $fieldName = $CONFIG['imageFieldName'];
  30. $result = $this->uploadFile($config, $fieldName);
  31. break;
  32. case 'uploadscrawl':
  33. $config = array(
  34. "pathFormat" => $CONFIG['scrawlPathFormat'],
  35. "maxSize" => $CONFIG['scrawlMaxSize'],
  36. "allowFiles" => $CONFIG['scrawlAllowFiles'],
  37. "oriName" => "scrawl.png"
  38. );
  39. $fieldName = $CONFIG['scrawlFieldName'];
  40. $result=$this->uploadBase64($config,$fieldName);
  41. break;
  42. case 'uploadvideo':
  43. $config = array(
  44. "pathFormat" => $CONFIG['videoPathFormat'],
  45. "maxSize" => $CONFIG['videoMaxSize'],
  46. "allowFiles" => $CONFIG['videoAllowFiles']
  47. );
  48. $fieldName = $CONFIG['videoFieldName'];
  49. $result=$this->uploadFile($config, $fieldName);
  50. break;
  51. case 'uploadfile':
  52. // default:
  53. $config = array(
  54. "pathFormat" => $CONFIG['filePathFormat'],
  55. "maxSize" => $CONFIG['fileMaxSize'],
  56. "allowFiles" => $CONFIG['fileAllowFiles']
  57. );
  58. $fieldName = $CONFIG['fileFieldName'];
  59. $result=$this->uploadFile($config, $fieldName);
  60. break;
  61. case 'listfile':
  62. $config=array(
  63. 'allowFiles' => $CONFIG['fileManagerAllowFiles'],
  64. 'listSize' => $CONFIG['fileManagerListSize'],
  65. 'path' => $CONFIG['fileManagerListPath'],
  66. );
  67. $result = $this->listFile($config);
  68. break;
  69. case 'listimage':
  70. $config=array(
  71. 'allowFiles' => $CONFIG['imageManagerAllowFiles'],
  72. 'listSize' => $CONFIG['imageManagerListSize'],
  73. 'path' => $CONFIG['imageManagerListPath'],
  74. );
  75. $result = $this->listFile($config);
  76. break;
  77. case 'catchimage':
  78. $config = array(
  79. "pathFormat" => $CONFIG['catcherPathFormat'],
  80. "maxSize" => $CONFIG['catcherMaxSize'],
  81. "allowFiles" => $CONFIG['catcherAllowFiles'],
  82. "oriName" => "remote.png"
  83. );
  84. $fieldName = $CONFIG['catcherFieldName'];
  85. $result = $this->saveRemote($config , $fieldName);
  86. break;
  87. default:
  88. $result = json_encode(array(
  89. 'state'=> 'wrong require'
  90. ));
  91. break;
  92. }
  93. if (isset($_GET["callback"])) {
  94. if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
  95. $this->output = htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
  96. } else {
  97. $this->output = json_encode(array(
  98. 'state'=> 'callback参数不合法'
  99. ));
  100. }
  101. } else {
  102. $this->output = $result;
  103. }
  104. }
  105. /**
  106. *
  107. * 输出结果
  108. * @param data 数组数据
  109. * @return 组合后json格式的结果
  110. */
  111. public function output(){
  112. return $this->output;
  113. }
  114. /**
  115. * 上传文件方法
  116. *
  117. */
  118. private function uploadFile($config,$fieldName){
  119. $upload = new \Think\Upload();
  120. $upload->maxSize = $config['maxSize'] ;// 设置附件上传大小
  121. $upload->exts = $this->format_exts($config['allowFiles']);// 设置附件上传类型
  122. $upload->rootPath = '.'.$this->rootpath; // 设置附件上传根目录
  123. $upload->autoSub = false;
  124. $upload->savePath = $this->getFullPath($config['pathFormat']); // 设置附件上传(子)目录
  125. $info=$upload->uploadOne($_FILES[$fieldName]);
  126. $rootpath = $this->rootpath;
  127. if(!$info){
  128. $data = array(
  129. "state"=>$upload -> getError(),
  130. );
  131. }else{
  132. $data = array(
  133. 'state'=>"SUCCESS",
  134. 'url'=>\Vin\FileStorage::getPath($rootpath,$info['savepath'].$info['savename']),
  135. 'title'=>$info['savename'],
  136. 'original'=>$info['name'],
  137. 'type'=>'.' . $info['ext'],
  138. 'size'=>$info['size'],
  139. );
  140. }
  141. return json_encode($data);
  142. }
  143. /**
  144. *
  145. * Enter description here ...
  146. */
  147. private function uploadBase64($config,$fieldName){
  148. $data = array();
  149. $base64Data = $_POST[$fieldName];
  150. $img = base64_decode($base64Data);
  151. $path = $this->getFullPath($config['pathFormat']);
  152. if(strlen($img)>$config['maxSize']){
  153. $data['states'] = 'too large';
  154. return json_encode($data);
  155. }
  156. $rootpath = $this->rootpath;
  157. //替换随机字符串
  158. $imgname = uniqid().'.png';
  159. $filename = $path.$imgname;
  160. if(\Vin\FileStorage::put($rootpath,$filename,$img)){
  161. $data=array(
  162. 'state'=>'SUCCESS',
  163. 'url'=>\Vin\FileStorage::getPath($rootpath,$filename),
  164. 'title'=>$imgname,
  165. 'original'=>'scrawl.png',
  166. 'type'=>'.png',
  167. 'size'=>strlen($img),
  168. );
  169. }else{
  170. $data=array(
  171. 'state'=>'cant write',
  172. );
  173. }
  174. return json_encode($data);
  175. }
  176. /**
  177. * 列出文件夹下所有文件,如果是目录则向下
  178. */
  179. private function listFile($config){
  180. $allowFiles = substr(str_replace(".", "|", join("", $config['allowFiles'])), 1);
  181. $size = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : $config['listSize'];
  182. $start = isset($_GET['start']) ? htmlspecialchars($_GET['start']) : 0;
  183. $end = $start + $size;
  184. $rootpath = $this->rootpath;
  185. $path = $config['path'];
  186. $files = \Vin\FileStorage::listFile($rootpath,$path, $allowFiles);
  187. //return $files;
  188. if (!count($files)) {
  189. return json_encode(array(
  190. "state" => "no match file",
  191. "list" => array(),
  192. "start" => $start,
  193. "total" => count($files)
  194. ));
  195. }
  196. /* 获取指定范围的列表 */
  197. $len = count($files);
  198. for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--){
  199. $list[] = $files[$i];
  200. }
  201. //倒序
  202. //for ($i = $end, $list = array(); $i < $len && $i < $end; $i++){
  203. // $list[] = $files[$i];
  204. //}
  205. /* 返回数据 */
  206. $result = json_encode(array(
  207. "state" => "SUCCESS",
  208. "list" => $list,
  209. "start" => $start,
  210. "total" => count($files)
  211. ));
  212. return $result;
  213. }
  214. /**
  215. *
  216. * Enter description here ...
  217. */
  218. private function saveRemote($config , $fieldName){
  219. $list = array();
  220. if (isset($_POST[$fieldName])) {
  221. $source = $_POST[$fieldName];
  222. } else {
  223. $source = $_GET[$fieldName];
  224. }
  225. foreach ($source as $imgUrl) {
  226. $upload = new \Think\Upload();
  227. $imgUrl = htmlspecialchars($imgUrl);
  228. $imgUrl = str_replace("&amp;", "&", $imgUrl);
  229. //http开头验证
  230. if (strpos($imgUrl, "http") !== 0) {
  231. $data = array('state'=>'不是http链接');
  232. return json_encode($data);
  233. }
  234. //格式验证(扩展名验证和Content-Type验证)
  235. $fileType = strtolower(strrchr($imgUrl, '.'));
  236. if (!in_array($fileType, $config['allowFiles']) || stristr($heads['Content-Type'], "image")) {
  237. $data = array("state"=>"错误文件格式");
  238. return json_encode($data);
  239. }
  240. //打开输出缓冲区并获取远程图片
  241. ob_start();
  242. $context = stream_context_create(
  243. array('http' => array(
  244. 'follow_location' => false // don't follow redirects
  245. ))
  246. );
  247. readfile($imgUrl, false, $context);
  248. $img = ob_get_contents();
  249. ob_end_clean();
  250. preg_match("/[\/]([^\/]*)[\.]?[^\.\/]*$/", $imgUrl, $m);
  251. $path = $this->getFullPath($config['pathFormat']);
  252. if(strlen($img)>$config['maxSize']){
  253. $data['states'] = 'too large';
  254. return json_encode($data);
  255. }
  256. $rootpath = $this->rootpath;
  257. $imgname = uniqid().'.png';
  258. $filename = $path.$imgname;
  259. $oriName = $m ? $m[1]:"";
  260. if(\Vin\FileStorage::put($rootpath,$filename,$img)){
  261. array_push($list, array(
  262. "state" => 'SUCCESS',
  263. "url" => \vin\FileStorage::getPath($rootpath,$filename),
  264. "size" => strlen($img),
  265. "title" => $imgname,
  266. "original" => $oriName,
  267. "source" => htmlspecialchars($imgUrl)
  268. ));
  269. }else{
  270. array_push($list,array('state'=>'文件写入失败'));
  271. }
  272. }
  273. /* 返回抓取数据 */
  274. return json_encode(array(
  275. 'state'=> count($list) ? 'SUCCESS':'ERROR',
  276. 'list'=> $list
  277. ));
  278. }
  279. /**
  280. * 规则替换命名文件
  281. * @param $path
  282. * @return string
  283. */
  284. private function getFullPath($path)
  285. {
  286. //替换日期事件
  287. $t = time();
  288. $d = explode('-', date("Y-y-m-d-H-i-s"));
  289. $format = $path;
  290. $format = str_replace("{yyyy}", $d[0], $format);
  291. $format = str_replace("{yy}", $d[1], $format);
  292. $format = str_replace("{mm}", $d[2], $format);
  293. $format = str_replace("{dd}", $d[3], $format);
  294. $format = str_replace("{hh}", $d[4], $format);
  295. $format = str_replace("{ii}", $d[5], $format);
  296. $format = str_replace("{ss}", $d[6], $format);
  297. $format = str_replace("{uid}", $this->uid, $format);
  298. return $format;
  299. }
  300. private function format_exts($exts){
  301. $data=array();
  302. foreach ($exts as $key => $value) {
  303. $data[]=ltrim($value,'.');
  304. }
  305. return $data;
  306. }
  307. }