function_attachment.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: function_attachment.php 28348 2012-02-28 06:16:29Z monkey $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. function attachtype($type, $returnval = 'html') {
  12. static $attachicons = array(
  13. 1 => 'unknown.gif',
  14. 2 => 'binary.gif',
  15. 3 => 'zip.gif',
  16. 4 => 'rar.gif',
  17. 5 => 'msoffice.gif',
  18. 6 => 'text.gif',
  19. 7 => 'html.gif',
  20. 8 => 'real.gif',
  21. 9 => 'av.gif',
  22. 10 => 'flash.gif',
  23. 11 => 'image.gif',
  24. 12 => 'pdf.gif',
  25. 13 => 'torrent.gif'
  26. );
  27. if(is_numeric($type)) {
  28. $typeid = $type;
  29. } else {
  30. if(preg_match("/bittorrent|^torrent\t/", $type)) {
  31. $typeid = 13;
  32. } elseif(preg_match("/pdf|^pdf\t/", $type)) {
  33. $typeid = 12;
  34. } elseif(preg_match("/image|^(jpg|gif|png|bmp)\t/", $type)) {
  35. $typeid = 11;
  36. } elseif(preg_match("/flash|^(swf|fla|flv|swi)\t/", $type)) {
  37. $typeid = 10;
  38. } elseif(preg_match("/audio|video|^(wav|mid|mp3|m3u|wma|asf|asx|vqf|mpg|mpeg|avi|wmv)\t/", $type)) {
  39. $typeid = 9;
  40. } elseif(preg_match("/real|^(ra|rm|rv)\t/", $type)) {
  41. $typeid = 8;
  42. } elseif(preg_match("/htm|^(php|js|pl|cgi|asp)\t/", $type)) {
  43. $typeid = 7;
  44. } elseif(preg_match("/text|^(txt|rtf|wri|chm)\t/", $type)) {
  45. $typeid = 6;
  46. } elseif(preg_match("/word|powerpoint|^(doc|ppt)\t/", $type)) {
  47. $typeid = 5;
  48. } elseif(preg_match("/^rar\t/", $type)) {
  49. $typeid = 4;
  50. } elseif(preg_match("/compressed|^(zip|arj|arc|cab|lzh|lha|tar|gz)\t/", $type)) {
  51. $typeid = 3;
  52. } elseif(preg_match("/octet-stream|^(exe|com|bat|dll)\t/", $type)) {
  53. $typeid = 2;
  54. } elseif($type) {
  55. $typeid = 1;
  56. } else {
  57. $typeid = 0;
  58. }
  59. }
  60. if($returnval == 'html') {
  61. return '<img src="'.STATICURL.'image/filetype/'.$attachicons[$typeid].'" border="0" class="vm" alt="" />';
  62. } elseif($returnval == 'id') {
  63. return $typeid;
  64. }
  65. }
  66. function parseattach($attachpids, $attachtags, &$postlist, $skipaids = array()) {
  67. global $_G;
  68. if(!$attachpids) {
  69. return;
  70. }
  71. $attachpids = is_array($attachpids) ? $attachpids : array($attachpids);
  72. $attachexists = FALSE;
  73. $skipattachcode = $aids = $payaids = $findattach = array();
  74. foreach(C::t('forum_attachment_n')->fetch_all_by_id('tid:'.$_G['tid'], 'pid', $attachpids) as $attach) {
  75. $attachexists = TRUE;
  76. if($skipaids && in_array($attach['aid'], $skipaids)) {
  77. $skipattachcode[$attach[pid]][] = "/\[attach\]$attach[aid]\[\/attach\]/i";
  78. continue;
  79. }
  80. $attached = 0;
  81. $extension = strtolower(fileext($attach['filename']));
  82. $attach['ext'] = $extension;
  83. $attach['imgalt'] = $attach['isimage'] ? strip_tags(str_replace('"', '\"', $attach['description'] ? $attach['description'] : $attach['filename'])) : '';
  84. $attach['attachicon'] = attachtype($extension."\t".$attach['filetype']);
  85. $attach['attachsize'] = sizecount($attach['filesize']);
  86. if($attach['isimage'] && !$_G['setting']['attachimgpost']) {
  87. $attach['isimage'] = 0;
  88. }
  89. $attach['attachimg'] = $attach['isimage'] && (!$attach['readperm'] || $_G['group']['readaccess'] >= $attach['readperm']) ? 1 : 0;
  90. if($attach['attachimg']) {
  91. $GLOBALS['aimgs'][$attach['pid']][] = $attach['aid'];
  92. }
  93. if($attach['price']) {
  94. if($_G['setting']['maxchargespan'] && TIMESTAMP - $attach['dateline'] >= $_G['setting']['maxchargespan'] * 3600) {
  95. C::t('forum_attachment_n')->update('tid:'.$_G['tid'], $attach['aid'], array('price' => 0));
  96. $attach['price'] = 0;
  97. } elseif(!$_G['forum_attachmentdown'] && $_G['uid'] != $attach['uid']) {
  98. $payaids[$attach['aid']] = $attach['pid'];
  99. }
  100. }
  101. $attach['payed'] = $_G['forum_attachmentdown'] || $_G['uid'] == $attach['uid'] ? 1 : 0;
  102. $attach['url'] = ($attach['remote'] ? $_G['setting']['ftp']['attachurl'] : $_G['setting']['attachurl']).'forum/';
  103. $attach['dbdateline'] = $attach['dateline'];
  104. $attach['dateline'] = dgmdate($attach['dateline'], 'u');
  105. $hideattachs = $_G['adminid'] != 1 && $_G['setting']['bannedmessages'] & 1 && (($postlist[$attach['pid']]['authorid'] && !$postlist[$attach['pid']]['username'])
  106. || ($postlist[$attach['pid']]['groupid'] == 4 || $postlist[$attach['pid']]['groupid'] == 5) || $postlist[$attach['pid']]['status'] == -1 || $postlist[$attach['pid']]['memberstatus'])
  107. || $_G['adminid'] != 1 && $postlist[$attach['pid']]['status'] & 1 || $postlist[$attach['pid']]['first'] && $_G['forum_threadpay'];
  108. if(!$hideattachs) {
  109. $postlist[$attach['pid']]['attachments'][$attach['aid']] = $attach;
  110. }
  111. if(!defined('IN_MOBILE_API') && !empty($attachtags[$attach['pid']]) && is_array($attachtags[$attach['pid']]) && in_array($attach['aid'], $attachtags[$attach['pid']])) {
  112. $findattach[$attach['pid']][$attach['aid']] = "/\[attach\]$attach[aid]\[\/attach\]/i";
  113. $attached = 1;
  114. }
  115. if(!$attached) {
  116. if($attach['isimage']) {
  117. if(!$hideattachs) {
  118. $postlist[$attach['pid']]['imagelist'][] = $attach['aid'];
  119. $postlist[$attach['pid']]['imagelistcount']++;
  120. }
  121. if($postlist[$attach['pid']]['first']) {
  122. $GLOBALS['firstimgs'][] = $attach['aid'];
  123. }
  124. } else {
  125. if(!$hideattachs && (!$_G['forum_skipaidlist'] || !in_array($attach['aid'], $_G['forum_skipaidlist']))) {
  126. $postlist[$attach['pid']]['attachlist'][] = $attach['aid'];
  127. }
  128. }
  129. }
  130. $aids[] = $attach['aid'];
  131. }
  132. if($aids) {
  133. $attachs = C::t('forum_attachment')->fetch_all($aids);
  134. foreach($attachs as $aid => $attach) {
  135. if($postlist[$attach['pid']]) {
  136. $postlist[$attach['pid']]['attachments'][$attach['aid']]['downloads'] = $attach['downloads'];
  137. }
  138. }
  139. }
  140. if($payaids) {
  141. foreach(C::t('common_credit_log')->fetch_all_by_uid_operation_relatedid($_G['uid'], 'BAC', array_keys($payaids)) as $creditlog) {
  142. $postlist[$payaids[$creditlog['relatedid']]]['attachments'][$creditlog['relatedid']]['payed'] = 1;
  143. }
  144. }
  145. if(!empty($skipattachcode)) {
  146. foreach($skipattachcode as $pid => $findskipattach) {
  147. foreach($findskipattach as $findskip) {
  148. $postlist[$pid]['message'] = preg_replace($findskip, '', $postlist[$pid]['message']);
  149. }
  150. }
  151. }
  152. if($attachexists) {
  153. foreach($attachtags as $pid => $aids) {
  154. if($findattach[$pid]) {
  155. foreach($findattach[$pid] as $aid => $find) {
  156. $postlist[$pid]['message'] = preg_replace($find, attachinpost($postlist[$pid]['attachments'][$aid], $postlist[$pid]), $postlist[$pid]['message'], 1);
  157. $postlist[$pid]['message'] = preg_replace($find, '', $postlist[$pid]['message']);
  158. }
  159. }
  160. }
  161. } else {
  162. loadcache('posttableids');
  163. $posttableids = $_G['cache']['posttableids'] ? $_G['cache']['posttableids'] : array('0');
  164. foreach($posttableids as $id) {
  165. C::t('forum_post')->update($id, $attachpids, array('attachment' => '0'), true);
  166. }
  167. }
  168. }
  169. function attachwidth($width) {
  170. global $_G;
  171. if($_G['setting']['imagemaxwidth'] && $width) {
  172. return 'class="zoom" onclick="zoom(this, this.src, 0, 0, '.($_G['setting']['showexif'] ? 1 : 0).')" width="'.($width > $_G['setting']['imagemaxwidth'] ? $_G['setting']['imagemaxwidth'] : $width).'"';
  173. } else {
  174. return 'thumbImg="1"';
  175. }
  176. }
  177. function packaids($attach) {
  178. global $_G;
  179. return aidencode($attach['aid'], 0, $_G['tid']);
  180. }
  181. function showattach($post, $type = 0) {
  182. $type = !$type ? 'attachlist' : 'imagelist';
  183. $return = '';
  184. if(!empty($post[$type]) && is_array($post[$type])) {
  185. foreach($post[$type] as $aid) {
  186. if(!empty($post['attachments'][$aid])) {
  187. $return .= $type($post['attachments'][$aid], $post['first']);
  188. }
  189. }
  190. }
  191. return $return;
  192. }
  193. function getattachexif($aid, $path = '') {
  194. global $_G;
  195. $return = $filename = '';
  196. if(!$path) {
  197. if($attach = C::t('forum_attachment_n')->fetch('aid:'.$aid, $aid, array(1, -1))) {
  198. if($attach['remote']) {
  199. $filename = $_G['setting']['ftp']['attachurl'].'forum/'.$attach['attachment'];
  200. } else {
  201. $filename = $_G['setting']['attachdir'].'forum/'.$attach['attachment'];
  202. }
  203. }
  204. } else {
  205. $filename = $path;
  206. }
  207. if($filename) {
  208. require_once libfile('function/exif');
  209. $exif = getexif($filename);
  210. $keys = array(
  211. exif_lang('Model'),
  212. exif_lang('ShutterSpeedValue'),
  213. exif_lang('ApertureValue'),
  214. exif_lang('FocalLength'),
  215. exif_lang('ExposureTime'),
  216. exif_lang('DateTimeOriginal'),
  217. exif_lang('ISOSpeedRatings'),
  218. );
  219. foreach($exif as $key => $value) {
  220. if(in_array($key, $keys)) {
  221. $return .= "$key : $value<br />";
  222. }
  223. }
  224. }
  225. return $return;
  226. }
  227. ?>