Location: PHPKode > projects > FavLinks Favorites Manager > favlinks/incs/include.php
<?php

	$dbuser = "lalit";
	$dbpass = "patel";
	$dbname = "personal";
/*
	$auser = "lalit";
	$apass = "favlinks";

	if (!isset($_SERVER['PHP_AUTH_USER'])) {
		auth_user();
	} else if (!isset($_SERVER['PHP_AUTH_USER'])) {
		auth_user();
	} else if ($_SERVER['PHP_AUTH_USER'] != $auser || $_SERVER['PHP_AUTH_PW'] != $apass) {
		auth_user();
	} else if (isset($_GET['action']) && $_GET['action'] == "logout") {
		auth_user();
	}


	function auth_user() {
		$realm = mt_rand( 1, 1000000000 );
		header('WWW-Authenticate: Basic realm="FavLinks Favorites Manager (http://favlinks.sf.net) [rid='.$realm.']"');
		header('HTTP/1.0 401 Unauthorized');
		echo "<font color=crimson size=4 face=verdana>Unauthorized access forbidden!</font><BR><BR><font size=1 face=verdana>FavLinks Favorites (Powered by: <a href='http://www.synapsys.co.in' target='_blank'>synapsys.co.in</a>)<BR><BR><a href='index.php'>Click here</a> to login.</font>";
		exit;
	}
*/
//---------------------------------------------------------------------------------------------
    mysql_connect("localhost", $dbuser, $dbpass) or die("Cannot connect to MySQL for username comp.");
    mysql_select_db($dbname) or die("Cannot select db for username comp.");
    define("MIN_ROWS_PER_PAGE", 10);

	if (isset($_POST['rate'])) {
		rate_link();
	}
//---------------------------------------------------------------------------------------------
    function draw_box($head, $contents) {

		$HTML  = "<table align=center class=table cellpadding=0 cellspacing=1 style='width:600px'><tr><td class=light style='padding:5pt'>";
		$HTML .= "<span class=bold>$head</span><br><br>";
		$HTML .= $contents;
		$HTML .= "</td></tr></table><br><br>";

		return ($HTML);
    }
//---------------------------------------------------------------------------------------------
    function add_head($title, $head) {
		$HTML  = '<html><head>'."\n";
		$HTML .= '<title>'.$title.'</title>'."\n";
		$HTML .= '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">'."\n";
		$HTML .= '<link rel=stylesheet type=text/css href="incs/style.css">'."\n";
		$HTML .= '</head>'."\n";
		$HTML .= '<body>'."\n";
		$HTML .= '<div align=center> <div align=center class=maindiv>'."\n";
		$HTML .= '<br><br><span class=large>'.$head.'</span><br><br><br><br>'."\n";

		return ($HTML);
    }
//---------------------------------------------------------------------------------------------
    function add_foot() {
		$HTML  = "\n".'<br><br><br>'."\n";
		$HTML .= '<hr width="95%" size="1" noshade>'."\n";
		$HTML .= 'FavLinks &nbsp;&#8250;&nbsp; <a href="index.php">Add</a> &nbsp;|&nbsp; <a href="browse.php">Browse</a> &nbsp;|&nbsp; <a href="search.php">Search</a>'."\n";
		$HTML .= '<br><br>FavLinks by Lalit Patel (<a href="http://favlinks.sf.net" target=_blank>http://favlinks.sf.net</a>)<!--Powered by: <a href="http://www.syanpsys.co.in">Synapsys</a>-->'."\n";
		$HTML .= '</div></div>'."\n";
		$HTML .= '</body></html>';

		return($HTML);
    }
//---------------------------------------------------------------------------------------------
    function isvalidlink($linkid) {
    	$linkid = (int) $linkid;
    	$l_query = "SELECT * FROM `favlinks` WHERE `id` = $linkid;";
    	$l_result = mysql_query($l_query);
    	return (mysql_num_rows($l_result) == 1);
    }
//---------------------------------------------------------------------------------------------
    function slashdot($me) {
    	$me = (ini_get('magic_quotes_gpc') ? $me : mysql_escape_string($me));
    	return($me);
    }
//---------------------------------------------------------------------------------------------
    function slashtod($me) {
    	$me = (ini_get('magic_quotes_gpc') ? stripslashes($me) : $me);
    	return($me);
    }
//---------------------------------------------------------------------------------------------
    function snip($me,$len=70) {
    	if (strlen($me) > $len)
    		return (substr($me, 0, $len)."...");
    	else {
	    	return($me);
	    }
    }
