PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/upload/attach_mod/displaying.php

http://torrentpier2.googlecode.com/
PHP | 454 lines | 326 code | 70 blank | 58 comment | 104 complexity | eb0fa63e9c4d1cbd92c4eefdafda15d4 MD5 | raw file
  1. <?php
  2. if (!defined('IN_FORUM')) die("Hacking attempt");
  3. $allowed_extensions = array();
  4. $display_categories = array();
  5. $download_modes = array();
  6. $upload_icons = array();
  7. $attachments = array();
  8. /**
  9. * Create needed arrays for Extension Assignments
  10. */
  11. function init_complete_extensions_data()
  12. {
  13. global $allowed_extensions, $display_categories, $download_modes, $upload_icons;
  14. if (!$extension_informations = get_extension_informations())
  15. {
  16. $extension_informations = $GLOBALS['datastore']->update('attach_extensions'); //get_extension_informations()
  17. $extension_informations = get_extension_informations();
  18. }
  19. $allowed_extensions = array();
  20. for ($i = 0, $size = sizeof($extension_informations); $i < $size; $i++)
  21. {
  22. $extension = strtolower(trim($extension_informations[$i]['extension']));
  23. $allowed_extensions[] = $extension;
  24. $display_categories[$extension] = intval($extension_informations[$i]['cat_id']);
  25. $download_modes[$extension] = intval($extension_informations[$i]['download_mode']);
  26. $upload_icons[$extension] = trim($extension_informations[$i]['upload_icon']);
  27. }
  28. }
  29. /**
  30. * Writing Data into plain Template Vars
  31. */
  32. function init_display_template($template_var, $replacement, $filename = 'viewtopic_attach.tpl')
  33. {
  34. global $template;
  35. // This function is adapted from the old template class
  36. // I wish i had the functions from the 3.x one. :D (This class rocks, can't await to use it in Mods)
  37. // Handle Attachment Informations
  38. if (!isset($template->uncompiled_code[$template_var]) && empty($template->uncompiled_code[$template_var]))
  39. {
  40. // If we don't have a file assigned to this handle, die.
  41. if (!isset($template->files[$template_var]))
  42. {
  43. die("Template->loadfile(): No file specified for handle $template_var");
  44. }
  45. $filename_2 = $template->files[$template_var];
  46. $str = implode('', @file($filename_2));
  47. if (empty($str))
  48. {
  49. die("Template->loadfile(): File $filename_2 for handle $template_var is empty");
  50. }
  51. $template->uncompiled_code[$template_var] = $str;
  52. }
  53. $complete_filename = $filename;
  54. if (substr($complete_filename, 0, 1) != '/')
  55. {
  56. $complete_filename = $template->root . '/' . $complete_filename;
  57. }
  58. if (!file_exists($complete_filename))
  59. {
  60. die("Template->make_filename(): Error - file $complete_filename does not exist");
  61. }
  62. $content = implode('', file($complete_filename));
  63. if (empty($content))
  64. {
  65. die('Template->loadfile(): File ' . $complete_filename . ' is empty');
  66. }
  67. // replace $replacement with uncompiled code in $filename
  68. $template->uncompiled_code[$template_var] = str_replace($replacement, $content, $template->uncompiled_code[$template_var]);
  69. }
  70. /**
  71. * Display Attachments in Posts
  72. */
  73. function display_post_attachments($post_id, $switch_attachment)
  74. {
  75. global $attach_config, $is_auth;
  76. if (intval($switch_attachment) == 0 || intval($attach_config['disable_mod']))
  77. {
  78. return;
  79. }
  80. if ($is_auth['auth_download'] && $is_auth['auth_view'])
  81. {
  82. display_attachments($post_id);
  83. }
  84. else
  85. {
  86. // Display Notice (attachment there but not having permissions to view it)
  87. // Not included because this would mean template and language file changes (at this stage this is not a wise step. ;))
  88. }
  89. }
  90. /**
  91. * Initializes some templating variables for displaying Attachments in Posts
  92. */
  93. function init_display_post_attachments($switch_attachment)
  94. {
  95. global $attach_config, $is_auth, $template, $lang, $postrow, $total_posts, $attachments, $forum_row, $t_data;
  96. if (empty($t_data) && !empty($forum_row))
  97. {
  98. $switch_attachment = $forum_row['topic_attachment'];
  99. }
  100. if (intval($switch_attachment) == 0 || intval($attach_config['disable_mod']) || (!($is_auth['auth_download'] && $is_auth['auth_view'])))
  101. {
  102. init_display_template('body', '{postrow.ATTACHMENTS}', 'viewtopic_attach_guest.tpl');
  103. return;
  104. }
  105. $post_id_array = array();
  106. for ($i = 0; $i < $total_posts; $i++)
  107. {
  108. if ($postrow[$i]['post_attachment'] == 1)
  109. {
  110. $post_id_array[] = (int) $postrow[$i]['post_id'];
  111. }
  112. }
  113. if (sizeof($post_id_array) == 0)
  114. {
  115. return;
  116. }
  117. $rows = get_attachments_from_post($post_id_array);
  118. $num_rows = sizeof($rows);
  119. if ($num_rows == 0)
  120. {
  121. return;
  122. }
  123. @reset($attachments);
  124. for ($i = 0; $i < $num_rows; $i++)
  125. {
  126. $attachments['_' . $rows[$i]['post_id']][] = $rows[$i];
  127. //bt
  128. if ($rows[$i]['tracker_status'])
  129. {
  130. if (defined('TORRENT_POST'))
  131. {
  132. message_die(GENERAL_ERROR, 'Multiple registered torrents in one topic<br /><br />first torrent found in post_id = '. TORRENT_POST .'<br />current post_id = '. $rows[$i]['post_id'] .'<br /><br />attachments info:<br /><pre style="text-align: left;">'. print_r($rows, TRUE) .'</pre>');
  133. }
  134. define('TORRENT_POST', $rows[$i]['post_id']);
  135. }
  136. //bt end
  137. }
  138. init_display_template('body', '{postrow.ATTACHMENTS}');
  139. init_complete_extensions_data();
  140. $template->assign_vars(array(
  141. 'L_POSTED_ATTACHMENTS' => $lang['POSTED_ATTACHMENTS'],
  142. 'L_KILOBYTE' => $lang['KB'])
  143. );
  144. }
  145. /**
  146. * END ATTACHMENT DISPLAY IN POSTS
  147. */
  148. /**
  149. * Assign Variables and Definitions based on the fetched Attachments - internal
  150. * used by all displaying functions, the Data was collected before, it's only dependend on the template used. :)
  151. * before this function is usable, init_display_attachments have to be called for specific pages (pm, posting, review etc...)
  152. */
  153. function display_attachments($post_id)
  154. {
  155. global $template, $upload_dir, $userdata, $allowed_extensions, $display_categories, $download_modes, $lang, $attachments, $upload_icons, $attach_config;
  156. $num_attachments = @sizeof($attachments['_' . $post_id]);
  157. if ($num_attachments == 0)
  158. {
  159. return;
  160. }
  161. $template->assign_block_vars('postrow.attach', array());
  162. for ($i = 0; $i < $num_attachments; $i++)
  163. {
  164. // Some basic things...
  165. $filename = $upload_dir . '/' . basename($attachments['_' . $post_id][$i]['physical_filename']);
  166. $thumbnail_filename = $upload_dir . '/' . THUMB_DIR . '/t_' . basename($attachments['_' . $post_id][$i]['physical_filename']);
  167. $upload_image = '';
  168. if ($attach_config['upload_img'] && empty($upload_icons[$attachments['_' . $post_id][$i]['extension']]))
  169. {
  170. $upload_image = '<img src="' . $attach_config['upload_img'] . '" alt="" border="0" />';
  171. }
  172. else if (trim($upload_icons[$attachments['_' . $post_id][$i]['extension']]) != '')
  173. {
  174. $upload_image = '<img src="' . $upload_icons[$attachments['_' . $post_id][$i]['extension']] . '" alt="" border="0" />';
  175. }
  176. $filesize = humn_size($attachments['_' . $post_id][$i]['filesize']);
  177. $display_name = htmlspecialchars($attachments['_' . $post_id][$i]['real_filename']);
  178. $comment = htmlspecialchars($attachments['_' . $post_id][$i]['comment']);
  179. $comment = str_replace("\n", '<br />', $comment);
  180. $denied = false;
  181. // Admin is allowed to view forbidden Attachments, but the error-message is displayed too to inform the Admin
  182. if (!in_array($attachments['_' . $post_id][$i]['extension'], $allowed_extensions))
  183. {
  184. $denied = true;
  185. $template->assign_block_vars('postrow.attach.denyrow', array(
  186. 'L_DENIED' => sprintf($lang['EXTENSION_DISABLED_AFTER_POSTING'], $attachments['_' . $post_id][$i]['extension']))
  187. );
  188. }
  189. if (!$denied || IS_ADMIN)
  190. {
  191. // define category
  192. $image = FALSE;
  193. $stream = FALSE;
  194. $swf = FALSE;
  195. $thumbnail = FALSE;
  196. $link = FALSE;
  197. if (@intval($display_categories[$attachments['_' . $post_id][$i]['extension']]) == STREAM_CAT)
  198. {
  199. $stream = TRUE;
  200. }
  201. else if (@intval($display_categories[$attachments['_' . $post_id][$i]['extension']]) == SWF_CAT)
  202. {
  203. $swf = TRUE;
  204. }
  205. else if (@intval($display_categories[$attachments['_' . $post_id][$i]['extension']]) == IMAGE_CAT && intval($attach_config['img_display_inlined']))
  206. {
  207. if (intval($attach_config['img_link_width']) != 0 || intval($attach_config['img_link_height']) != 0)
  208. {
  209. list($width, $height) = image_getdimension($filename);
  210. if ($width == 0 && $height == 0)
  211. {
  212. $image = TRUE;
  213. }
  214. else
  215. {
  216. if ($width <= intval($attach_config['img_link_width']) && $height <= intval($attach_config['img_link_height']))
  217. {
  218. $image = TRUE;
  219. }
  220. }
  221. }
  222. else
  223. {
  224. $image = TRUE;
  225. }
  226. }
  227. if (@intval($display_categories[$attachments['_' . $post_id][$i]['extension']]) == IMAGE_CAT && $attachments['_' . $post_id][$i]['thumbnail'] == 1)
  228. {
  229. $thumbnail = TRUE;
  230. $image = FALSE;
  231. }
  232. if (!$image && !$stream && !$swf && !$thumbnail)
  233. {
  234. $link = TRUE;
  235. }
  236. if ($image)
  237. {
  238. // Images
  239. // NOTE: If you want to use the download.php everytime an image is displayed inlined, replace the
  240. // Section between BEGIN and END with (Without the // of course):
  241. // $img_source = BB_ROOT . 'download.php?id=' . $attachments['_' . $post_id][$i]['attach_id'];
  242. // $download_link = TRUE;
  243. //
  244. //
  245. if (intval($attach_config['allow_ftp_upload']) && trim($attach_config['download_path']) == '')
  246. {
  247. $img_source = BB_ROOT . 'download.php?id=' . $attachments['_' . $post_id][$i]['attach_id'];
  248. $download_link = TRUE;
  249. }
  250. else
  251. {
  252. // Check if we can reach the file or if it is stored outside of the webroot
  253. if ($attach_config['upload_dir'][0] == '/' || ( $attach_config['upload_dir'][0] != '/' && $attach_config['upload_dir'][1] == ':'))
  254. {
  255. $img_source = BB_ROOT . 'download.php?id=' . $attachments['_' . $post_id][$i]['attach_id'];
  256. $download_link = TRUE;
  257. }
  258. else
  259. {
  260. // BEGIN
  261. $img_source = $filename;
  262. $download_link = FALSE;
  263. // END
  264. }
  265. }
  266. $template->assign_block_vars('postrow.attach.cat_images', array(
  267. 'DOWNLOAD_NAME' => $display_name,
  268. 'S_UPLOAD_IMAGE' => $upload_image,
  269. 'IMG_SRC' => $img_source,
  270. 'FILESIZE' => $filesize,
  271. 'COMMENT' => $comment,
  272. ));
  273. // Directly Viewed Image ... update the download count
  274. if (!$download_link)
  275. {
  276. $sql = 'UPDATE ' . BB_ATTACHMENTS_DESC . '
  277. SET download_count = download_count + 1
  278. WHERE attach_id = ' . (int) $attachments['_' . $post_id][$i]['attach_id'];
  279. if ( !(DB()->sql_query($sql)) )
  280. {
  281. message_die(GENERAL_ERROR, 'Couldn\'t update attachment download count.', '', __LINE__, __FILE__, $sql);
  282. }
  283. }
  284. }
  285. if ($thumbnail)
  286. {
  287. // Images, but display Thumbnail
  288. // NOTE: If you want to use the download.php everytime an thumnmail is displayed inlined, replace the
  289. // Section between BEGIN and END with (Without the // of course):
  290. // $thumb_source = BB_ROOT . 'download.php?id=' . $attachments['_' . $post_id][$i]['attach_id'] . '&thumb=1';
  291. //
  292. if (intval($attach_config['allow_ftp_upload']) && trim($attach_config['download_path']) == '')
  293. {
  294. $thumb_source = BB_ROOT . 'download.php?id=' . $attachments['_' . $post_id][$i]['attach_id'] . '&thumb=1';
  295. }
  296. else
  297. {
  298. // Check if we can reach the file or if it is stored outside of the webroot
  299. if ($attach_config['upload_dir'][0] == '/' || ( $attach_config['upload_dir'][0] != '/' && $attach_config['upload_dir'][1] == ':'))
  300. {
  301. $thumb_source = BB_ROOT . 'download.php?id=' . $attachments['_' . $post_id][$i]['attach_id'] . '&thumb=1';
  302. }
  303. else
  304. {
  305. // BEGIN
  306. $thumb_source = $thumbnail_filename;
  307. // END
  308. }
  309. }
  310. $template->assign_block_vars('postrow.attach.cat_thumb_images', array(
  311. 'DOWNLOAD_NAME' => $display_name,
  312. 'S_UPLOAD_IMAGE' => $upload_image,
  313. 'IMG_SRC' => BB_ROOT . 'download.php?id=' . $attachments['_' . $post_id][$i]['attach_id'],
  314. 'IMG_THUMB_SRC' => $thumb_source,
  315. 'FILESIZE' => $filesize,
  316. 'COMMENT' => $comment,
  317. ));
  318. }
  319. if ($stream)
  320. {
  321. // Streams
  322. $template->assign_block_vars('postrow.attach.cat_stream', array(
  323. 'U_DOWNLOAD_LINK' => $filename,
  324. 'S_UPLOAD_IMAGE' => $upload_image,
  325. 'DOWNLOAD_NAME' => $display_name,
  326. 'FILESIZE' => $filesize,
  327. 'COMMENT' => $comment,
  328. 'DOWNLOAD_COUNT' => sprintf($lang['DOWNLOAD_NUMBER'], $attachments['_' . $post_id][$i]['download_count']))
  329. );
  330. // Viewed/Heared File ... update the download count (download.php is not called here)
  331. $sql = 'UPDATE ' . BB_ATTACHMENTS_DESC . '
  332. SET download_count = download_count + 1
  333. WHERE attach_id = ' . (int) $attachments['_' . $post_id][$i]['attach_id'];
  334. if ( !(DB()->sql_query($sql)) )
  335. {
  336. message_die(GENERAL_ERROR, 'Couldn\'t update attachment download count', '', __LINE__, __FILE__, $sql);
  337. }
  338. }
  339. if ($swf)
  340. {
  341. // Macromedia Flash Files
  342. list($width, $height) = swf_getdimension($filename);
  343. $template->assign_block_vars('postrow.attach.cat_swf', array(
  344. 'U_DOWNLOAD_LINK' => $filename,
  345. 'S_UPLOAD_IMAGE' => $upload_image,
  346. 'DOWNLOAD_NAME' => $display_name,
  347. 'FILESIZE' => $filesize,
  348. 'COMMENT' => $comment,
  349. 'DOWNLOAD_COUNT' => sprintf($lang['DOWNLOAD_NUMBER'], $attachments['_' . $post_id][$i]['download_count']),
  350. 'WIDTH' => $width,
  351. 'HEIGHT' => $height)
  352. );
  353. // Viewed/Heared File ... update the download count (download.php is not called here)
  354. $sql = 'UPDATE ' . BB_ATTACHMENTS_DESC . '
  355. SET download_count = download_count + 1
  356. WHERE attach_id = ' . (int) $attachments['_' . $post_id][$i]['attach_id'];
  357. if ( !(DB()->sql_query($sql)) )
  358. {
  359. message_die(GENERAL_ERROR, 'Couldn\'t update attachment download count', '', __LINE__, __FILE__, $sql);
  360. }
  361. }
  362. //bt
  363. if ($link && ($attachments['_'. $post_id][$i]['extension'] === TORRENT_EXT))
  364. {
  365. include(BB_ROOT .'attach_mod/displaying_torrent.php');
  366. }
  367. else if ($link)
  368. //bt end
  369. {
  370. $target_blank = ( (@intval($display_categories[$attachments['_' . $post_id][$i]['extension']]) == IMAGE_CAT) ) ? 'target="_blank"' : '';
  371. // display attachment
  372. $template->assign_block_vars('postrow.attach.attachrow', array(
  373. 'U_DOWNLOAD_LINK' => BB_ROOT . 'download.php?id=' . $attachments['_' . $post_id][$i]['attach_id'],
  374. 'S_UPLOAD_IMAGE' => $upload_image,
  375. 'DOWNLOAD_NAME' => $display_name,
  376. 'FILESIZE' => $filesize,
  377. 'COMMENT' => $comment,
  378. 'TARGET_BLANK' => $target_blank,
  379. 'DOWNLOAD_COUNT' => sprintf($lang['DOWNLOAD_NUMBER'], $attachments['_' . $post_id][$i]['download_count']),
  380. ));
  381. }
  382. }
  383. }
  384. }