25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

344 lines
13 KiB

  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. header("location:{$url}");
  37. }else{
  38. echo "www.showdoc.cc";
  39. }
  40. }
  41. //上传图片
  42. public function uploadImg(){
  43. $login_user = $this->checkLogin();
  44. $item_id = I("item_id/d") ? I("item_id/d") : 0 ;
  45. $page_id = I("page_id/d") ? I("page_id/d") : 0 ;
  46. if ($_FILES['editormd-image-file']['name'] == 'blob') {
  47. $_FILES['editormd-image-file']['name'] .= '.jpg';
  48. }
  49. if (!$_FILES['editormd-image-file']) {
  50. return false;
  51. }
  52. if (strstr(strip_tags(strtolower($_FILES['editormd-image-file']['name'])), ".php") ) {
  53. return false;
  54. }
  55. $url = D("Attachment")->upload($_FILES , 'editormd-image-file' , $login_user['uid'] , $item_id , $page_id ) ;
  56. if ($url) {
  57. echo json_encode(array("url"=>$url,"success"=>1));
  58. }
  59. }
  60. //上传附件
  61. public function attachmentUpload(){
  62. $login_user = $this->checkLogin();
  63. $item_id = I("item_id/d") ? I("item_id/d") : 0 ;
  64. $page_id = I("page_id/d") ? I("page_id/d") : 0 ;
  65. $uploadFile = $_FILES['file'] ;
  66. // 如果附件是要上传绑定到某个页面,那么检验项目权限。如果不绑定,只是上传到自己的文件库,则不需要校验项目权限
  67. if( $page_id > 0 || $item_id > 0){
  68. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  69. $this->sendError(10103);
  70. return;
  71. }
  72. }
  73. if (!$uploadFile) {
  74. return false;
  75. }
  76. if (strstr(strip_tags(strtolower($uploadFile['name'])), ".php") ) {
  77. return false;
  78. }
  79. $url = D("Attachment")->upload($_FILES , 'file' , $login_user['uid'] , $item_id , $page_id ) ;
  80. if ($url) {
  81. echo json_encode(array("url"=>$url,"success"=>1));
  82. }
  83. }
  84. //页面的上传附件列表
  85. public function pageAttachmentUploadList(){
  86. $login_user = $this->checkLogin();
  87. $item_id = I("item_id/d") ? I("item_id/d") : 0 ;
  88. $page_id = I("page_id/d") ? I("page_id/d") : 0 ;
  89. if (!$page_id) {
  90. $this->sendError(10103,"请至少先保存一次页面内容");
  91. return;
  92. }
  93. $return = array() ;
  94. $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();
  95. if ($files) {
  96. $item_id = $files[0]['item_id'] ;
  97. if (!$this->checkItemVisit($login_user['uid'] , $item_id)) {
  98. $this->sendError(10103);
  99. return;
  100. }
  101. foreach ($files as $key => $value) {
  102. $url = '';
  103. if($value['sign']){
  104. $url = get_domain().U("api/attachment/visitFile",array("sign" => $value['sign'])) ;
  105. }else{
  106. $url = $value['real_url'] ;
  107. }
  108. $return[] = array(
  109. "file_id"=>$value['file_id'],
  110. "display_name"=>$value['display_name'],
  111. "url"=>$url,
  112. "addtime"=> date("Y-m-d H:i:s" , $value['addtime'] ),
  113. );
  114. }
  115. }
  116. $this->sendResult($return);
  117. }
  118. //删除页面中已上传文件
  119. public function deletePageUploadFile(){
  120. $login_user = $this->checkLogin();
  121. $file_id = I("file_id/d") ? I("file_id/d") : 0 ;
  122. $page_id = I("page_id/d") ? I("page_id/d") : 0 ;
  123. $count = D("FilePage")->where(" file_id = '$file_id' and page_id > 0 ")->count() ;
  124. if($count <= 1 ){
  125. $this->deleteMyAttachment();
  126. }else{
  127. $page = M("Page")->where(" page_id = '$page_id' ")->find();
  128. if (!$this->checkItemPermn($login_user['uid'] , $page['item_id'])) {
  129. $this->sendError(10103);
  130. return;
  131. }
  132. $res = D("FilePage")->where(" file_id = '$file_id' and page_id = '$page_id' ")->delete() ;
  133. if($res){
  134. $this->sendResult(array());
  135. }else{
  136. $this->sendError(10101,"删除失败");
  137. }
  138. }
  139. }
  140. //获取全站的附件列表。给管理员查看附件用
  141. public function getAllList(){
  142. $login_user = $this->checkLogin();
  143. $this->checkAdmin(); //重要,校验管理员身份
  144. $page = I("page/d");
  145. $count = I("count/d");
  146. $attachment_type = I("attachment_type/d");
  147. $display_name = I("display_name");
  148. $username = I("username");
  149. $return = array() ;
  150. $where = ' 1 = 1 ';
  151. if($attachment_type == 1 ){
  152. $where .=" and file_type like '%image%' " ;
  153. }
  154. if($attachment_type == 2 ){
  155. $where .=" and file_type not like '%image%' " ;
  156. }
  157. if($display_name){
  158. $display_name = \SQLite3::escapeString($display_name) ;
  159. $where .=" and display_name like '%{$display_name}%' " ;
  160. }
  161. if($username){
  162. $username = \SQLite3::escapeString($username) ;
  163. $uid = D("User")->where(" username = '{$username}' ")->getField('uid') ;
  164. $uid = $uid ? $uid : -99 ;
  165. $where .=" and uid = '{$uid}' " ;
  166. }
  167. $files = D("UploadFile")->where($where)->order("addtime desc")->page($page ,$count)->select();
  168. if ($files) {
  169. foreach ($files as $key => $value) {
  170. $username = '';
  171. if($value['uid']){
  172. $username = D("User")->where(" uid = {$value['uid']} ")->getField('username') ;
  173. }
  174. $url = '';
  175. if($value['sign']){
  176. $url = get_domain().U("api/attachment/visitFile",array("sign" => $value['sign'])) ;
  177. }else{
  178. $url = $value['real_url'] ;
  179. }
  180. $return['list'][] = array(
  181. "file_id"=>$value['file_id'],
  182. "username"=>$username,
  183. "uid"=>$value['uid'],
  184. "file_type"=>$value['file_type'],
  185. "visit_times"=>$value['visit_times'],
  186. "file_size"=>$value['file_size'],
  187. "item_id"=>$value['item_id'],
  188. "page_id"=>$value['page_id'],
  189. "file_size_m"=>round( $value['file_size']/(1024*1024),3),
  190. "display_name"=>$value['display_name']?$value['display_name']:'',
  191. "url"=>$url ,
  192. "addtime"=> date("Y-m-d H:i:s" , $value['addtime'] ),
  193. "last_visit_time"=> date("Y-m-d H:i:s" , $value['last_visit_time'] ),
  194. );
  195. }
  196. }
  197. $return['total'] = D("UploadFile")->where($where)->count();
  198. $used = D("UploadFile")->where($where)->getField('sum(file_size)');
  199. $return['used'] = $used ;
  200. $return['used_m'] = round( $used/(1024*1024),3) ;
  201. $this->sendResult($return);
  202. }
  203. //删除附件
  204. public function deleteAttachment(){
  205. $login_user = $this->checkLogin();
  206. $this->checkAdmin(); //重要,校验管理员身份
  207. $file_id = I("file_id/d") ? I("file_id/d") : 0 ;
  208. $file = D("UploadFile")->where("file_id = '$file_id' ")->find();
  209. $ret = D("Attachment")->deleteFile($file_id);
  210. if ($ret) {
  211. $this->sendResult(array());
  212. }else{
  213. $this->sendError(10101,"删除失败");
  214. }
  215. }
  216. //获取我的附件列表
  217. public function getMyList(){
  218. $login_user = $this->checkLogin();
  219. $page = I("page/d");
  220. $count = I("count/d");
  221. $attachment_type = I("attachment_type/d");
  222. $display_name = I("display_name");
  223. $username = I("username");
  224. $return = array() ;
  225. $where = " uid = {$login_user['uid']} ";
  226. if($attachment_type == 1 ){
  227. $where .=" and file_type like '%image%' " ;
  228. }
  229. if($attachment_type == 2 ){
  230. $where .=" and file_type not like '%image%' " ;
  231. }
  232. if($display_name){
  233. $display_name = \SQLite3::escapeString($display_name) ;
  234. $where .=" and display_name like '%{$display_name}%' " ;
  235. }
  236. $files = D("UploadFile")->where($where)->order("addtime desc")->page($page ,$count)->select();
  237. if ($files) {
  238. foreach ($files as $key => $value) {
  239. $username = '';
  240. $return['list'][] = array(
  241. "file_id"=>$value['file_id'],
  242. "uid"=>$value['uid'],
  243. "file_type"=>$value['file_type'],
  244. "visit_times"=>$value['visit_times'],
  245. "file_size"=>$value['file_size'],
  246. "item_id"=>$value['item_id'],
  247. "page_id"=>$value['page_id'],
  248. "file_size_m"=>round( $value['file_size']/(1024*1024),3),
  249. "display_name"=>$value['display_name']?$value['display_name']:'',
  250. "url"=>get_domain().U("api/attachment/visitFile",array("sign" => $value['sign'])),
  251. "addtime"=> date("Y-m-d H:i:s" , $value['addtime'] ),
  252. "last_visit_time"=> date("Y-m-d H:i:s" , $value['last_visit_time'] ),
  253. );
  254. }
  255. }
  256. $return['total'] = D("UploadFile")->where($where)->count();
  257. $used = D("UploadFile")->where($where)->getField('sum(file_size)');
  258. $return['used'] = $used ;
  259. $return['used_m'] = round( $used/(1024*1024),3) ;
  260. $used_flow = D("Attachment")->getUserFlow($login_user['uid']) ; ; //该用户的本月使用流量
  261. $return['used_flow_m'] = round( $used_flow/(1024*1024),3) ;
  262. $this->sendResult($return);
  263. }
  264. //删除附件
  265. public function deleteMyAttachment(){
  266. $login_user = $this->checkLogin();
  267. $file_id = I("file_id/d") ? I("file_id/d") : 0 ;
  268. $file = D("UploadFile")->where("file_id = '$file_id' and uid ='$login_user[uid]' ")->find();
  269. if($file){
  270. $ret = D("Page")->deleteFile($file_id);
  271. if ($ret) {
  272. $this->sendResult(array());
  273. return ;
  274. }
  275. }
  276. $this->sendError(10101,"删除失败");
  277. }
  278. //将已上传文件绑定到页面中
  279. public function bindingPage(){
  280. $login_user = $this->checkLogin();
  281. $file_id = I("file_id/d") ? I("file_id/d") : 0 ;
  282. $page_id = I("page_id/d");
  283. $file = D("UploadFile")->where("file_id = '$file_id' and uid ='$login_user[uid]' ")->find();
  284. $page = M("Page")->where(" page_id = '$page_id' ")->find();
  285. if (!$this->checkItemPermn($login_user['uid'] , $page['item_id'])) {
  286. $this->sendError(10103);
  287. return;
  288. }
  289. $insert = array(
  290. "file_id" => $file_id,
  291. "item_id" => $page['item_id'] ,
  292. "page_id" => $page_id,
  293. "addtime" => time(),
  294. );
  295. $ret = D("FilePage")->add($insert);
  296. if( $ret){
  297. $this->sendResult(array());
  298. }else{
  299. $this->sendError(10101);
  300. }
  301. }
  302. }