//---------------------------------------------------------------------------------------------
    function rate_link() {
		if (isset($_POST['id']) && ($linkid = (int) $_POST['id']) > 1000  && isvalidlink($linkid) && isset($_POST['score']) && ($score = (int) $_POST['score'] % 6 ) >= 0 && isset($_POST['src'])) {
			$source   =	base64_decode(substr($_POST['src'], 0, strpos($_POST['src'], '*')));
			$source  .= str_replace('*', '#', strstr($_POST['src'], '*'));
			$r_query  = "UPDATE `favlinks` SET `rating` = '$score' WHERE `id` = '$linkid' LIMIT 1;";
			$r_result = mysql_query($r_query);
			header("Location: $source");
		}
    }
//---------------------------------------------------------------------------------------------
    function searchbox($q='', $w='all', $c='any', $r=-1, $d='high', $s='title', $e=MIN_ROWS_PER_PAGE) {
		$HTML  = '<form action="search.php" method="GET" name="searchform">';
		$HTML .= '<input type="hidden" name="search" value="yes">';
		$HTML .= '<table width="600" border="0" align="center" cellpadding="4" cellspacing="1" class=table>';
		$HTML .= '<tr><td class="t_head">Search</td></tr>';
		$HTML .= '<tr><td align="center" class=light>Search for: <input name="query" type="text" value="'.htmlspecialchars($q).'" size="50"> in ';
		$HTML .= '<select name="where">'
		.'<option value="all" '.($w == 'all' ? 'selected' : '').'>All</option>'
		.'<option value="title"'.($w == 'title' ? 'selected' : '').'>Title</option>'
		.'<option value="url"'.($w == 'url' ? 'selected' : '').'>URL</option>'
		.'<option value="desc"'.($w == 'desc' ? 'selected' : '').'>Description</option>'
		.'</select></td></tr>';
		$HTML .= '<tr><td align="center" class=light>In Category: <select name="category" id="category">';
		$HTML .= '<option value="any">Any</option>';

		$c_query = "SELECT DISTINCT `category` FROM `favlinks` ORDER BY `category` ASC;";
		$c_result = mysql_query($c_query);
		while ($c_values = mysql_fetch_array($c_result)) {
			$HTML .= "\n<option ".($c == $c_values['category'] ? 'selected' : '').">".snip($c_values['category'],50)."</option>";
		}

		$HTML .= '</select> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Rating:<select name="rating">'
		.'<option value="-1" '.($r == -1 ? "selected" : "").'>Any</option>'
		.'<option value="0" '.($r == 0 ? "selected" : "").'>Unrated</option>'
		.'<option value="1" '.($r == 1 ? "selected" : "").'>1 star</option>'
		.'<option value="2" '.($r == 2 ? "selected" : "").'>2 stars</option>'
		.'<option value="3" '.($r == 3 ? "selected" : "").'>3 stars</option>'
		.'<option value="4" '.($r == 4 ? "selected" : "").'>4 stars</option>'
		.'<option value="5" '.($r == 5 ? "selected" : "").'>5 stars</option>'
		.'</select></td></tr>';
		$HTML .= '<tr><td align="center" class=dark><input name="submit" type="submit" value="Search FavLinks"></td></tr>';
		$HTML .= '</table>';
		$HTML .= '<input type="hidden" name="detail" value="'.$d.'">';
		$HTML .= '<input type="hidden" name="sortby" value="'.$s.'">';
		$HTML .= '<input type="hidden" name="rows" value="'.$e.'">';
		$HTML .= '<input type="hidden" name="page" value="1">';
		$HTML .= '</form>';

		echo $HTML;
    }
