Location: PHPKode > projects > Enigma > Enigma2/Enigma2_Install/modules/Mod_Downloads.php
<?php
/*
*****************************************************************

 Mod_Downloads.php
 
*****************************************************************
LSP: Lunabyte Systems Portal
Open-Source Project Inspired by Zef Hemel (hide@address.com)
*****************************************************************
Software Version:                  LSP 2.0 "Enigma 2"
Software by:                         Lunabyte Systems (http://www.lunabyte.net)
Copyright 2002-2005 by:       Lunabyte Systems (http://www.lunabyte.net)
Support, News, Updates at:    http://www.lunabyte.net
*****************************************************************
This program is free software; you may redistribute it and/or modify it 
under the terms of the provided license as published by Lunabyte Systems.

This program is distributed in the hope that it is and will be useful,                  
but WITHOUT ANY WARRANTIES; without even any implied warranty of           
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                        

See the "LSP_license.txt" file for details of the LSP license.
The latest version can always be found at http://www.lunabyte.net.
*****************************************************************
*/

if (!defined('ENIGMA'))
	die('<b>Access Violation</b><br />Direct Access to this location is not allowed.');
else
	DownloadHub();

function DownloadHub()
{
	global $context, $scripturl, $txt;

	isAllowedTo('download_files');

	loadMTemplate('Downloads');
	loadMLanguage('Downloads');

	$context['raw_data'] = '';

	$context['current_cat'] = 0;
	$context['cat_level'] = 0;
	$context['cat_cache'] = array();
	$_REQUEST['catd'] = !empty($_REQUEST['catd']) ? $_REQUEST['catd'] : NULL;

	$sa = !empty($_REQUEST['sa']) ? $_REQUEST['sa'] : NULL;

	$subActions = array(
		'dlview',
		'download',
		'dlrate',
	);

	if (in_array($sa, $subActions))
		$sa();
	elseif (isset($subActions[$sa]))
		$subActions[$sa]();
	else
		downloads();
	unset($subActions);

	$context['sub_template'] = 'rawdata';
}

