AttachmentController.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. $oss_open = D("Options")->get("oss_open" ) ;
  56. if ($oss_open) {
  57. $uploadFile = $_FILES['editormd-image-file'] ;
  58. $url = upload_oss($uploadFile);
  59. if ($url) {
  60. $sign = md5($url.time().rand()) ;
  61. $insert = array(
  62. "sign" => $sign,
  63. "uid" => $login_user['uid'],
  64. "item_id" => $item_id,
  65. "page_id" => $page_id,
  66. "display_name" => $uploadFile['name'],
  67. "file_type" => $uploadFile['type'],
  68. "file_size" => $uploadFile['size'],
  69. "real_url" => $url,
  70. "addtime" => time(),
  71. );
  72. $ret = D("UploadFile")->add($insert);
  73. $url = get_domain().U("api/attachment/visitFile",array("sign" => $sign))."&showdoc=.jpg";
  74. echo json_encode(array("url"=>$url,"success"=>1));
  75. }
  76. return ;
  77. }
  78. $upload = new \Think\Upload();// 实例化上传类
  79. $upload->maxSize = 1003145728 ;// 设置附件上传大小
  80. $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
  81. $upload->rootPath = './../Public/Uploads/';// 设置附件上传目录
  82. $upload->savePath = '';// 设置附件上传子目录
  83. $info = $upload->uploadOne($_FILES['editormd-image-file']) ;
  84. if(!$info) {// 上传错误提示错误信息
  85. $this->error($upload->getError());
  86. return;
  87. }else{// 上传成功 获取上传文件信息
  88. $url = get_domain().__ROOT__.substr($upload->rootPath,1).$info['savepath'].$info['savename'] ;
  89. $sign = md5($url.time().rand()) ;
  90. $uploadFile = $_FILES['editormd-image-file'] ;
  91. $insert = array(
  92. "sign" => $sign,
  93. "uid" => $login_user['uid'],
  94. "item_id" => $item_id,
  95. "page_id" => $page_id,
  96. "display_name" => $uploadFile['name'],
  97. "file_type" => $uploadFile['type'],
  98. "file_size" => $uploadFile['size'],
  99. "real_url" => $url,
  100. "addtime" => time(),
  101. );
  102. $ret = D("UploadFile")->add($insert);
  103. $url = get_domain().U("api/attachment/visitFile",array("sign" => $sign))."&showdoc=.jpg";
  104. echo json_encode(array("url"=>$url,"success"=>1));
  105. }
  106. }
  107. //页面的上传附件
  108. public function pageAttachmentUpload(){
  109. $login_user = $this->checkLogin();
  110. $item_id = I("item_id/d") ? I("item_id/d") : 0 ;
  111. $page_id = I("page_id/d") ? I("page_id/d") : 0 ;
  112. $uploadFile = $_FILES['file'] ;
  113. if (!$page_id) {
  114. $this->sendError(10103,"请至少先保存一次页面内容");
  115. return;
  116. }
  117. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  118. $this->sendError(10103);
  119. return;
  120. }
  121. if (!$uploadFile) {
  122. return false;
  123. }
  124. if (strstr(strip_tags(strtolower($uploadFile['name'])), ".php") ) {
  125. return false;
  126. }
  127. $oss_open = D("Options")->get("oss_open" ) ;
  128. if ($oss_open) {
  129. $url = upload_oss($uploadFile);
  130. if ($url) {
  131. $sign = md5($url.time().rand()) ;
  132. $insert = array(
  133. "sign" => $sign,
  134. "uid" => $login_user['uid'],
  135. "item_id" => $item_id,
  136. "page_id" => $page_id,
  137. "display_name" => $uploadFile['name'],
  138. "file_type" => $uploadFile['type'],
  139. "file_size" => $uploadFile['size'],
  140. "real_url" => $url,
  141. "addtime" => time(),
  142. );
  143. $ret = D("UploadFile")->add($insert);
  144. $url = get_domain().U("api/attachment/visitFile",array("sign" => $sign));
  145. echo json_encode(array("url"=>$url,"success"=>1));
  146. }
  147. return ;
  148. }
  149. $upload = new \Think\Upload();// 实例化上传类
  150. $upload->maxSize = 4145728000 ;// 设置附件上传大小
  151. $upload->rootPath = './../Public/Uploads/';// 设置附件上传目录
  152. $upload->savePath = '';// 设置附件上传子目录
  153. $info = $upload->uploadOne($uploadFile) ;
  154. if(!$info) {// 上传错误提示错误信息
  155. $this->error($upload->getError());
  156. return;
  157. }else{// 上传成功 获取上传文件信息
  158. $url = get_domain().__ROOT__.substr($upload->rootPath,1).$info['savepath'].$info['savename'] ;
  159. $sign = md5($url.time().rand()) ;
  160. $insert = array(
  161. "sign" => $sign,
  162. "uid" => $login_user['uid'],
  163. "item_id" => $item_id,
  164. "page_id" => $page_id,
  165. "display_name" => $uploadFile['name'],
  166. "file_type" => $uploadFile['type'],
  167. "file_size" => $uploadFile['size'],
  168. "real_url" => $url,
  169. "addtime" => time(),
  170. );
  171. $ret = D("UploadFile")->add($insert);
  172. $url = get_domain().U("api/attachment/visitFile",array("sign" => $sign));
  173. echo json_encode(array("url"=>$url,"success"=>1));
  174. }
  175. }
  176. //页面的上传附件列表
  177. public function pageAttachmentUploadList(){
  178. $login_user = $this->checkLogin();
  179. $item_id = I("item_id/d") ? I("item_id/d") : 0 ;
  180. $page_id = I("page_id/d") ? I("page_id/d") : 0 ;
  181. if (!$page_id) {
  182. $this->sendError(10103,"请至少先保存一次页面内容");
  183. return;
  184. }
  185. $return = array() ;
  186. $files = D("UploadFile")->where("page_id = '$page_id' ")->order("addtime desc")->select();
  187. if ($files) {
  188. $item_id = $files[0]['item_id'] ;
  189. if (!$this->checkItemVisit($login_user['uid'] , $item_id)) {
  190. $this->sendError(10103);
  191. return;
  192. }
  193. foreach ($files as $key => $value) {
  194. $url = '';
  195. if($value['sign']){
  196. $url = get_domain().U("api/attachment/visitFile",array("sign" => $value['sign'])) ;
  197. }else{
  198. $url = $value['real_url'] ;
  199. }
  200. $return[] = array(
  201. "file_id"=>$value['file_id'],
  202. "display_name"=>$value['display_name'],
  203. "url"=>$url,
  204. "addtime"=> date("Y-m-d H:i:s" , $value['addtime'] ),
  205. );
  206. }
  207. }
  208. $this->sendResult($return);
  209. }
  210. //删除页面中已上传文件
  211. public function deletePageUploadFile(){
  212. $login_user = $this->checkLogin();
  213. $file_id = I("file_id/d") ? I("file_id/d") : 0 ;
  214. $file = D("UploadFile")->where("file_id = '$file_id' ")->find();
  215. $item_id = $file['item_id'] ;
  216. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  217. $this->sendError(10103);
  218. return;
  219. }
  220. $ret = D("Page")->deleteFile($file_id);
  221. if ($ret) {
  222. $this->sendResult(array());
  223. }else{
  224. $this->sendError(10101,"删除失败");
  225. }
  226. }
  227. //获取全站的附件列表。给管理员查看附件用
  228. public function getAllList(){
  229. $login_user = $this->checkLogin();
  230. $this->checkAdmin(); //重要,校验管理员身份
  231. $page = I("page/d");
  232. $count = I("count/d");
  233. $attachment_type = I("attachment_type/d");
  234. $display_name = I("display_name");
  235. $username = I("username");
  236. $return = array() ;
  237. $where = ' 1 = 1 ';
  238. if($attachment_type == 1 ){
  239. $where .=" and file_type like '%image%' " ;
  240. }
  241. if($attachment_type == 2 ){
  242. $where .=" and file_type not like '%image%' " ;
  243. }
  244. if($display_name){
  245. $display_name = \SQLite3::escapeString($display_name) ;
  246. $where .=" and display_name like '%{$display_name}%' " ;
  247. }
  248. if($username){
  249. $username = \SQLite3::escapeString($username) ;
  250. $uid = D("User")->where(" username = '{$username}' ")->getField('uid') ;
  251. $uid = $uid ? $uid : -99 ;
  252. $where .=" and uid = '{$uid}' " ;
  253. }
  254. $files = D("UploadFile")->where($where)->order("addtime desc")->page($page ,$count)->select();
  255. if ($files) {
  256. foreach ($files as $key => $value) {
  257. $username = '';
  258. if($value['uid']){
  259. $username = D("User")->where(" uid = {$value['uid']} ")->getField('username') ;
  260. }
  261. $url = '';
  262. if($value['sign']){
  263. $url = get_domain().U("api/attachment/visitFile",array("sign" => $value['sign'])) ;
  264. }else{
  265. $url = $value['real_url'] ;
  266. }
  267. $return['list'][] = array(
  268. "file_id"=>$value['file_id'],
  269. "username"=>$username,
  270. "uid"=>$value['uid'],
  271. "file_type"=>$value['file_type'],
  272. "visit_times"=>$value['visit_times'],
  273. "file_size"=>$value['file_size'],
  274. "item_id"=>$value['item_id'],
  275. "page_id"=>$value['page_id'],
  276. "file_size_m"=>round( $value['file_size']/(1024*1024),3),
  277. "display_name"=>$value['display_name']?$value['display_name']:'',
  278. "url"=>$url ,
  279. "addtime"=> date("Y-m-d H:i:s" , $value['addtime'] ),
  280. "last_visit_time"=> date("Y-m-d H:i:s" , $value['last_visit_time'] ),
  281. );
  282. }
  283. }
  284. $return['total'] = D("UploadFile")->where($where)->count();
  285. $used = D("UploadFile")->where($where)->getField('sum(file_size)');
  286. $return['used'] = $used ;
  287. $return['used_m'] = round( $used/(1024*1024),3) ;
  288. $this->sendResult($return);
  289. }
  290. //删除附件
  291. public function deleteAttachment(){
  292. $login_user = $this->checkLogin();
  293. $this->checkAdmin(); //重要,校验管理员身份
  294. $file_id = I("file_id/d") ? I("file_id/d") : 0 ;
  295. $file = D("UploadFile")->where("file_id = '$file_id' ")->find();
  296. $ret = D("Page")->deleteFile($file_id);
  297. if ($ret) {
  298. $this->sendResult(array());
  299. }else{
  300. $this->sendError(10101,"删除失败");
  301. }
  302. }
  303. //获取我的附件列表
  304. public function getMyList(){
  305. $login_user = $this->checkLogin();
  306. $page = I("page/d");
  307. $count = I("count/d");
  308. $attachment_type = I("attachment_type/d");
  309. $display_name = I("display_name");
  310. $username = I("username");
  311. $return = array() ;
  312. $where = " uid = '{$login_user[uid]}' ";
  313. if($attachment_type == 1 ){
  314. $where .=" and file_type like '%image%' " ;
  315. }
  316. if($attachment_type == 2 ){
  317. $where .=" and file_type not like '%image%' " ;
  318. }
  319. if($display_name){
  320. $display_name = \SQLite3::escapeString($display_name) ;
  321. $where .=" and display_name like '%{$display_name}%' " ;
  322. }
  323. $files = D("UploadFile")->where($where)->order("addtime desc")->page($page ,$count)->select();
  324. if ($files) {
  325. foreach ($files as $key => $value) {
  326. $username = '';
  327. $return['list'][] = array(
  328. "file_id"=>$value['file_id'],
  329. "uid"=>$value['uid'],
  330. "file_type"=>$value['file_type'],
  331. "visit_times"=>$value['visit_times'],
  332. "file_size"=>$value['file_size'],
  333. "item_id"=>$value['item_id'],
  334. "page_id"=>$value['page_id'],
  335. "file_size_m"=>round( $value['file_size']/(1024*1024),3),
  336. "display_name"=>$value['display_name']?$value['display_name']:'',
  337. "url"=>get_domain().U("api/attachment/visitFile",array("sign" => $value['sign'])),
  338. "addtime"=> date("Y-m-d H:i:s" , $value['addtime'] ),
  339. "last_visit_time"=> date("Y-m-d H:i:s" , $value['last_visit_time'] ),
  340. );
  341. }
  342. }
  343. $return['total'] = D("UploadFile")->where($where)->count();
  344. $used = D("UploadFile")->where($where)->getField('sum(file_size)');
  345. $return['used'] = $used ;
  346. $return['used_m'] = round( $used/(1024*1024),3) ;
  347. $used_flow = D("Attachment")->getUserFlow($login_user['uid']) ; ; //该用户的本月使用流量
  348. $return['used_flow_m'] = round( $used_flow/(1024*1024),3) ;
  349. $this->sendResult($return);
  350. }
  351. //删除附件
  352. public function deleteMyAttachment(){
  353. $login_user = $this->checkLogin();
  354. $file_id = I("file_id/d") ? I("file_id/d") : 0 ;
  355. $file = D("UploadFile")->where("file_id = '$file_id' and uid ='$login_user[uid]' ")->find();
  356. if($file){
  357. $ret = D("Page")->deleteFile($file_id);
  358. if ($ret) {
  359. $this->sendResult(array());
  360. return ;
  361. }
  362. }
  363. $this->sendError(10101,"删除失败");
  364. }
  365. }