//---------------------------------------------------------------------------------------------
	function resultsbox($cur_det, $cur_sort, $cur_rows, $max_rows, $cur_page) {
		$source = urldecode($_SERVER['PHP_SELF'].($_SERVER['QUERY_STRING'] != '' ? "?".slashtod($_SERVER['QUERY_STRING']) : '?'));
		// For some HTML chars in the query string
		$cat    = isset($_GET['category']) ? slashtod($_GET['category']) : '';
		$newcat = isset($_GET['category']) ? htmlspecialchars(slashtod($_GET['category'])) : '';
		$source = str_replace('category='.$cat, 'category='.$newcat, $source);

		$cat    = isset($_GET['query']) ? slashtod($_GET['query']) : '';
		$newcat = isset($_GET['query']) ? htmlspecialchars(slashtod($_GET['query'])) : '';
		$source = str_replace('query='.$cat, 'query='.$newcat, $source);

//		echo $cat."<br>".$newcat."<br>".$source;

/*		echo "<pre><div align=left>
		Detail $cur_det
		Sort   $cur_sort
		Rows   $cur_rows / $max_rows
		Page   $cur_page</div>
		</pre>"; */

		$HTML  = "<table width=95% cellspacing=0 cellpadding=4 border=0 class=box>";
		$HTML .= "<tr><td class=bold width=10%>Details:</td>";

		if (substr_count($source, 'detail=') == 0) {
			$source = $source.'&detail='.$cur_det;
		}
		if ($cur_det == 'high') {
			$new_src = str_replace('detail='.$cur_det, 'detail=low', $source);
			$HTML .= "<td width=30%><b>High</b> &nbsp; &nbsp;<a href=\"$new_src\">Low</a></td>";
		} else {
			$new_src = str_replace('detail='.$cur_det, 'detail=high', $source);
			$HTML .= "<td width=30%><a href=\"$new_src\">High</a> &nbsp; &nbsp;<b>Low</b></td>";
		}

		$HTML .= "<td class=bold  align=right>Results per page: &nbsp; &nbsp;</td><td width=40%>";
		if (substr_count($source, 'rows=') == 0) {
			$source = $source.'&rows='.$cur_rows;
		}
		$nos = array(10, 15, 20, 25, 50, 100);
		foreach($nos as $value) {
			if ($value == $cur_rows) {
				$HTML .= "<b>{$value}</b> &nbsp; ";
			} else {
				$new_src = str_replace("rows=$cur_rows", "rows=$value", $source);
				$new_src = str_replace("page=$cur_page", "page=1", $new_src);
				$HTML .= "<a href=\"$new_src\">{$value}</a> &nbsp; ";
			}
		}
		$HTML .="</td></tr>";
		$HTML .= "<tr><td class=bold>Sort by: </td><td>";
		//<td>Title &nbsp;|&nbsp; Rating &nbsp;|&nbsp; <b>ID</b> &nbsp;|&nbsp; Date</td>";
		if (substr_count($source, 'sortby=') == 0) {
			$source = $source.'&sortby='.$cur_sort;
		}
		$nos = array('Title' => 'title', 'Rating' => 'rating', 'ID' => 'id', 'Date' => 'date');
		foreach($nos as $key => $value) {
			if ($value == $cur_sort) {
				$HTML .= "<b>{$key}</b>&nbsp; &nbsp; ";
			} else {
				$new_src = str_replace("sortby=$cur_sort", "sortby=$value", $source);
				$new_src = str_replace("page=$cur_page", "page=1", $new_src);
				$HTML .= "<a href=\"$new_src\">{$key}</a>&nbsp; &nbsp; ";
			}
		}
		$HTML .="</td><td class=bold width=20% align=right>Goto Page: &nbsp; &nbsp;</td><td>";
		if (substr_count($source, 'page=') == 0) {
			$source = $source.'&page='.$cur_page;
		}
		$r  = (int) ($max_rows / $cur_rows);
		$r += ($max_rows % $cur_rows == 0) ? 0 : 1;
		for($j = 1; $j<=$r; $j++) {
			if ($j == $cur_page) {
				$HTML .= "<b>{$j}</b> &nbsp; ";
			} else {
				$new_src = str_replace("page=$cur_page", "page=$j", $source);
				$HTML .= "<a href=\"$new_src\">{$j}</a> &nbsp; ";
			}
		}
		$HTML .= "</td></tr></table>";


		echo $HTML;
	}
