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

 Mod_Links.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
LinkHub(); // load the hub first

/* This is the Hub for Links.php, its main function is to clean up index.php,
   but also preloads redundent info to cleanup functions in this file */
function LinkHub()
{
	global $context, $db_prefix, $ID_MEMBER, $modSettings, $_REQUEST, $txt, $scripturl;

	$sa = (empty($_REQUEST['sa']) && !empty($_POST['sa'])) ? $_POST['sa'] :
		empty($_REQUEST['sa']) ? 'links' : $_REQUEST['sa'];

	$subActions = array(
		'links',
		'links2',
		'addlink',
		'addlink2',
		'gotolink',
		'editlink',
		'editlink2',
		'deletelink',
		'linkIndex',
		'reviewlink',
		'linkcats',
	);

	loadMLanguage('Links');

	//get links permissions - find out who can do what.
	$context['subLinks'] = allowedTo('submit_link');
	$context['revLinks'] = allowedTo('review_link');

	$context['linktree'][] = array(
		'url'	=> $scripturl . '?module=Links',
		'name'	=> $txt['links_53'],
	);

	if ($context['subLinks'])
		$context['linktree'][] = array(
			'url'	=> $scripturl . '?module=Links;sa=addlink',
			'name'	=> $txt['links_47'],
		);

	if (($context['revLinks'] == 1) || ($context['subLinks'] == 1))
		$context['linktree'][] = array(
			'url'	=> $scripturl . '?module=Links;sa=linkIndex',
			'name'	=> $txt['links_48'],
		);


	$context['page_title'] = $txt['links_53'];

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

// this function redirects users that click a link.
function gotolink()
{
	global $db_prefix, $txt;

	$id = (int) $_REQUEST['id'];

	db_query("
		UPDATE {$db_prefix}links_items
		SET clicks=clicks+1
		WHERE linkID = '$id'
		LIMIT 1", __FILE__, __LINE__);

	$request = db_query("
		SELECT linkURL
		FROM {$db_prefix}links_items
		WHERE linkID = '$id'
		LIMIT 1", __FILE__, __LINE__);

	list($exit) = mysql_fetch_row ($request);
	mysql_free_result($request);

	if (!empty($exit))
		redirectexit($exit, false);
	else
		fatal_lang_error ('link_not_found');
}

// the main link index
function links()
{
	global $context, $db_prefix, $modSettings, $scripturl, $txt, $ID_MEMBER, $user_info;

	// if only one category was selected....
	if ((isset($_REQUEST['id'])) && ($_REQUEST['id'] != '') && ($_REQUEST['id'] != 'none'))
		$id = $_REQUEST['id'];
	elseif ((isset($_REQUEST['id'])) && ($_REQUEST['id'] == 'none'))
		$id = '0';
	else
		$id = '';			
	// ...then setup the query accordingly
	$item_cat_query = ($id >= '0') ? 'AND l.catID = \'' . $id . '\'': '';

	// setup the starting row for the query from page index
	$index = (empty($_GET['start']) || $_GET['start'] < 0) ? 0 : $_GET['start'];
	// set the max links to load per page
	$max = (!empty($modSettings['maxlinksviewlinks'])) ? $modSettings['maxlinksviewlinks'] : 20;
	// get some counts - total links in db (for page index) and number of links per category for cat title display
	$get_count = db_query("
		SELECT l.linkID, l.catID
		FROM {$db_prefix}links_items AS l
			LEFT JOIN {$db_prefix}links_cats AS c ON (c.catID = l.catID)
		WHERE (l.stage = 1 OR l.ID_SUBMITTER = $ID_MEMBER)
			$item_cat_query
		ORDER BY c.name ASC, l.clicks DESC", __FILE__, __LINE__);
	// start at zero
	$totalLinks = 0;
	$totalCats = 0;
	if (mysql_num_rows($get_count) > 0)
	{
		while ($ct = mysql_fetch_row($get_count))
		{	// add 1 to the total for this row
			$totalLinks++;
			// if this is the first row for this catID, set the count to 1
			if (empty($catID[$ct[1]])){
				$catID[$ct[1]] = 1;;
				$totalCats++;
			}	
			// otherwise, add 1 to the count
			else
				$catID[$ct[1]]++;
		}
		mysql_free_result($get_count);
	}

	if ($totalLinks > 0)
	{	// set the array
		$context['linkcat'] = array();
		$totalFrontLinks = 0;
		
		$request = db_query("
			SELECT l.*, c.name AS catName, c.description AS catDesc, mem.realName
			FROM {$db_prefix}links_items AS l
				LEFT JOIN {$db_prefix}members AS mem ON (l.ID_SUBMITTER = mem.ID_MEMBER)
				LEFT JOIN {$db_prefix}links_cats AS c ON (c.catID = l.catID)
			WHERE (l.stage = 1 OR l.ID_SUBMITTER = $ID_MEMBER)
				$item_cat_query
			ORDER BY c.name ASC, l.clicks DESC
			LIMIT $index, $max", __FILE__, __LINE__);

		while ($result = mysql_fetch_assoc($request))
		{
			// setup the upper array for the category if it's not already setup
			if (empty($context['linkcat'][$result['catID']]))
			{
				$context['linkcat'][$result['catID']] = array(
					'name'		=> empty($result['catName']) ? $txt['links_45'] . ' ' . $txt['links_20'] : $result['catName'],
					'description'	=> empty($result['catDesc']) ? $txt['links_46'] : $result['catDesc'],
					'index'		=> $result['catID'],
					'total'		=> $catID[$result['catID']], // here's the count for this cat
					'colspan'		=> 'colspan="4"'
				);
				$linkcount[$result['catid']] = $catID[$result['catID']];
				$totalFrontLinks = $totalFrontLinks + $num;
			}
			// setup an array for this link
			$context['linkcat'][$result['catID']]['links'][] = array(
				'linkID'		=> $result['linkID'],
				'name'		=> $result['name'],
				'description'	=> $result['description'],
				'submittedBy'	=> ($result['ID_SUBMITTER'] > 0) ? $result['realName'] : $result['submittedBy'],
				'submitterLink'=> ($result['ID_SUBMITTER'] > 0) ? $scripturl . '?action=profile;u=' .$result['ID_SUBMITTER'] : 'mailto:' . $result['submitterEmail'],
				'IsSubmitter'	=> ($result['ID_SUBMITTER'] == $ID_MEMBER && $context['subLinks']) ? true : false,
				'clicks'		=> $result['clicks'],
				'status'		=> (($result['ID_SUBMITTER'] == $ID_MEMBER && $context['subLinks'] && !$user_info['is_guest']) || $context['revLinks'] && $result['stage'] != '1') ? statusText($result['stage']) : '',
			);
			// do a check to see if user should see edit/delete options for the current category at all, but only set it once, so it isn't turned off again after setting
			if (empty($context['linkcat'][$result['catID']]['showOptions']))
			{
				$context['linkcat'][$result['catID']]['showOptions'] = (($result['ID_SUBMITTER'] == $ID_MEMBER && $context['subLinks']) || $user_info['is_admin']) ? true : false;
				$context['linkcat'][$result['catID']]['colspan'] = 'colspan="3"';
			}
		}
		$linkspercat = floor($max / $totalCats);
		if ($totalFrontLinks > $max) {
			foreach($linkcount AS $id => $num) 
				foreach ($context['linkcat'][$id]['links'] AS $key => $val) {
					if ($key >= $linkspercat)
						unset ($context['linkcat'][$id]['links'][$key]);
				}
		}
		mysql_free_result($request);
	}
	// only setup the page index if the count is greater than the max per page
	if ($totalLinks > $max)
		$context['page_index'] = $txt['links_49'] . ': ' . constructPageIndex($scripturl . '?module=Links' . (($id != '') ? ';id=' . $id : ''), $index, $totalLinks, $max);

	// if there's only 1 category, add it's name to the page title and show the right linktree
	if ($id != '')
	{
		$context['page_title'] .= ' - ' . $context['linkcat'][$id]['name'];
		$context['linktree'][] = array(
			'name' 	=> $context['linkcat'][$id]['name'],
		);
	}

	// if a reviewer, check if there are any links needing review
	if ($context['revLinks'])
	{
		$request = db_query("
			SELECT COUNT(*)
			FROM {$db_prefix}links_items
			WHERE reviewFlag = 1", __FILE__, __LINE__);
		list ($newItems) = mysql_fetch_row($request);
		mysql_free_result($request);
		$context['newItems'] = ($newItems > 0) ? $newItems : 0;
	}

	loadMTemplate('Links');
	$context['sub_template'] = 'links_main';

}

function linkcats()
{
	global $context, $db_prefix, $modSettings, $scripturl, $txt, $ID_MEMBER, $user_info;

	// end the breadcrumb trail
	$context['linktree'][] = array(
		'name'	=> $txt['links_50'],
	);

	$context['page_title'] .= ' - ' . $txt['links_50'];

	// setup the none/default category
	$context['linkcat'][0]['name'] = $txt['links_45'] . ' ' . $txt['links_20'];
	$context['linkcat'][0]['index'] = 0;
	$context['linkcat'][0]['description'] = $txt['links_46'];
	// get the total # of links without a category since this won't count in the next query
	$request = db_query("
		SELECT COUNT(*)
		FROM {$db_prefix}links_items
		WHERE catID = 0", __FILE__, __LINE__);
	list ($context['linkcat'][0]['total']) = mysql_fetch_row($request);
	mysql_free_result($request);

	// Get a list of all catagories
	$request = db_query("
		SELECT c.*, COUNT(l.linkID) as ct
		FROM {$db_prefix}links_cats as c
			LEFT JOIN {$db_prefix}links_items as l ON (l.catID = c.catID)
		WHERE (l.stage = 1 OR l.ID_SUBMITTER = $ID_MEMBER)
		GROUP BY c.name", __FILE__, __LINE__);

	$i=1;
	$lastRow = mysql_num_rows($request);
	if ($lastRow > 0)
	{
		// setup the array
		while ($cat = mysql_fetch_assoc($request))
		{
			$context['linkcat'][] = array(
				'name'		=> $cat['name'],
				'description'	=> $cat['description'],
				'index'		=> $cat['catID'],
				'total'		=> $cat['ct'],
				'lastcat'		=> ($i==$lastRow) ? (int) true : (int) false
			);
			$i++;
		}
		mysql_free_result($request);
	}

	// if a reviewer, check if there are any links needing review
	if ($context['revLinks'])
	{
		$request = db_query("
			SELECT COUNT(*)
			FROM {$db_prefix}links_items
			WHERE reviewFlag = 1", __FILE__, __LINE__);
		list ($newItems) = mysql_fetch_row($request);
		mysql_free_result($request);
		$context['newItems'] = ($newItems > 0) ? $newItems : 0;
	}

	loadMTemplate('Links');
	$context['sub_template'] = 'link_cats';
}

function addlink()
{
	global $context, $db_prefix, $txt, $user_info, $modSettings;

	// make sure they're supposed to be here
	isAllowedTo('submit_link');

	// end the breadcrumb trail
	$context['linktree'][] = array(
		'name'	=> $txt['links_1'],
	);

	$context['page_title'] .= ' - ' . $txt['links_1'];
	
	// set the None cat option
	$context['linkcats'][0] = array(
		'catID' => 0,
		'name' => $txt['links_9'],
	);
	// get the rest of the cats
	$request = db_query("
		SELECT catID, name
		FROM {$db_prefix}links_cats
		ORDER BY 'name'", __FILE__, __LINE__);

	while ($cats = mysql_fetch_assoc ($request))
		$context['linkcats'][] = array(
			'catID' => $cats['catID'],
			'name' => $cats['name'],
		);

	mysql_free_result($request);

	// if this user is a guest and image verification is not disabled, load the verification
	if ($user_info['is_guest'] && (empty($modSettings['disablelinkverify']) || $modSettings['disablelinkverify'] != '1'))
	{
		$context['link_image_verify'] = (int) true;

		// Image verification stuff
		include_once('ModuleCaptcha/image_class.php');

		$context['lksesc'] = md5(round(rand(0,40000)));

		$lsp_captcha = new captcha($context['lksesc'], 'modules/ModuleCaptcha/cache');
		$context['lkimg'] = $lsp_captcha->get_pic(5);
	}
	// no verification
	else
		$context['link_image_verify'] = (int) false;

	loadMTemplate('Links');
	$context['sub_template'] = 'add_link';
}

function addlink2()
{
	global $context, $db_prefix, $ID_MEMBER, $txt, $user_info, $modSettings;

	// if the user is a guest, and verification is not disabled, check post variable set on site.
	if ($user_info['is_guest'] && (empty($modSettings['disablelinkverify']) || $modSettings['disablelinkverify'] != '1') && !empty($_POST['go']) && $_POST['go'] == 'verify')
	{
		include_once('ModuleCaptcha/image_class.php');

		$lsp_captcha = new captcha($_POST['lkv'] , 'modules/ModuleCaptcha/cache');

		$checkcode = $lsp_captcha->verify( $_POST['password'] );
	}
	// the user is a member or verification is disabled, just check the normal session
	elseif ((!empty($modSettings['disablelinkverify']) && $modSettings['disablelinkverify'] == '1') || !$user_info['is_guest'])
	{
		checkSession('post', '', true);
		// if session is bad, they'll already have an error.  if it's good, set the checkcode var
		$checkcode = (int) true;
	}
	// This user MUST be a bot! Let's have some fun!
	else
	{
		header('http://www.spamcop.net/');
		exit();
	}

	// if they are a guest and entered in the right code or session passed for members
	if ($checkcode)
	{
		if(isset($_POST['linkurl'])) { $linkURL = $_POST['linkurl']; }
		if(isset($_POST['submitted'])) { $submit = $_POST['submitted']; }
		if(isset($_POST['email'])) { $email = $_POST['email']; }
	
		if(!get_magic_quotes_gpc())
		{
			$_POST['cat']	= mysql_escape_string($_POST['cat']);
			$_POST['name']	= mysql_escape_string($_POST['name']);
			$linkURL	= mysql_escape_string($linkURL);
			$_POST['description'] = mysql_escape_string($_POST['description']);
			$submit		= mysql_escape_string($submit);
			$email		= mysql_escape_string($email);
		}
	
		$time = time();
		//if they forget to put HTTP in there, be sure to add it
		if (!stristr($linkURL, '://'))
			$linkURL = 'http://' . $linkURL;
	
		// If poster is a Guest then evaluate the legality of name and email
		if ($context['user']['is_guest'])
		{
			$submit = trim($submit);
			if ($submit == '' || $submit == '_' || $submit == ' ')
				fatal_lang_error(75, false);
	
			if (strlen($submit) > 25)
				fatal_lang_error(75, false);
	
			if (empty($email))
				fatal_lang_error(76, false);
	
			if (!preg_match("/^[0-9A-Za-z@\._\-]+$/", $email))
				fatal_lang_error(76, false);
		}
		else
			$submit = '';
			$email = '';
	
		//set the reviewFlag
		if ($context['subLinks'] == 1)
		{
			$reviewFlag = 1;
			$stage = 0;
		}
		if ($context['revLinks'] == 1)
		{
			$reviewFlag = 0;
			$stage = 1;
		}
	
	
		db_query("
			INSERT INTO {$db_prefix}links_items
			(catID, name, description, linkURL, ID_SUBMITTER, dateEntered, submittedBy, submitterEmail, stage, reviewFlag)
			VALUES ('$_POST[cat]', '$_POST[name]', '$_POST[description]', '$linkURL', '$ID_MEMBER', '$time', '$submit', '$email', '$stage', '$reviewFlag')", __FILE__, __LINE__);
	
		redirectexit('module=Links;id=' . $_POST['cat']);
	}
	// invalid code entered, redirect back to sign again.
	else
	{
		redirectexit('module=Links;sa=addlink;code=1');
	}
}

function editlink()
{
	global $context, $db_prefix, $txt;


	// end the breadcrumb trail
	$context['linktree'][] = array(
		'name'	=> $txt['links_67'],
	);

	$context['page_title'] .= ' - ' . $txt['links_67'];

	if (($context['subLinks'] == 1) || ($context['revLinks'] == 1))
	{
		// set the None cat option
		$context['linkcats'][0] = array(
			'catID' => 0,
			'name' => $txt['links_9'],
		);
		// get the rest of the cats
		$request = db_query("
			SELECT catID, name
			FROM {$db_prefix}links_cats
			ORDER BY 'name'", __FILE__, __LINE__);

		while ($cats = mysql_fetch_assoc ($request))
			$context['linkcats'][] = array(
				'catID' => $cats['catID'],
				'name' => $cats['name'],
			);

		mysql_free_result($request);

		$linkID = (int) $_REQUEST['linkID'];

		//get the current information on this link
		$request = db_query("
			SELECT catID, name, linkURL, description, linkID
			FROM {$db_prefix}links_items
			WHERE linkID = '$linkID'
			LIMIT 1", __FILE__, __LINE__);
		list(
		$context['curlink']['catID'],
		$context['curlink']['name'],
		$context['curlink']['linkURL'],
		$context['curlink']['description'],
		$context['curlink']['linkID']) = mysql_fetch_row ($request);
		mysql_free_result($request);
	}
	else
		fatal_lang_error(1);

	loadMTemplate('Links');
	$context['sub_template'] = 'edit_link';
}

function editlink2()
{
	global $context, $db_prefix, $ID_MEMBER, $txt;

	$linkID = (int) $_POST['linkID'];
	$linkURL = $_POST['linkurl'];
	$time = time();

	//if they forget to put HTTP in there, be sure to add it
	if (!stristr($linkURL, '://'))
		$linkURL = 'http://' . $linkURL;

	//if a reviewer made the edit, update the status to approved
	if ($context['revLinks'] == 1)
	{
		db_query("
			UPDATE {$db_prefix}links_items
			SET catID = '$_POST[cat]', name = '$_POST[name]', description = '$_POST[description]',
			linkURL = '$linkURL', stage = 1, ID_REVIEWER = '$ID_MEMBER', dateModified = '$time',
			reviewFlag = 0
			WHERE linkID = '$linkID'", __FILE__, __LINE__);
	}

	//if a submitter made the edit, update the status to re-submitted
	else if ($context['subLinks'] == 1)
	{
		db_query("
			UPDATE {$db_prefix}links_items
			SET catID = '$_POST[cat]', name = '$_POST[name]', description = '$_POST[description]',
			linkURL = '$linkURL', stage = 4, dateModified = '$time', reviewFlag = 1
			WHERE linkID = '$linkID'", __FILE__, __LINE__);
	}
	else
		fatal_lang_error(1);

	redirectexit('module=Links;id=' . $_POST['cat']);
}

function deletelink()
{
	global $db_prefix, $context, $ID_MEMBER;

	$linkID = (int) $_REQUEST['linkID'];

	if (!$context['subLinks'] || !$context['revLinks'])
		fatal_lang_error(1);

	// one extra check befor deleting...
	$query = (!$context['revLinks']) ? "AND ID_SUBMITTER = '$ID_MEMBER'" : '';

	//find out where we came from
	$request = db_query("
		SELECT catID
		FROM {$db_prefix}links_items
		WHERE linkID = '$linkID'
		LIMIT 1", __FILE__, __LINE__);

	list ($catID) = mysql_fetch_row ($request);
	mysql_free_result ($request);

	//run the delete query
	db_query("
		DELETE FROM {$db_prefix}links_items
		WHERE linkID = '$linkID'
			$query
		LIMIT 1", __FILE__, __LINE__);

	redirectexit('module=Links;id=' . $catID);
}

function linkIndex()
{
	global $context, $db_prefix, $ID_MEMBER, $txt, $scripturl;

	$context['page_title'] .= ' - ' . $txt['links_44'];

	// end the breadcrumb trail
	$context['linktree'][] = array(
		'name'	=> $txt['links_44'],
	);

	// set the category filter, if a category was already selected
	if (!empty($_POST['catfilter']) && $_POST['catfilter'] != 'none')
		$catfilter = $_POST['catfilter'];
	elseif (!empty($_POST['catfilter']) && $_POST['catfilter'] == 'none')  // set category to 0 if the "none" cat was selected
		$catfilter = '0'; // *** the quotation marks are important on this one
	else
		$catfilter = '';

	// setup the category query based on the current selection
	$cat_query = ($catfilter >= '0') ? 'AND l.catID = ' . $catfilter : '';
	// set the stage filter
	$stagef = (isset($_POST['stagef'])) ? $_POST['stagef'] : '';
	// set the stage query based on selection
	$stage_query = ($stagef == 9 || $stagef == '') ? '' : 'AND l.stage = ' . $stagef;

	//set up some conditionals for the mySQL query
	if ($context['revLinks'] == 1)
		$permit_query = '';
	else if ($context['subLinks'] == 1)
		$permit_query = ' AND ID_SUBMITTER = ' . $ID_MEMBER;
	else
		fatal_lang_error(1);

	$context['linkstage'] = array(
		'alid'	=> array(
			'option'	=> 'All',
			'stage'		=> '9',
			'selected'	=> ($stagef == 9 || $stagef == '') ? 'selected="selected" ' : ''),
		'peid'	=> array(
			'option'	=> 'Pending',
			'stage'		=> '0',
			'selected'	=> ($stagef == 0 && $stagef != '') ? 'selected="selected" ' : ''),
		'apid'	=> array(
			'option'	=> 'Approved',
			'stage'		=> '1',
			'selected'	=> ($stagef == 1) ? 'selected="selected" ' : ''),
		'rjid'	=> array(
			'option'	=> 'Rejected',
			'stage'		=> '2',
			'selected'	=> ($stagef == 2) ? 'selected="selected" ' : ''),
		'caid'	=> array(
			'option'	=> 'Canceled',
			'stage'		=> '3',
			'selected'	=> ($stagef == 3) ? 'selected="selected" ' : ''),
		'rsid'	=> array(
			'option'	=> 'ReSubmitted',
			'stage'		=> '4',
			'selected'	=> ($stagef == 4) ? 'selected="selected" ' : ''),
	);

     //setup the category filter
	// setup All category
	$context['filtercat'][] = array(
		'name'		=> $txt['links_27'],
		'value'		=> '',
		'selected'	=> (($catfilter == '') ? 'selected="selected"' : ''),
	);
	// setup None/Default category
	$context['filtercat'][] = array(
		'name'		=> $txt['links_9'] . '/' . $txt['links_45'],
		'value'		=> '0',
		'selected'	=> (($catfilter == '0') ? 'selected="selected"' : ''),
	);

	//pull all the link categories for the filter list
	$request = db_query("
		SELECT *
		FROM {$db_prefix}links_cats
		ORDER BY name", __FILE__, __LINE__);

	if (!empty($request))
	{
			while ($cats = mysql_fetch_assoc($request))
			$context['filtercat'][] = array(
				'name'		=> $cats['name'],
				'value'		=> $cats['catID'],
				'selected'	=> (($catfilter == $cats['catID']) ? 'selected="selected"' : ''),
			);
		mysql_free_result ($request);
	}

	//  now get the items to be displayed
	$request = db_query("
		SELECT l.*, IFNULL(l.dateModified, l.dateEntered) AS mostrecent, c.name AS cname, mem.realName AS submitter,
		mem2.realName AS reviewer, mem.ID_MEMBER AS submitterID, mem2.ID_MEMBER AS reviewerID
		FROM ({$db_prefix}links_items AS l)
			LEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER=l.ID_SUBMITTER)
			LEFT JOIN {$db_prefix}members AS mem2 ON (mem2.ID_MEMBER=l.ID_REVIEWER)
			LEFT JOIN {$db_prefix}links_cats AS c ON (l.catID = c.catID)
		 WHERE l.catID IS NOT NULL
			$cat_query
			$stage_query
			$permit_query
		ORDER BY mostrecent DESC", __FILE__, __LINE__);

	if (mysql_num_rows($request) > 0)
	{
		while ($row = mysql_fetch_assoc($request))
		{
			$context['linkindex'][] = array(
				'name'		=> $row['name'],
				'linkID'		=> $row['linkID'],
				'cname'		=> ($row['catID'] == '0') ? $txt['links_9'] . '/' . $txt['links_45'] : $row['cname'],
	
				'submitter'	=> ($row['submitterID'] > 0) ? $row['submitter'] : $row['submittedBy'],
				'submitterLink'=> ($row['submitterID'] > 0) ? $scripturl . '?action=profile;u=' . $row['submitterID'] : 'mailto:' . $row['submitterEmail'],
	
				'reviewer'	=> ($row['reviewerID'] == '') ? '' : $row['reviewer'],
				'reviewerLink'	=> ($row['reviewerID'] == '') ? '' : $scripturl . '?action=profile;u=' .$row['reviewerID'],
	
				'status'	=> statusText($row['stage']),
				'subdate'	=> timeformat($row['dateEntered']),
				'moddate'	=> (!$row['dateModified']) ? '' : timeformat($row['dateModified']),
			);
		}
	}
	mysql_free_result ($request);

	loadMTemplate('Links');
	$context['sub_template'] = 'link_index';
}

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

	// Continue the breadcrumb trail
	$context['linktree'][] = array(
		'name'	=> $txt['links_64'],
	);

	$context['page_title'] .= ' - ' . $txt['links_64'];

	$id = (int) $_REQUEST['id'];

	$request = db_query("
			SELECT l.*, mem.realName, mem.ID_MEMBER
			FROM ({$db_prefix}links_items AS l)
				LEFT JOIN {$db_prefix}members AS mem ON (l.ID_SUBMITTER = mem.ID_MEMBER)
			WHERE linkID = '$id'", __FILE__, __LINE__);

	if (mysql_num_rows($request) > 0)
	{
		while ($link = mysql_fetch_array($request))
			$context['linkreview'][] = array(
				'linkID'	=> $link['linkID'],
				'name'		=> $link['name'],
				'description'	=> $link['description'],
				'submittedBy'	=> ($link['ID_SUBMITTER'] > 0) ? $link['realName'] : $link['submittedBy'],
				'submitterLink'=> ($link['ID_SUBMITTER'] > 0) ? $scripturl . '?action=profile;u=' .$link['ID_SUBMITTER'] : 'mailto:' . $link['submitterEmail'],
				'clicks'	=> $link['clicks'],
			);
		mysql_free_result ($request);
	}

	// load link Console data...
	linkConsole();

	loadMTemplate('Links');
	$context['sub_template'] = 'link_review';
}

// linkConsole was moved here to keep unused functions from being loaded till they are needed.
function linkConsole()
{
	global $context, $db_prefix, $txt;

	$id = (int) $_REQUEST['id'];

	//get the link
	$request = db_query("
		SELECT l.*, m.realName AS submitter, mem.realName AS reviewer
		FROM ({$db_prefix}links_items AS l)
			LEFT JOIN {$db_prefix}members AS m ON (m.ID_MEMBER = l.ID_SUBMITTER)
			LEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = l.ID_REVIEWER)
		WHERE linkID = '$id'
		LIMIT 1", __FILE__, __LINE__);

	$context['submissions'] = mysql_fetch_assoc ($request);
	mysql_free_result ($request);

	$context['load_link_Console'] = true;
	if (!$context['submissions'])
	{
		$context['load_link_Console'] = false;
		return false;
	}

	if ($context['submissions']['reviewer'] == '')
	{
		$context['submissions']['reviewer'] = $txt['links_68'];
	}

	if ($context['submissions']['comment'] == '')
	{
		$context['submissions']['comment'] = $txt['links_69'];
	}

	$request = db_query("
		SELECT *
		FROM {$db_prefix}links_cats", __FILE__, __LINE__);

	while ($cats = mysql_fetch_assoc($request))
		$context['listcats'][] = array(
			'selected'	=> ($context['submissions']['catID'] == $cats['catID']) ? 'selected="selected"' : '',
			'catID'		=> $cats['catID'],
			'name'		=> $cats['name'],
		);

	mysql_free_result ($request);
}

// linkRules was moved here to keep unused functions from being loaded till they are needed.
// Renamed from linksRules to links2. same function.
function links2()
{
	global $db_prefix, $ID_MEMBER;

	// SMF doesn't break up the query stirng anymore so wee need to get these from $_REQUEST
	$linkID		= (int) $_REQUEST['linkID'];
	$category	= (int) $_REQUEST['category'];
	$lsource	= (int) $_REQUEST['lsource'];
	$comment	= htmlspecialchars($_REQUEST['comment'], ENT_QUOTES);

	/************************
	* asource values        *
	* 4 = approve link      *
	* 5 = reject link       *
	* 9 = no submission     *
	************************/

	/*******************
	* workflow values  *
	* 0 = pending      *
	* 1 = approved     *
	* 2 = rejected     *
	* 3 = cancelled    *
	* 4 = resubmitted  *
	*******************/

	// setup some variables so the queries are easier to process
	$time = time();

	// figure out who has permissions to do what
	if (!allowedTo('site_review_link'))
		redirectexit('module=Links');

	// if they got this far they must be able to review a link! ::) so lets update it...

	// Reviewer approved the link
	$stage = ($lsource == 4) ? '1' : '';
	// Reviewer rejected the link
	$stage = ($lsource == 5) ? '2' : $stage;

	// Reviewer messed something up...
	if ($stage == '')
		fatal_error ('Invalid Workflow value.');

	db_query("
		UPDATE {$db_prefix}links_items
		SET stage = '$stage', catID = '$category', dateModified = '$time',
		ID_REVIEWER = '$ID_MEMBER', comment = '$comment', reviewFlag = '0'
		WHERE linkID = '$linkID'
		LIMIT 1", __FILE__, __LINE__);

	logAction('Link Review', array('link' => $linkID, 'status' => ($stage == 1) ? $txt['links_70'] : $txt['links_71']));
	redirectexit('module=Links;sa=linkIndex');
}
?>
Return current item: Enigma