AttachmentController.class.php 15 KB

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