file.ctrl.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. load()->func('file');
  8. $do = in_array($_GPC['do'], array('upload', 'delete')) ? $_GPC['do'] : 'upload';
  9. $type = in_array($_GPC['type'], array('image','audio')) ? $_GPC['type'] : 'image';
  10. $result = array('error' => 1, 'message' => '');
  11. if ($do == 'delete') {
  12. if ($type = 'image') {
  13. $id = intval($_GPC['id']);
  14. if (empty($id)) {
  15. return message($result, '', 'ajax');
  16. }
  17. $attachment = pdo_get('core_attachment', array('id' => $id, 'uniacid' => $_W['uniacid']), array('attachment', 'uniacid', 'uid'));
  18. if (empty($attachment)) {
  19. return message(error(1, '图片不存在或已删除!'), '', 'ajax');
  20. }
  21. if (empty($_W['openid']) || (!empty($_W['fans']) && $attachment['uid'] != $_W['fans']['from_user']) || (!empty($_W['member']) && $attachment['uid'] != $_W['member']['uid'])) {
  22. return message(error(1, '无权删除!'), '', 'ajax');
  23. }
  24. $uni_remote_setting = uni_setting_load('remote');
  25. if (!empty($uni_remote_setting['remote']['type'])) {
  26. $_W['setting']['remote'] = $uni_remote_setting['remote'];
  27. }
  28. if ($_W['setting']['remote']['type']) {
  29. $result = file_remote_delete($attachment['attachment']);
  30. } else {
  31. $result = file_delete($attachment['attachment']);
  32. }
  33. if (!is_error($result)) {
  34. pdo_delete('core_attachment', array('id' => $id, 'uniacid' => $_W['uniacid']));
  35. }
  36. if (!is_error($result)) {
  37. return message(error('0'), '', 'ajax');
  38. } else {
  39. return message(error(1, $result['message']), '', 'ajax');
  40. }
  41. }
  42. }
  43. if ($do == 'upload') {
  44. if($type == 'image'){
  45. $setting = $_W['setting']['upload'][$type];
  46. $result = array(
  47. 'jsonrpc' => '2.0',
  48. 'id' => 'id',
  49. 'error' => array('code' => 1, 'message'=>''),
  50. );
  51. if (empty($_FILES['file']['tmp_name'])) {
  52. $binaryfile = file_get_contents('php://input', 'r');
  53. if (!empty($binaryfile)) {
  54. mkdirs(ATTACHMENT_ROOT . '/temp');
  55. $tempfilename = random(5);
  56. $tempfile = ATTACHMENT_ROOT . '/temp/' . $tempfilename;
  57. if (file_put_contents($tempfile, $binaryfile)) {
  58. $imagesize = @getimagesize($tempfile);
  59. $imagesize = explode('/', $imagesize['mime']);
  60. $_FILES['file'] = array(
  61. 'name' => $tempfilename . '.' . $imagesize[1],
  62. 'tmp_name' => $tempfile,
  63. 'error' => 0,
  64. );
  65. }
  66. }
  67. }
  68. if (!empty($_FILES['file']['name'])) {
  69. if ($_FILES['file']['error'] != 0) {
  70. $result['error']['message'] = '上传失败,请重试!';
  71. die(json_encode($result));
  72. }
  73. if (!file_is_image($_FILES['file']['tmp_name'])) {
  74. $result['message'] = '上传失败, 请重试.';
  75. die(json_encode($result));
  76. }
  77. $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
  78. $ext = strtolower($ext);
  79. $file = file_upload($_FILES['file']);
  80. if (is_error($file)) {
  81. $result['error']['message'] = $file['message'];
  82. die(json_encode($result));
  83. }
  84. $pathname = $file['path'];
  85. $fullname = ATTACHMENT_ROOT . '/' . $pathname;
  86. $thumb = empty($setting['thumb']) || $ext == 'gif' ? 0 : 1; $width = intval($setting['width']); if ($thumb == 1 && $width > 0 && (!isset($_GPC['thumb']) || (isset($_GPC['thumb']) && !empty($_GPC['thumb'])))) {
  87. $thumbnail = file_image_thumb($fullname, '', $width);
  88. @unlink($fullname);
  89. if (is_error($thumbnail)) {
  90. $result['message'] = $thumbnail['message'];
  91. die(json_encode($result));
  92. } else {
  93. $filename = pathinfo($thumbnail, PATHINFO_BASENAME);
  94. $pathname = $thumbnail;
  95. $fullname = ATTACHMENT_ROOT .'/'.$pathname;
  96. }
  97. }
  98. $info = array(
  99. 'name' => $_FILES['file']['name'],
  100. 'ext' => $ext,
  101. 'filename' => $pathname,
  102. 'attachment' => $pathname,
  103. 'url' => tomedia($pathname),
  104. 'is_image' => 1,
  105. 'filesize' => filesize($fullname),
  106. );
  107. $size = getimagesize($fullname);
  108. $info['width'] = $size[0];
  109. $info['height'] = $size[1];
  110. setting_load('remote');
  111. $uni_remote_setting = uni_setting_load('remote');
  112. if (!empty($uni_remote_setting['remote']['type'])) {
  113. $_W['setting']['remote'] = $uni_remote_setting['remote'];
  114. }
  115. if (!empty($_W['setting']['remote']['type'])) {
  116. $remotestatus = file_remote_upload($pathname);
  117. if (is_error($remotestatus)) {
  118. $result['message'] = '远程附件上传失败,请检查配置并重新上传';
  119. file_delete($pathname);
  120. die(json_encode($result));
  121. } else {
  122. file_delete($pathname);
  123. $info['url'] = tomedia($pathname);
  124. }
  125. }
  126. pdo_insert('core_attachment', array(
  127. 'uniacid' => $uniacid,
  128. 'uid' => $_W['uid'],
  129. 'filename' => $_FILES['file']['name'],
  130. 'attachment' => $pathname,
  131. 'type' => $type == 'image' ? 1 : 2,
  132. 'createtime' => TIMESTAMP,
  133. ));
  134. $info['id'] = pdo_insertid();
  135. die(json_encode($info));
  136. } else {
  137. $result['error']['message'] = '请选择要上传的图片!';
  138. die(json_encode($result));
  139. }
  140. }
  141. }