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