You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

423 lines
16 KiB

4 years ago
  1. <?php
  2. //附件/图片等等
  3. namespace Api\Controller;
  4. use Think\Controller;
  5. class AttachmentController extends BaseController
  6. {
  7. public function index()
  8. {
  9. echo 'Attachment';
  10. }
  11. //浏览附件
  12. public function visitFile()
  13. {
  14. // dump("2333");
  15. // exit();
  16. $sign = I("sign");
  17. $imageView2 = I("imageView2");
  18. $d = D("UploadFile");
  19. $ret = $d->where(" sign = '%s' ", array($sign))->find();
  20. if ($ret) {
  21. $beyond_the_quota = 0;
  22. $days = ceil((time() - $ret['addtime']) / 86400);//自添加图片以来的天数
  23. $adv_day_times = $ret['visit_times'] / $days; //平均每天的访问次数
  24. $flow_rate = ($ret['file_size'] * $ret['visit_times']) / $days; //日均流量
  25. //如果是apk文件且在微信浏览器中打开
  26. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false && strpos($ret['real_url'], '.apk') !== false) {
  27. header("Content-type: text/html; charset=utf-8");
  28. echo "<head><title>温馨提示</title></head>";
  29. echo "<br><h1>微信不支持直接下载,请点击右上角“---”在外部浏览器中打开</h1>";
  30. return;
  31. }
  32. $d->where(" sign = '%s' ", array($sign))->save(array("visit_times" => $ret['visit_times'] + 1, "last_visit_time" => time()));
  33. //记录用户流量
  34. D("Attachment")->recordUserFlow($ret['uid'], $ret['file_size']);
  35. //$ret['cache_url'] = '' ; //把这个变量赋值为空,禁用掉cache_url;
  36. if ($ret['cache_url']) {
  37. $url = $ret['cache_url'];
  38. } else {
  39. $url = $ret['real_url'];
  40. }
  41. // dump($url);exit();
  42. header("location:{$url}");
  43. } else {
  44. echo "www.showdoc.cc";
  45. }
  46. }
  47. //上传图片
  48. public function uploadImg()
  49. {
  50. $login_user = $this->checkLogin();
  51. $item_id = I("item_id/d") ? I("item_id/d") : 0;
  52. $page_id = I("page_id/d") ? I("page_id/d") : 0;
  53. if ($_FILES['editormd-image-file']['name'] == 'blob') {
  54. $_FILES['editormd-image-file']['name'] .= '.jpg';
  55. }
  56. if (!$_FILES['editormd-image-file']) {
  57. return false;
  58. }
  59. if (strstr(strip_tags(strtolower($_FILES['editormd-image-file']['name'])), ".php")) {
  60. return false;
  61. }
  62. $oss_open = D("Options")->get("oss_open");
  63. if ($oss_open) {
  64. $uploadFile = $_FILES['editormd-image-file'];
  65. $url = upload_oss($uploadFile);
  66. if ($url) {
  67. $sign = md5($url . time() . rand());
  68. $insert = array(
  69. "sign" => $sign,
  70. "uid" => $login_user['uid'],
  71. "item_id" => $item_id,
  72. "page_id" => $page_id,
  73. "display_name" => $uploadFile['name'],
  74. "file_type" => $uploadFile['type'],
  75. "file_size" => $uploadFile['size'],
  76. "real_url" => $url,
  77. "addtime" => time(),
  78. );
  79. $ret = D("UploadFile")->add($insert);
  80. $url = get_domain() . U("api/attachment/visitFile", array("sign" => $sign)) . "&showdoc=.jpg";
  81. echo json_encode(array("url" => $url, "success" => 1));
  82. }
  83. return;
  84. }
  85. $upload = new \Think\Upload();// 实例化上传类
  86. $upload->maxSize = 1003145728;// 设置附件上传大小
  87. $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
  88. $upload->rootPath = './../Public/Uploads/';// 设置附件上传目录
  89. $upload->savePath = '';// 设置附件上传子目录
  90. $info = $upload->uploadOne($_FILES['editormd-image-file']);
  91. if (!$info) {// 上传错误提示错误信息
  92. $this->error($upload->getError());
  93. return;
  94. } else {// 上传成功 获取上传文件信息
  95. $url = get_domain() . __ROOT__ . substr($upload->rootPath, 1) . $info['savepath'] . $info['savename'];
  96. $sign = md5($url . time() . rand());
  97. $uploadFile = $_FILES['editormd-image-file'];
  98. $insert = array(
  99. "sign" => $sign,
  100. "uid" => $login_user['uid'],
  101. "item_id" => $item_id,
  102. "page_id" => $page_id,
  103. "display_name" => $uploadFile['name'],
  104. "file_type" => $uploadFile['type'],
  105. "file_size" => $uploadFile['size'],
  106. "real_url" => $url,
  107. "addtime" => time(),
  108. );
  109. $ret = D("UploadFile")->add($insert);
  110. $url = get_domain() . U("api/attachment/visitFile", array("sign" => $sign)) . "&showdoc=.jpg";
  111. echo json_encode(array("url" => $url, "success" => 1));
  112. }
  113. }
  114. //页面的上传附件
  115. public function pageAttachmentUpload()
  116. {
  117. $login_user = $this->checkLogin();
  118. $item_id = I("item_id/d") ? I("item_id/d") : 0;
  119. $page_id = I("page_id/d") ? I("page_id/d") : 0;
  120. $uploadFile = $_FILES['file'];
  121. if (!$page_id) {
  122. $this->sendError(10103, "请至少先保存一次页面内容");
  123. return;
  124. }
  125. if (!$this->checkItemPermn($login_user['uid'], $item_id)) {
  126. $this->sendError(10103);
  127. return;
  128. }
  129. if (!$uploadFile) {
  130. return false;
  131. }
  132. if (strstr(strip_tags(strtolower($uploadFile['name'])), ".php")) {
  133. return false;
  134. }
  135. $oss_open = D("Options")->get("oss_open");
  136. if ($oss_open) {
  137. $url = upload_oss($uploadFile);
  138. if ($url) {
  139. $sign = md5($url . time() . rand());
  140. $insert = array(
  141. "sign" => $sign,
  142. "uid" => $login_user['uid'],
  143. "item_id" => $item_id,
  144. "page_id" => $page_id,
  145. "display_name" => $uploadFile['name'],
  146. "file_type" => $uploadFile['type'],
  147. "file_size" => $uploadFile['size'],
  148. "real_url" => $url,
  149. "addtime" => time(),
  150. );
  151. $ret = D("UploadFile")->add($insert);
  152. $url = get_domain() . U("api/attachment/visitFile", array("sign" => $sign));
  153. echo json_encode(array("url" => $url, "success" => 1));
  154. }
  155. return;
  156. }
  157. $upload = new \Think\Upload();// 实例化上传类
  158. $upload->maxSize = 4145728000;// 设置附件上传大小
  159. $upload->rootPath = './../Public/Uploads/';// 设置附件上传目录
  160. $upload->savePath = '';// 设置附件上传子目录
  161. $info = $upload->uploadOne($uploadFile);
  162. if (!$info) {// 上传错误提示错误信息
  163. $this->error($upload->getError());
  164. return;
  165. } else {// 上传成功 获取上传文件信息
  166. $url = get_domain() . __ROOT__ . substr($upload->rootPath, 1) . $info['savepath'] . $info['savename'];
  167. $sign = md5($url . time() . rand());
  168. $insert = array(
  169. "sign" => $sign,
  170. "uid" => $login_user['uid'],
  171. "item_id" => $item_id,
  172. "page_id" => $page_id,
  173. "display_name" => $uploadFile['name'],
  174. "file_type" => $uploadFile['type'],
  175. "file_size" => $uploadFile['size'],
  176. "real_url" => $url,
  177. "addtime" => time(),
  178. );
  179. $ret = D("UploadFile")->add($insert);
  180. $url = get_domain() . U("api/attachment/visitFile", array("sign" => $sign));
  181. echo json_encode(array("url" => $url, "success" => 1));
  182. }
  183. }
  184. //页面的上传附件列表
  185. public function pageAttachmentUploadList()
  186. {
  187. $login_user = $this->checkLogin();
  188. $item_id = I("item_id/d") ? I("item_id/d") : 0;
  189. $page_id = I("page_id/d") ? I("page_id/d") : 0;
  190. if (!$page_id) {
  191. $this->sendError(10103, "请至少先保存一次页面内容");
  192. return;
  193. }
  194. $return = array();
  195. $files = D("UploadFile")->where("page_id = '$page_id' ")->order("addtime desc")->select();
  196. if ($files) {
  197. $item_id = $files[0]['item_id'];
  198. if (!$this->checkItemVisit($login_user['uid'], $item_id)) {
  199. $this->sendError(10103);
  200. return;
  201. }
  202. foreach ($files as $key => $value) {
  203. $url = '';
  204. if ($value['sign']) {
  205. $url = get_domain() . U("api/attachment/visitFile", array("sign" => $value['sign']));
  206. } else {
  207. $url = $value['real_url'];
  208. }
  209. $return[] = array(
  210. "file_id" => $value['file_id'],
  211. "display_name" => $value['display_name'],
  212. "url" => $url,
  213. "addtime" => date("Y-m-d H:i:s", $value['addtime']),
  214. );
  215. }
  216. }
  217. $this->sendResult($return);
  218. }
  219. //删除页面中已上传文件
  220. public function deletePageUploadFile()
  221. {
  222. $login_user = $this->checkLogin();
  223. $file_id = I("file_id/d") ? I("file_id/d") : 0;
  224. $file = D("UploadFile")->where("file_id = '$file_id' ")->find();
  225. $item_id = $file['item_id'];
  226. if (!$this->checkItemPermn($login_user['uid'], $item_id)) {
  227. $this->sendError(10103);
  228. return;
  229. }
  230. $ret = D("Page")->deleteFile($file_id);
  231. if ($ret) {
  232. $this->sendResult(array());
  233. } else {
  234. $this->sendError(10101, "删除失败");
  235. }
  236. }
  237. //获取全站的附件列表。给管理员查看附件用
  238. public function getAllList()
  239. {
  240. $login_user = $this->checkLogin();
  241. $this->checkAdmin(); //重要,校验管理员身份
  242. $page = I("page/d");
  243. $count = I("count/d");
  244. $attachment_type = I("attachment_type/d");
  245. $display_name = I("display_name");
  246. $username = I("username");
  247. $return = array();
  248. $where = ' 1 = 1 ';
  249. if ($attachment_type == 1) {
  250. $where .= " and file_type like '%image%' ";
  251. }
  252. if ($attachment_type == 2) {
  253. $where .= " and file_type not like '%image%' ";
  254. }
  255. if ($display_name) {
  256. $display_name = \SQLite3::escapeString($display_name);
  257. $where .= " and display_name like '%{$display_name}%' ";
  258. }
  259. if ($username) {
  260. $username = \SQLite3::escapeString($username);
  261. $uid = D("User")->where(" username = '{$username}' ")->getField('uid');
  262. $uid = $uid ? $uid : -99;
  263. $where .= " and uid = '{$uid}' ";
  264. }
  265. $files = D("UploadFile")->where($where)->order("addtime desc")->page($page, $count)->select();
  266. if ($files) {
  267. foreach ($files as $key => $value) {
  268. $username = '';
  269. if ($value['uid']) {
  270. $username = D("User")->where(" uid = {$value['uid']} ")->getField('username');
  271. }
  272. $url = '';
  273. if ($value['sign']) {
  274. $url = get_domain() . U("api/attachment/visitFile", array("sign" => $value['sign']));
  275. } else {
  276. $url = $value['real_url'];
  277. }
  278. $return['list'][] = array(
  279. "file_id" => $value['file_id'],
  280. "username" => $username,
  281. "uid" => $value['uid'],
  282. "file_type" => $value['file_type'],
  283. "visit_times" => $value['visit_times'],
  284. "file_size" => $value['file_size'],
  285. "item_id" => $value['item_id'],
  286. "page_id" => $value['page_id'],
  287. "file_size_m" => round($value['file_size'] / (1024 * 1024), 3),
  288. "display_name" => $value['display_name'] ? $value['display_name'] : '',
  289. "url" => $url,
  290. "addtime" => date("Y-m-d H:i:s", $value['addtime']),
  291. "last_visit_time" => date("Y-m-d H:i:s", $value['last_visit_time']),
  292. );
  293. }
  294. }
  295. $return['total'] = D("UploadFile")->where($where)->count();
  296. $used = D("UploadFile")->where($where)->getField('sum(file_size)');
  297. $return['used'] = $used;
  298. $return['used_m'] = round($used / (1024 * 1024), 3);
  299. $this->sendResult($return);
  300. }
  301. //删除附件
  302. public function deleteAttachment()
  303. {
  304. $login_user = $this->checkLogin();
  305. $this->checkAdmin(); //重要,校验管理员身份
  306. $file_id = I("file_id/d") ? I("file_id/d") : 0;
  307. $file = D("UploadFile")->where("file_id = '$file_id' ")->find();
  308. $ret = D("Page")->deleteFile($file_id);
  309. if ($ret) {
  310. $this->sendResult(array());
  311. } else {
  312. $this->sendError(10101, "删除失败");
  313. }
  314. }
  315. //获取我的附件列表
  316. public function getMyList()
  317. {
  318. $login_user = $this->checkLogin();
  319. $page = I("page/d");
  320. $count = I("count/d");
  321. $attachment_type = I("attachment_type/d");
  322. $display_name = I("display_name");
  323. $username = I("username");
  324. $return = array();
  325. $where = " uid = '{$login_user[uid]}' ";
  326. if ($attachment_type == 1) {
  327. $where .= " and file_type like '%image%' ";
  328. }
  329. if ($attachment_type == 2) {
  330. $where .= " and file_type not like '%image%' ";
  331. }
  332. if ($display_name) {
  333. $display_name = \SQLite3::escapeString($display_name);
  334. $where .= " and display_name like '%{$display_name}%' ";
  335. }
  336. $files = D("UploadFile")->where($where)->order("addtime desc")->page($page, $count)->select();
  337. if ($files) {
  338. foreach ($files as $key => $value) {
  339. $username = '';
  340. $return['list'][] = array(
  341. "file_id" => $value['file_id'],
  342. "uid" => $value['uid'],
  343. "file_type" => $value['file_type'],
  344. "visit_times" => $value['visit_times'],
  345. "file_size" => $value['file_size'],
  346. "item_id" => $value['item_id'],
  347. "page_id" => $value['page_id'],
  348. "file_size_m" => round($value['file_size'] / (1024 * 1024), 3),
  349. "display_name" => $value['display_name'] ? $value['display_name'] : '',
  350. "url" => get_domain() . U("api/attachment/visitFile", array("sign" => $value['sign'])),
  351. "addtime" => date("Y-m-d H:i:s", $value['addtime']),
  352. "last_visit_time" => date("Y-m-d H:i:s", $value['last_visit_time']),
  353. );
  354. }
  355. }
  356. $return['total'] = D("UploadFile")->where($where)->count();
  357. $used = D("UploadFile")->where($where)->getField('sum(file_size)');
  358. $return['used'] = $used;
  359. $return['used_m'] = round($used / (1024 * 1024), 3);
  360. $used_flow = D("Attachment")->getUserFlow($login_user['uid']);; //该用户的本月使用流量
  361. $return['used_flow_m'] = round($used_flow / (1024 * 1024), 3);
  362. $this->sendResult($return);
  363. }
  364. //删除附件
  365. public function deleteMyAttachment()
  366. {
  367. $login_user = $this->checkLogin();
  368. $file_id = I("file_id/d") ? I("file_id/d") : 0;
  369. $file = D("UploadFile")->where("file_id = '$file_id' and uid ='$login_user[uid]' ")->find();
  370. if ($file) {
  371. $ret = D("Page")->deleteFile($file_id);
  372. if ($ret) {
  373. $this->sendResult(array());
  374. return;
  375. }
  376. }
  377. $this->sendError(10101, "删除失败");
  378. }
  379. }