function downloads()
{
	global $db_prefix, $settings, $prefs, $txt, $yytitle, $s;
	global $scatd, $modSettings, $scripturl, $context;

	$context['page_title'] = &$txt['dl_104'];

	$result = db_query("
		SELECT *
		FROM {$db_prefix}download_cats
		ORDER BY name ASC", __FILE__, __LINE__);

	if (mysql_num_rows($result)>0)
	{
		while ($row = mysql_fetch_array($result))
		{
			$context['cat_cache'][$row['isSub']][$row['id']] = $row;
			if($_REQUEST['catd'] != 0 && $row['id'] == $_REQUEST['catd'])
				$context['current_cat'] = $row;
		}
		mysql_free_result ($result);
	}
	else
		$context['raw_data'] .= $txt['dl_268'] . '<br />';

	if($_REQUEST['catd'] && $_REQUEST['catd'] != 0)
	{
		$result = db_query("
			SELECT id, cat, name, filename
			FROM {$db_prefix}download_items
			ORDER BY name ASC", __FILE__, __LINE__);

		if(mysql_num_rows($result)>0)
		{
			while($row = mysql_fetch_array($result))
				$context['item_cache'][$row['cat']][$row['id']] = $row;
		}
		mysql_free_result ($result);
	}
	if (!$_REQUEST['catd'] || $_REQUEST['catd'] == 0)
		subcats(0,0);
	else
		subcats($_REQUEST['catd'],1);
}

function subcats($id, $getitems)
{
	global $context, $settings, $txt, $scripturl;

	if ($context['cat_level'] == 0 && groupPermissions($context['current_cat']['permission']) == 'true')
	{
		if ($id == 0)
			$context['raw_data'] .= '
			<img src="' . $settings['images_url'] . '/icons/folder_open.gif" style="border: 0" alt="" /> <a href="' . $scripturl . '?module=Downloads"> <span style="font-weight: bold">' . $txt['dl_85'] . '</span></a><br />
			<br />';
		else
		{
			$context['raw_data'] .= '
			<img src="' . $settings['images_url'] . '/icons/folder_open.gif" style="border: 0" alt="" />
				<a href="' . $scripturl . '?module=Downloads"> <span style="font-weight: bold">' . $txt['dl_85'] . '</span></a><br />
				<br />';
			$context['cat_level']++;
			$context['raw_data'] .= '
			<img src="' . $settings['images_url'] . '/icons/folder_open.gif" style="border: 0" alt="" />
				<a href="' . $scripturl . '?module=Downloads;catd=' . $id . '"> <span style="font-weight: bold">' . $context['current_cat']['name'] . '</span></a>
				- ' . $context['current_cat']['description'] . '<br />';
		}

		if($getitems != 0)
			$context['raw_data'] .= items($id, $context['cat_level']);
	}

	if (empty($context['cat_cache'][$id]))
	{
		$context['cat_level']--;
		return;
	}

	while (list($key1, $cat) = each($context['cat_cache'][$id]))
	{
		if (groupPermissions($cat['permission']) == 'true')
		{
			if ($context['cat_level'] == 1)
			{
				$context['raw_data'] .= '
			<br />';
				$image = '<img src="' . $settings['images_url'] . '/blank.gif" style="border: 0; width: 20px; height: 1px" alt="" /><img src="' . $settings['images_url'] . '/selected.gif" style="border: 0" alt="" />';
				$width = '45px';
			}
			else
			{
				if(!empty($notfirst))
				{
					$context['raw_data'] .= '
			<hr />';
				}
				$notfirst = 1;
				$image = '<img src="' . $settings['images_url'] . '/selected.gif" style="border: 0" alt="" />';
				$width = '25px';
			}

			$context['raw_data'] .=
			$image . '
				<a href="' . $scripturl . '?module=Downloads;catd=' . $cat['id'] . '"> <span style="font-weight: bold">' . $cat['name'] . '</span></a>
				- ' . $cat['description'] . '<br />';

			if ($getitems != 0)
				$items = items($cat['id'], $context['cat_level']);
			else
				$items = '';
			$context['raw_data'] .= $items;
			$context['cat_level']++;
			subcats ($cat['id'], $getitems);
		}
	}

	if ($context['cat_level'] == 0 && groupPermissions($context['current_cat']['permission']) == 'true')
	{
		if($getitems != 0)
			$context['raw_data'] .= items($_REQUEST['id'],0);
	}
	$context['cat_level']--;
	return;
}

function items($cat, $level)
{
	global $context, $settings, $scripturl, $txt;

	if(empty($context['item_cache'][$cat]))
		return;

	$result = '
		<br />
		<img src="' . $settings['images_url'] . '/blank.gif" style="border: 0; width: 37px; height: 1px" alt="" />Files:<br />';
	while (list($key1, $item) = each($context['item_cache'][$cat]))
	{
		if($level == 0)
			$result .= '
		<img src="' . $settings['images_url'] . '/blank.gif" style="border: 0; width: 20px; height: 1px" alt="" />';
		else
			$result .= '
		<img src="' . $settings['images_url'] . '/blank.gif" style="border: 0; width: 45px; height: 1px" alt="" />';

		$mirrors = explode("\n", $item['filename']);
		$path_parts = pathinfo($mirrors[0]);
		$extension = $path_parts['extension'];
		$extension = strtolower($extension);

		if (in_array($extension, array('zip' . 'rar' . 'ace')) === true)
			$file_img_url = 'archive.gif';
		elseif (in_array($extension, array('gif' . 'jpg' . 'jpeg' . 'bmp')) === true)
			$file_img_url = 'image.gif';
		else
			$file_img_url = 'file.gif';

		$result .= '
		<img src="' . $settings['images_url'] . '/icons/' . $file_img_url . '" alt="" /> <a href="' . $scripturl . '?module=Downloads;sa=dlview;id=' . $item['id'] . '"><b>' . $item['name'] . '</b></a><br />';
	}
	return $result;
}

function download()
{
	global $db_prefix, $txt, $mirror, $modSettings, $scripturl;

	if (!$mirror || $mirror == 0)
		$mirror = 1;

	db_query("
		UPDATE {$db_prefix}download_items
		SET downloads = downloads + 1
		WHERE id = $_REQUEST[id]", __FILE__, __LINE__);

	$request = db_query("
		SELECT filename
		FROM {$db_prefix}download_items
		WHERE id = '$_REQUEST[id]'", __FILE__, __LINE__);

	$item = mysql_fetch_array($request);
	mysql_free_result ($request);
	$mirrors = explode("\r\n",$item['filename']);

	$url = $mirrors[$mirror-1];
	if (substr($url, 0, 7) == 'http://' XOR substr($url, 0, 6) == 'ftp://')
	{
		redirectexit($url, false);
	} else {
		redirectexit($modSettings['dlsurl'] . '/' . $url, false);
	}
}

function dlview()
{
	global $txt, $db_prefix, $txt, $context, $scripturl, $modSettings;

	$context['page_title'] = &$txt['dl_97'];

	if($_REQUEST['id'])
	{
		$result = db_query("
				SELECT *
				FROM {$db_prefix}download_items
				WHERE id = $_REQUEST[id]", __FILE__, __LINE__);

		if(mysql_num_rows($result)>0)
			$item = mysql_fetch_array($result);
		else
		{
			fatal_error($txt['dl_1']);
		}

		mysql_free_result ($result);

		db_query("
			UPDATE {$db_prefix}download_items
			SET views = views + 1
			WHERE id = $_REQUEST[id]", __FILE__, __LINE__);

		$vote_result = db_query("
			SELECT SUM(vote)/COUNT(vote)
			FROM {$db_prefix}download_votes
			WHERE dl_id = $_REQUEST[id]", __FILE__, __LINE__);

		$vote_result2 = mysql_fetch_array($vote_result);
		$vote = $vote_result2[0];
		mysql_free_result ($vote_result);

		if($vote == null)
			$vote = $txt['dl_6'];

		if ($item['name'] != '' && $item['filename'] != '')
		{
			$result = db_query("
				SELECT *
				FROM {$db_prefix}download_cats
				WHERE id = $item[cat]", __FILE__, __LINE__);

			$context['current_cat'] = mysql_fetch_array($result);
			subcats($item['cat'],0);
			mysql_free_result ($result);

			$context['raw_data'] .= '<br />';

			$name = $item['name'];
			$mirrors = explode("\r\n",$item['filename']);

			$desc = (!empty($item['ddesc'])) ? $item['ddesc'] : '';

			$author = (!empty($item['author_name'])) ? $item['author_name'] : '';
			$author = (!empty($item['author_email'])) ? '<a href="mailto:' . $item['author_email'] . '">' . $author . '</a><br />' : $author;

			$downloads = $item['downloads'];
			$views = $item['views'];

			if ($item['website_title'] != '')
				$website = '<a href="' . $item['website'] . '">' . $item['website_title'] . '</a><br />';
			elseif ($item['website'] != '')
				$website = '<a href="' . $item['website'] . '">' . $item['website'] . '</a><br />';
			else
				$website = '';

			if (strtolower(substr($mirrors[0], 0, 7)) == 'http://' || strtolower(substr($mirrors[0], 0, 6)) == 'ftp://')
			{
				if($item['filesize'] == 0)
				{
					$ourhead = '';
					$url=parse_url($mirrors[0]);
					$host=$url['host'];
					$path=$url['path'];

					$fp = fsockopen($host, 80, $errno, $errstr, 20);

					if(!$fp)
						die('error');
					else
					{
						fputs ($fp,"HEAD $url HTTP/1.1\r\n");
						fputs ($fp,"HOST: Enigma\r\n");
						fputs ($fp,"Connection: close\r\n\r\n");

						while (!feof($fp)) 
							$ourhead = sprintf("%s%s", $ourhead, fgets ($fp,128));
					}
					fclose ($fp);

					$split_head = explode("Content-Length: ", $ourhead);
					$size = round(abs($split_head[0]));
					$filesize = $size;
				}
				else
					$filesize = $item['filesize'];
			}
			else
				$filesize = filesize($modSettings['dlsdir'] . '/' . $item['filename']);
		}

		$alreadyconverted = '';
		if ($filesize > 1073741824 || $filesize == 1073741824)
		{
			$number = 1073741824;
			$filesize = ($filesize / $number);
			$sizetype = 'GB';
			$alreadyconverted = 1;
		}
		if ($filesize > 1048576 || $filesize == 1048576 && $alreadyconverted != 1)
		{
			$number = 1048576;
			$filesize = ($filesize / $number);
			$sizetype = 'MB';
			$alreadyconverted = 1;
		}
		if ($filesize > 1024 || $filesize == 1024 && $alreadyconverted != 1)
		{
			$number = 1024;
			$filesize = ($filesize / $number);
			$sizetype = 'KB';
			$alreadyconverted = '1';
		}
		if ($alreadyconverted == '')
			$sizetype = 'B';

		$filesize = explode('.', $filesize);
		$filesize[0] = substr($filesize[0],0,2);

		if (!empty($filesize[0]))
			$filesizedecimal = '.' . $filesize[0];
		else
			$filesizedecimal = '';

		$context['raw_data'] .= '
<div class="tborder">
	<table width="100%" cellpadding="3" cellspacing="2" border="0">
		<tr>
			<td class="titlebg" width="100%" colspan="2"><b>' . $txt['dl_97'] . '</b></td>
		</tr>
			<td class="windowbg2" width="30%"><b>' . $txt['dl_195'] . '</b>:</td>
			<td class="windowbg2" width="70%"><b>' . $name . '</b></td>
		</tr><tr>
			<td class="windowbg2" width="30%"><b>' . $txt['dl_92'] . '</b>:</td>
			<td class="windowbg2" width="70%">' . $desc . '</td>
		</tr><tr>
			<td class="windowbg2" width="30%"><b>' . $txt['dl_91'] . '</b>:</td>
			<td class="windowbg2" width="70%">' . $filesize[0] . $filesizedecimal . ' ' . $sizetype . '</td>
		</tr><tr>
			<td class="windowbg2" width="30%"><b>' . $txt[29] . '</b>:</td>
			<td class="windowbg2" width="70%">' . $author . '</td>
		</tr><tr>
			<td class="windowbg2" width="30%"><b>' . $txt['dl_95'] . '</b>:</td>
			<td class="windowbg2" width="70%">' . $website . '</td>
		</tr><tr>
			<td class="windowbg2" width="30%"><b>' . $txt['dl_104'] . '</b>:</td>
			<td class="windowbg2" width="70%">' . $downloads . '</td>
		</tr><tr>
			<td class="windowbg2" width="30%"><b>' . $txt['dl_4'] . '</b>:</td>
			<td class="windowbg2" width="70%">' . $vote . '</td>
		</tr><tr>
			<td class="windowbg2" width="30%"><b>' . $txt[301] . '</b>:</td>
			<td class="windowbg2" width="70%">' . $views . '</td>
		</tr><tr>
			<td class="windowbg2" width="30%"><b>' . $txt['dl_2'] . '</b>:</td>
			<td class="windowbg2" width="70%">';

		for($i=1; $i <= count($mirrors); $i++)
			$context['raw_data'] .= '<a href="' . $scripturl . '?module=Downloads;sa=download;id=' . $item['id'] . ';mirror=' . $i . '" target="_blank"><i>[Mirror ' . $i . ']</i></a> ';

		$context['raw_data'] .= '</td>
		</tr><tr>
			<th class="windowbg2" width="100%" colspan="2"><form action="' . $scripturl . '" method="post">
				<select name="rate" onchange="if (this.options[this.selectedIndex].value) window.location.href=\'' . $scripturl . '\' + this.options[this.selectedIndex].value;">
					<option value="">'.$txt['dl_5'].'</option>';

		for($x=1; $x <= 10; $x++)
		{
			$context['raw_data'] .= '
					<option value="?module=Downloads;sa=dlrate;id=' . $_REQUEST['id'] . ';rate=' . $x . '">' . $x . '</option>';
		}

		$context['raw_data'] .='
				</select></th>
		</tr></form>
	</table>
</div>
<br />';
	}
	else
	{
		fatal_error($txt['dl_1']);
	}
}

function dlrate()
{
	global $ID_MEMBER, $db_prefix;

	if ($_REQUEST['id'] && $_REQUEST['rate'])
	{
		$result = db_query("
			SELECT *
			FROM {$db_prefix}download_votes
			WHERE dl_id = $_REQUEST[id] AND member_id = $ID_MEMBER", __FILE__, __LINE__);

		if (mysql_num_rows($result) == 0 && $_REQUEST['rate'] > 0 && $_REQUEST['rate'] <= 10)
		{
			db_query("
				INSERT INTO {$db_prefix}download_votes
					(dl_id, member_id, vote)
				 VALUES ($_REQUEST[id], $ID_MEMBER, $_REQUEST[rate])", __FILE__, __LINE__);
		}
		mysql_free_result ($result);
	}

	redirectexit('module=Downloads;sa=dlview;id=' . $_REQUEST['id']);
}

?>
Return current item: Enigma