AttachmentController.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. //附件/图片等等
  3. namespace Api\Controller;
  4. use Think\Controller;
  5. class AttachmentController extends BaseController {
  6. public function index(){
  7. echo 'Attachment';
  8. }
  9. //浏览附件
  10. public function visitFile(){
  11. $sign = I("sign");
  12. $imageView2 = I("imageView2");
  13. $d = D("UploadFile") ;
  14. $ret = $d->where(" sign = '%s' ",array($sign))->find();
  15. if ($ret) {
  16. $beyond_the_quota = 0 ;
  17. $days = ceil(( time() -$ret['addtime'])/86400);//自添加图片以来的天数
  18. $adv_day_times = $ret['visit_times'] / $days ; //平均每天的访问次数
  19. $flow_rate = ( $ret['file_size'] * $ret['visit_times'] ) / $days ; //日均流量
  20. //如果是apk文件且在微信浏览器中打开
  21. if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false && strpos($ret['real_url'] , '.apk') !== false ) {
  22. header("Content-type: text/html; charset=utf-8");
  23. echo "<head><title>温馨提示</title></head>";
  24. echo "<br><h1>微信不支持直接下载,请点击右上角“---”在外部浏览器中打开</h1>";
  25. return ;
  26. }
  27. $d->where(" sign = '%s' ",array($sign))->save(array("visit_times" => $ret['visit_times'] + 1 ,"last_visit_time"=>time()));
  28. //记录用户流量
  29. D("Attachment")->recordUserFlow($ret['uid'] , $ret['file_size']) ;
  30. //$ret['cache_url'] = '' ; //把这个变量赋值为空,禁用掉cache_url;
  31. if ($ret['cache_url']) {
  32. $url = $ret['cache_url'] ;
  33. }else{
  34. $url = $ret['real_url'] ;
  35. }
  36. $array = explode("/Public/Uploads/", $url) ;
  37. $file_path = "../Public/Uploads/".$array[1] ;
  38. $oss_open = D("Options")->get("oss_open" ) ;
  39. if (!$oss_open
  40. && file_exists($file_path)
  41. && $ret['display_name']
  42. && !strstr(strtolower($file_path),'.bmp')
  43. && !strstr(strtolower($file_path),'.jpg')
  44. && !strstr(strtolower($file_path),'.png')
  45. && !strstr(strtolower($file_path),'.pdf')
  46. ) {
  47. $this->_downloadFile($file_path, $ret['display_name']);
  48. }else{
  49. header("location:{$url}");
  50. }
  51. }else{
  52. echo "www.showdoc.cc";
  53. }
  54. }
  55. //上传图片
  56. public function uploadImg(){
  57. $login_user = $this->checkLogin();
  58. $item_id = I("item_id/d") ? I("item_id/d") : 0 ;
  59. $page_id = I("page_id/d") ? I("page_id/d") : 0 ;
  60. if ($_FILES['editormd-image-file']['name'] == 'blob') {
  61. $_FILES['editormd-image-file']['name'] .= '.jpg';
  62. }
  63. if (!$_FILES['editormd-image-file']) {
  64. return false;
  65. }
  66. if (strstr(strip_tags(strtolower($_FILES['editormd-image-file']['name'])), ".php") || strstr(strip_tags(strtolower($_FILES['editormd-image-file']['name'])), ".htm") ) {
  67. return false;
  68. }
  69. $url = D("Attachment")->upload($_FILES , 'editormd-image-file' , $login_user['uid'] , $item_id , $page_id ) ;
  70. if ($url) {
  71. echo json_encode(array("url"=>$url,"success"=>1));
  72. }
  73. }
  74. //上传附件
  75. public function attachmentUpload(){
  76. $login_user = $this->checkLogin();
  77. $item_id = I("item_id/d") ? I("item_id/d") : 0 ;
  78. $page_id = I("page_id/d") ? I("page_id/d") : 0 ;
  79. $uploadFile = $_FILES['file'] ;
  80. // 如果附件是要上传绑定到某个页面,那么检验项目权限。如果不绑定,只是上传到自己的文件库,则不需要校验项目权限
  81. if( $page_id > 0 || $item_id > 0){
  82. if (!$this->checkItemEdit($login_user['uid'] , $item_id)) {
  83. $this->sendError(10103);
  84. return;
  85. }
  86. }
  87. if (!$uploadFile) {
  88. return false;
  89. }
  90. if (strstr(strip_tags(strtolower($uploadFile['name'])), ".php") || strstr(strip_tags(strtolower($uploadFile['name'])), ".htm") ) {
  91. $this->sendError(10100,'不支持此文件类型');
  92. return false;
  93. }
  94. $url = D("Attachment")->upload($_FILES , 'file' , $login_user['uid'] , $item_id , $page_id ) ;
  95. if ($url) {
  96. echo json_encode(array("url"=>$url,"success"=>1));
  97. }
  98. }
  99. //页面的上传附件列表
  100. public function pageAttachmentUploadList(){
  101. $login_user = $this->checkLogin(false);
  102. $item_id = I("item_id/d") ? I("item_id/d") : 0 ;
  103. $page_id = I("page_id/d") ? I("page_id/d") : 0 ;
  104. if (!$page_id) {
  105. $this->sendError(10103,"请至少先保存一次页面内容");
  106. return;
  107. }
  108. $return = array() ;
  109. $files = D("UploadFile")->join(" file_page on file_page.file_id = upload_file.file_id")->field("upload_file.* , file_page.item_id as item_id ,file_page.page_id as page_id ")->where("file_page.page_id = '$page_id' ")->order("file_page.addtime desc")->select();
  110. if ($files) {
  111. $item_id = $files[0]['item_id'] ;
  112. if (!$this->checkItemVisit($login_user['uid'] , $item_id)) {
  113. $this->sendError(10103);
  114. return;
  115. }
  116. foreach ($files as $key => $value) {
  117. $url = '';
  118. if($value['sign']){
  119. $url = get_domain().U("api/attachment/visitFile",array("sign" => $value['sign'])) ;
  120. }else{
  121. $url = $value['real_url'] ;
  122. }
  123. $return[] = array(
  124. "file_id"=>$value['file_id'],
  125. "display_name"=>$value['display_name'],
  126. "url"=>$url,
  127. "addtime"=> date("Y-m-d H:i:s" , $value['addtime'] ),
  128. );
  129. }
  130. }
  131. $this->sendResult($return);
  132. }
  133. //删除页面中已上传文件
  134. public function deletePageUploadFile(){
  135. $login_user = $this->checkLogin();
  136. $file_id = I("file_id/d") ? I("file_id/d") : 0 ;
  137. $page_id = I("page_id/d") ? I("page_id/d") : 0 ;
  138. $count = D("FilePage")->where(" file_id = '$file_id' and page_id > 0 ")->count() ;
  139. if($count <= 1 ){
  140. $this->deleteMyAttachment();
  141. }else{
  142. $page = M("Page")->where(" page_id = '$page_id' ")->find();
  143. if (!$this->checkItemEdit($login_user['uid'] , $page['item_id'])) {
  144. $this->sendError(10103);
  145. return;
  146. }
  147. $res = D("FilePage")->where(" file_id = '$file_id' and page_id = '$page_id' ")->delete() ;
  148. if($res){
  149. $this->sendResult(array());
  150. }else{
  151. $this->sendError(10101,"删除失败");
  152. }
  153. }
  154. }
  155. //获取全站的附件列表。给管理员查看附件用
  156. public function getAllList(){
  157. $login_user = $this->checkLogin();
  158. $this->checkAdmin(); //重要,校验管理员身份
  159. $page = I("page/d");
  160. $count = I("count/d");
  161. $attachment_type = I("attachment_type/d");
  162. $display_name = I("display_name");
  163. $username = I("username");
  164. $return = array() ;
  165. $where = ' 1 = 1 ';
  166. if($attachment_type == 1 ){
  167. $where .=" and file_type like '%image%' " ;
  168. }
  169. if($attachment_type == 2 ){
  170. $where .=" and file_type not like '%image%' " ;
  171. }
  172. if($display_name){
  173. $display_name = \SQLite3::escapeString($display_name) ;
  174. $where .=" and display_name like '%{$display_name}%' " ;
  175. }
  176. if($username){
  177. $username = \SQLite3::escapeString($username) ;
  178. $uid = D("User")->where(" username = '{$username}' ")->getField('uid') ;
  179. $uid = $uid ? $uid : -99 ;
  180. $where .=" and uid = '{$uid}' " ;
  181. }
  182. $files = D("UploadFile")->where($where)->order("addtime desc")->page($page ,$count)->select();
  183. if ($files) {
  184. foreach ($files as $key => $value) {
  185. $username = '';
  186. if($value['uid']){
  187. $username = D("User")->where(" uid = {$value['uid']} ")->getField('username') ;
  188. }
  189. $url = '';
  190. if($value['sign']){
  191. $url = get_domain().U("api/attachment/visitFile",array("sign" => $value['sign'])) ;
  192. }else{
  193. $url = $value['real_url'] ;
  194. }
  195. $return['list'][] = array(
  196. "file_id"=>$value['file_id'],
  197. "username"=>$username,
  198. "uid"=>$value['uid'],
  199. "file_type"=>$value['file_type'],
  200. "visit_times"=>$value['visit_times'],
  201. "file_size"=>$value['file_size'],
  202. "item_id"=>$value['item_id'],
  203. "page_id"=>$value['page_id'],
  204. "file_size_m"=>round( $value['file_size']/(1024*1024),3),
  205. "display_name"=>$value['display_name']?$value['display_name']:'',
  206. "url"=>$url ,
  207. "addtime"=> date("Y-m-d H:i:s" , $value['addtime'] ),
  208. "last_visit_time"=> date("Y-m-d H:i:s" , $value['last_visit_time'] ),
  209. );
  210. }
  211. }
  212. $return['total'] = D("UploadFile")->where($where)->count();
  213. $used = D("UploadFile")->where($where)->getField('sum(file_size)');
  214. $return['used'] = $used ;
  215. $return['used_m'] = round( $used/(1024*1024),3) ;
  216. $this->sendResult($return);
  217. }
  218. //删除附件
  219. public function deleteAttachment(){
  220. $login_user = $this->checkLogin();
  221. $this->checkAdmin(); //重要,校验管理员身份
  222. $file_id = I("file_id/d") ? I("file_id/d") : 0 ;
  223. $file = D("UploadFile")->where("file_id = '$file_id' ")->find();
  224. $ret = D("Attachment")->deleteFile($file_id);
  225. if ($ret) {
  226. $this->sendResult(array());
  227. }else{
  228. $this->sendError(10101,"删除失败");
  229. }
  230. }
  231. //获取我的附件列表
  232. public function getMyList(){
  233. $login_user = $this->checkLogin();
  234. $page = I("page/d");
  235. $count = I("count/d");
  236. $attachment_type = I("attachment_type/d");
  237. $display_name = I("display_name");
  238. $username = I("username");
  239. $return = array() ;
  240. $where = " uid = {$login_user['uid']} ";
  241. if($attachment_type == 1 ){
  242. $where .=" and file_type like '%image%' " ;
  243. }
  244. if($attachment_type == 2 ){
  245. $where .=" and file_type not like '%image%' " ;
  246. }
  247. if($display_name){
  248. $display_name = \SQLite3::escapeString($display_name) ;
  249. $where .=" and display_name like '%{$display_name}%' " ;
  250. }
  251. $files = D("UploadFile")->where($where)->order("addtime desc")->page($page ,$count)->select();
  252. if ($files) {
  253. foreach ($files as $key => $value) {
  254. $username = '';
  255. $return['list'][] = array(
  256. "file_id"=>$value['file_id'],
  257. "uid"=>$value['uid'],
  258. "file_type"=>$value['file_type'],
  259. "visit_times"=>$value['visit_times'],
  260. "file_size"=>$value['file_size'],
  261. "item_id"=>$value['item_id'],
  262. "page_id"=>$value['page_id'],
  263. "file_size_m"=>round( $value['file_size']/(1024*1024),3),
  264. "display_name"=>$value['display_name']?$value['display_name']:'',
  265. "url"=>get_domain().U("api/attachment/visitFile",array("sign" => $value['sign'])),
  266. "addtime"=> date("Y-m-d H:i:s" , $value['addtime'] ),
  267. "last_visit_time"=> date("Y-m-d H:i:s" , $value['last_visit_time'] ),
  268. );
  269. }
  270. }
  271. $return['total'] = D("UploadFile")->where($where)->count();
  272. $used = D("UploadFile")->where($where)->getField('sum(file_size)');
  273. $return['used'] = $used ;
  274. $return['used_m'] = round( $used/(1024*1024),3) ;
  275. $used_flow = D("Attachment")->getUserFlow($login_user['uid']) ; ; //该用户的本月使用流量
  276. $return['used_flow_m'] = round( $used_flow/(1024*1024),3) ;
  277. $this->sendResult($return);
  278. }
  279. //删除附件
  280. public function deleteMyAttachment(){
  281. $login_user = $this->checkLogin();
  282. $file_id = I("file_id/d") ? I("file_id/d") : 0 ;
  283. $file = D("UploadFile")->where("file_id = '$file_id' and uid ='$login_user[uid]' ")->find();
  284. if($file){
  285. $ret = D("Page")->deleteFile($file_id);
  286. if ($ret) {
  287. $this->sendResult(array());
  288. return ;
  289. }
  290. }
  291. $this->sendError(10101,"删除失败");
  292. }
  293. //将已上传文件绑定到页面中
  294. public function bindingPage(){
  295. $login_user = $this->checkLogin();
  296. $file_id = I("file_id/d") ? I("file_id/d") : 0 ;
  297. $page_id = I("page_id/d");
  298. $file = D("UploadFile")->where("file_id = '$file_id' and uid ='$login_user[uid]' ")->find();
  299. $page = M("Page")->where(" page_id = '$page_id' ")->find();
  300. if (!$this->checkItemEdit($login_user['uid'] , $page['item_id'])) {
  301. $this->sendError(10103);
  302. return;
  303. }
  304. $insert = array(
  305. "file_id" => $file_id,
  306. "item_id" => $page['item_id'] ,
  307. "page_id" => $page_id,
  308. "addtime" => time(),
  309. );
  310. $ret = D("FilePage")->add($insert);
  311. if( $ret){
  312. $this->sendResult(array());
  313. }else{
  314. $this->sendError(10101);
  315. }
  316. }
  317. //输出本地文件到浏览器
  318. public function _downloadFile($filename, $rename='showdoc') {
  319. //设置脚本的最大执行时间,设置为0则无时间限制
  320. set_time_limit(3000);
  321. ini_set('max_execution_time', '0');
  322. //通过header()发送头信息
  323. //因为不知道文件是什么类型的,告诉浏览器输出的是字节流
  324. header('content-type:application/octet-stream');
  325. //告诉浏览器返回的文件大小类型是字节
  326. header('Accept-Ranges:bytes');
  327. //获得文件大小
  328. $filesize = filesize($filename);//(此方法无法获取到远程文件大小),远程文件用下面get_headers方法
  329. //$header_array = get_headers($filename, true);
  330. //$filesize = $header_array['Content-Length'];
  331. //var_dump($header_array);exit();
  332. //告诉浏览器返回的文件大小
  333. header('Accept-Length:'.$filesize);
  334. //告诉浏览器文件作为附件处理并且设定最终下载完成的文件名称
  335. header('content-disposition:attachment;filename='.basename($rename));
  336. //针对大文件,规定每次读取文件的字节数为4096字节,直接输出数据
  337. $read_buffer = 4096;
  338. $handle = fopen($filename, 'rb');
  339. //总的缓冲的字节数
  340. $sum_buffer = 0;
  341. //只要没到文件尾,就一直读取
  342. while(!feof($handle) && $sum_buffer<$filesize) {
  343. echo fread($handle,$read_buffer);
  344. $sum_buffer += $read_buffer;
  345. }
  346. //关闭句柄
  347. fclose($handle);
  348. }
  349. }