//---------------------------------------------------------------------------------------------
    function list_results($result, $heading = 'none', $rowcount='&nbsp;', $detail='high', $browse=false) {
		$source   = base64_encode($_SERVER['PHP_SELF'].($_SERVER['QUERY_STRING'] != '' ? "?".slashtod($_SERVER['QUERY_STRING']) : ''));

    	$HTML  = '<table width="95%" border="0" align="center" cellpadding="4" cellspacing="1" class=table>';
		$HTML .= ($heading != 'none' ? '<tr><td class="t_head" width=70%>'.$heading.'</td><td class="small" align=right width=30%><font color=white>'.$rowcount.'</font></td></tr>' : '');

		if (mysql_num_rows($result) == 0) {
			$HTML .= "<tr><td colspan=2 class=light>0 (zero) links returned.</td></tr>";
		} else {
			$color = 0;
			while ($values = mysql_fetch_array($result, MYSQL_ASSOC) ) {
				$linkid = $values['id'];
				if ($detail == 'high') {
					$HTML .= "\n<tr><td colspan=2 class=".($color%2 == 0 ? 'light' : 'dark' ).">";
					$HTML .= "\n<a name='$linkid'>";
					$HTML .= "\n<p class=desc><b>".$values['title']."</b><br>";
					$HTML .= nl2br($values['desc'])."<br>";
					$HTML .= "<a href=\"".$values['url']."\" target='_blank'>".$values['url']."</a></p>";
					$HTML .= "\n<p align='right' class='small' style='line-height:2'>";
					(!$browse) ? $HTML .= "Category: <a href=\"browse.php?do=browse&category=".$values['category']."&detail=high\">".$values['category']."</a><br>" : '';
					$HTML .= "[ ID: ".$values['id']." &nbsp;|&nbsp; ";
					$HTML .= "Date added: ".date("d-M-Y", strtotime($values['date']))." &nbsp;|&nbsp; ";
					$HTML .= "Rating: ";
					$r = 1;
					for ($i=0; $i<$values['rating']; $i++) {
						$HTML .= "<a href='javascript:rate(".$values['id'].",$r)'><img src='imgs/star.on.gif' width=15 height=15 hspace=2 align='absmiddle' border=0></a>";
						$r++;
					}
					for ($i=0; $i<(5 - $values['rating']); $i++) {
						$HTML .= "<a href='javascript:rate(".$values['id'].",$r)'><img src='imgs/star.off.gif' width=15 height=15 hspace=2 align='absmiddle' border=0></a>";
						$r++;
					}
					$HTML .= "&nbsp;|&nbsp; <a href=\"edit.php?id=".$values['id']."&action=edit&src=".$source."\">Edit</a> &nbsp;|&nbsp; <a href=\"edit.php?id=".$values['id']."&action=delete&src=".$source."\">Delete</a> ]</p>";
					$HTML .= "\n</td></tr>";
				} else {
					$HTML .= "\n<tr><td colspan=2 class=".($color%2 == 0 ? 'light' : 'dark' ).">";
					$HTML .= "\n<a name='$linkid'>";
					$HTML .= "\n<b>".snip($values['title'],100)."</b><br>";
					//$HTML .= nl2br($values['desc'])."<br>";
					$HTML .= "<a href=\"".$values['url']."\" target='_blank'>".snip($values['url'],100)."</a><br>";
					$HTML .= "\n<div class='small' align=right style='margin-top=10'>[ ";
					(!$browse) ? $HTML .= "Category: <a href=\"browse.php?do=browse&category=".$values['category']."&detail=high\">".snip($values['category'], 60)."</a> &nbsp;|&nbsp;" : '';
					$HTML .= " ID: ".$linkid;
					$HTML .= " &nbsp;|&nbsp; Rating: ";
					$r = 1;
					for ($i=0; $i<$values['rating']; $i++) {
						$HTML .= "<a href='javascript:rate(".$values['id'].",$r)'><img src='imgs/star.on.s.gif'  width=10 height=10 hspace=2 align='absmiddle' border=0></a>";
						$r++;
					}
					for ($i=0; $i<(5 - $values['rating']); $i++) {
						$HTML .= "<a href='javascript:rate(".$values['id'].",$r)'><img src='imgs/star.off.s.gif' width=10 height=10 hspace=2 align='absmiddle' border=0></a>";
						$r++;
					}
					$HTML .= " &nbsp;|&nbsp; <a href=\"edit.php?id=".$values['id']."&action=edit&src=".$source."\">Edit</a> &nbsp;|&nbsp; <a href=\"edit.php?id=".$values['id']."&action=delete&src=".$source."\">Delete</a>";
					$HTML .= " ]</div>\n</td></tr>";
				}
				$color ++;
			}
		}
		$HTML .= '</table>';

		$HTML .= <<<EOS
<span id=empty></span>
<script language="JavaScript">
function rate(id, score) {
	var formtxt = "";
	formtxt  += "<form id=rateform name=rateform action='{$_SERVER['PHP_SELF']}' method=POST>";
	formtxt  += "<input type='hidden' name='rate' value='yes'>";
	formtxt  += "<input type='hidden' name='id' value='" + id + "'>";
	formtxt  += "<input type='hidden' name='src' value='{$source}*" + id + "'>";
	formtxt  += "<input type='hidden' name='score' value='" + score + "'>";
	formtxt  += "</form>";
	document.getElementById('empty').innerHTML = formtxt;
	document.getElementById('rateform').submit();
}
</script>
EOS;
		echo $HTML;
    }
//---------------------------------------------------------------------------------------------


?>
Return current item: FavLinks Favorites Manager