Location: PHPKode > scripts > Yet Another Link Directory > yald2/inc/functions.php
<?php
/******************************************************************************
* This file is part of Yet Another Link Directory.                            *
*                                                                             *
* Yet Another Link Directory is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or           *
* (at your option) any later version.                                         *
*                                                                             *
* Yet Another Link Directory is distributed in the hope that it will be       *
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of      *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
* GNU General Public License for more details.                                *
*                                                                             *
* You should have received a copy of the GNU General Public License           *
* along with Yet Another Link Directory; if not, write to the Free Software   *
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
******************************************************************************/


function linkCategory($id){
	global $settings;

	$query = 'SELECT category FROM '.$settings['links_table'].' WHERE id="'.mysql_safe($id).'"';
	$result = mysql_query($query);
	$row = mysql_fetch_row($result);
	return $row[0];
}

function logEvent($type,$userid,$value){
	global $settings;
	$query = 'INSERT INTO `'.$settings['log_table'].'` (`type`,`date`,`userid`,`value`) VALUES ("'.$type.'","'.time().'","'.$userid.'","'.mysql_safe($value).'")';
	mysql_query($query);
}

function categoryMenu($root,$selected='1',$incl_root=false,$excludeid='') {
	global $settings;
	// retrieve the left and right value of the $root node
	$result = mysql_query('SELECT lft, rgt FROM '.$settings['categories_table'].
	' WHERE id="'.$root.'";');
	$row = mysql_fetch_array($result);

	// start with an empty $right stack
	$right = array();

	// now, retrieve all descendants of the $root node
	$result = mysql_query('SELECT id, name, lft, rgt, parent FROM '.$settings['categories_table'].
	' WHERE lft BETWEEN '.$row['lft'].' AND '.
	$row['rgt'].' ORDER BY lft ASC;');

	if(!empty($excludeid)){
		$excludearray = subCategories($excludeid);
	} else {
		$excludearray = array();
	}


	// display each row
	$output = '<select name="category">';
	for ($i=0;$row = mysql_fetch_array($result);$i++) {
		// only check stack if there is one
		if (count($right)>0) {
			// check if we should remove a node from the stack
			while ($right[count($right)-1]<$row['rgt']) {
				array_pop($right);
			}
		}

		if($selected == $row['id']){
			$selectedtext = ' selected="selected"';
		} else {
			$selectedtext = '';
		}

		if(array_search($row['id'],$excludearray) === false){

			// display indented node title
			if(!(!$incl_root && $row['id'] == '1')){
				if(!$incl_root){
					$count = count($right)-1;
				} else {
					$count = count($right);
				}
				$output .= '<option value="'.$row['id'].'"'.$selectedtext.'>'.str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;',$count).$row['name']."</option>\n";
			}

		}

		$right[] = $row['rgt'];
	}
	$output .= '</select>';
	return $output;
}

function categoryName($id){
	global $settings;
	$query = 'SELECT name FROM '.$settings['categories_table'].' WHERE id="'.mysql_safe($id).'"';
	$result = mysql_query($query);
	$row = mysql_fetch_row($result);
	return $row[0];
}

function urlExists($url){
	if (@fopen($url,"r")) {
		return true;
	} else {
		return false;
	}
}

function mysql_safe($var) {
	if (get_magic_quotes_gpc()) {
		$var = stripslashes($var);
	}
	$var = mysql_real_escape_string($var);
	return $var;
}

function gpcStripSlashes($var){
	if(get_magic_quotes_gpc()){
		return stripslashes($var);
	}
	return $var;
}

function categoryDelete($id){
	global $settings;
	logEvent('del_cat','0',categoryName($id));
	$query = 'DELETE FROM '.$settings['categories_table'].' WHERE id="'.mysql_safe($id).'"';
	mysql_query($query);
	$query = 'DELETE FROM '.$settings['links_table'].' WHERE category="'.mysql_safe($id).'"';
	mysql_query($query);
}

function recursiveCategoryDelete($id){
	$subcats = subCategories($id);
	//categoryDelete($id);
	foreach($subcats as $key=>$subid){
		categoryDelete($subid);
	}
	rebuildTree('1',1);
}

function categorySubs($catid){
	global $settings;
	$query = 'SELECT * FROM '.$settings['categories_table'].' WHERE parent="'.mysql_safe($catid).'"';
	$result = mysql_query($query);
	if(mysql_num_rows($result)>0){
		return true;
	} else {
		return false;
	}
}

function categoryExists($id,$name=''){
	global $settings;
	$query = 'SELECT * FROM '.$settings['categories_table'].' WHERE id="'.mysql_safe($id).'"';
	$result = mysql_query($query);
	if(mysql_num_rows($result)==0){
		return false;
	} else {
		if(!empty($name)){
			$row = mysql_fetch_array($result);
			if($name == cleanUrl($row['name'])){
				return true;
			}
		} else {
			return true;
		}
		return false;
	}
}

function linkUrlExists($url,$category=null){
	global $settings;
	$query = 'SELECT * FROM `'.$settings['links_table'].'` WHERE url="'.mysql_safe($url).'"';
	if(!empty($category)){
		$query .= ' and category="'.mysql_safe($category).'"';
	}
	$result = mysql_query($query);
	if(mysql_num_rows($result)==0){
		return false;
	} else {
		return true;
	}
}

function validateEmail($email) {
	// First, we check that there's one @ symbol, and that the lengths are right
	if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
		// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
		return false;
	}
	// Split it into sections to make life easier
	$email_array = explode("@", $email);
	$local_array = explode(".", $email_array[0]);
	for ($i = 0; $i < sizeof($local_array); $i++) {
		if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
			return false;
		}
	}
	if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
		$domain_array = explode(".", $email_array[1]);
		if (sizeof($domain_array) < 2) {
			return false; // Not enough parts to domain
		}
		for ($i = 0; $i < sizeof($domain_array); $i++) {
			if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
				return false;
			}
		}
	}
	return true;
}

function linkUrl($id){
	global $settings;
	$query = 'SELECT url FROM '.$settings['links_table'].' WHERE id="'.mysql_safe($id).'"';
	$result = mysql_query($query);
	$row = mysql_fetch_row($result);
	return $row[0];
}

function categoryPath($catid){
	global $settings;

	$path = array();


	$query = 'SELECT lft,rgt FROM `'.$settings['categories_table'].'` WHERE id="'.mysql_safe($catid).'"';
	$result = mysql_query($query);
	$row = mysql_fetch_array($result);
	$lft = $row['lft'];
	$rgt = $row['rgt'];

	$query = 'SELECT name,id,path FROM `'.$settings['categories_table'].'` WHERE lft < '.$lft.' AND rgt > '.$rgt.' ORDER BY lft ASC;';
	$result = mysql_query($query);
	while($row = mysql_fetch_array($result)){
		if($row['id'] != '1'){
			//$path .= $settings['nav_separator'].'<a href="'.$_SERVER['PHP_SELF'].'?viewcat='.$row['id'].'">'.htmlentities($row['name']).'</a>';
			$path[] = array('name'=>htmlentities($row['name']),'url'=>rewriteUrl($row['id'],$row['name'],$row['path']));
		}
	}
	return $path;
}

function categoryCleanedPath($catid){
	global $settings;

	$path = null;

	$query = 'SELECT lft,rgt,name FROM `'.$settings['categories_table'].'` WHERE id="'.mysql_safe($catid).'"';
	$result = mysql_query($query);
	$row = mysql_fetch_array($result);
	$lft = $row['lft'];
	$rgt = $row['rgt'];

	$query = 'SELECT name, id FROM `'.$settings['categories_table'].'` WHERE lft < '.$lft.' AND rgt > '.$rgt.' ORDER BY lft ASC;';
	$result = mysql_query($query);
	for($i=0; $subrow = mysql_fetch_array($result); $i++){
		if($subrow['id'] != '1'){
			$path .= cleanUrl($subrow['name']) . '/';
		}
	}

	$path .= cleanUrl($row['name']);

	return $path;
}

function categoryParent($catid){
	global $nav, $settings;
	$query = 'SELECT parent FROM '.$settings['categories_table'].' WHERE id="'.mysql_safe($catid).'"';
	$result = mysql_query($query);
	$row = mysql_fetch_row($result);
	return $row[0];
}

function linkRemove($id){
	global $nav, $settings;
	$query = 'DELETE FROM '.$settings['links_table'].' WHERE id="'.mysql_safe($id).'"';
	mysql_query($query);
}

function listCategories($catid){
	global $settings, $template;

	$query = 'SELECT * FROM '.$settings['categories_table'].' WHERE parent="'.mysql_safe($catid).'" ORDER BY `name` ASC';
	$result = mysql_query($query);

	while($row = mysql_fetch_array($result)){
		$id[] = $row['id'];
		$name[] = $row['name'];
		$path[] = $row['path'];
		$disp_subs[] = $row['disp_subs'];
	}

	//$cats = '<table width="100%" border="0"><tr><td width="50%" valign="top"><ul>';

	if(mysql_num_rows($result) % 2){
		$divnum = mysql_num_rows($result)+1;
	} else {
		$divnum = mysql_num_rows($result);
	}

	$template->assign('category_division',$divnum/2);

	$categoryarray = array();

	for($i=0; $i<mysql_num_rows($result); $i++){

		$main_link_url = rewriteUrl($id[$i],$name[$i],$path[$i]);

		if($disp_subs[$i]=='true' && numSubCats($id[$i]) > 0){
			$query = 'SELECT * FROM '.$settings['categories_table'].' WHERE parent="'.$id[$i].'" ORDER BY `name` ASC LIMIT '.$settings['show_num_subs'];
			$result2 = mysql_query($query);

			for($subi=0;$subrow = mysql_fetch_array($result2); $subi++){

				$link_url = rewriteUrl($subrow['id'],$subrow['name'],$subrow['path']);
				$subsarray[] = array('id'=>$subrow['id'],'name'=>$subrow['name'],'url'=>$link_url);

			}

			if(numSubCats($id[$i]) >= $settings['show_num_subs']){
				$subscontinued = true;
			} else {
				$subscontinued = false;
			}

		} else {
			$subscontinued = false;
			$subsarray = null;
		}

		$categoryarray[] = array('id'=>$id[$i],'name'=>$name[$i],'url'=>$main_link_url,'subs'=>$subsarray,'continued'=>$subscontinued,'links'=>categoryLinks($id[$i]));
		$subsarray = null;
	}

	return $categoryarray;
}

function stripslashes_array($value){
	$value = is_array($value) ?
	array_map('stripslashes_array', $value) :
	stripslashes($value);

	return $value;
}

function numSubCats($parent){
	global $settings;
	$query = 'SELECT * FROM '.$settings['categories_table'].' WHERE parent="'.$parent.'"';
	$result = mysql_query($query);
	return mysql_num_rows($result);
}

function cleanUrl($text)
{
	$text=strtolower($text);
	$code_entities_match = array(' ','--','&quot;','!','@','#','$','%','^','&','*','(',')','_','+','{','}','|',':','"','<','>','?','[',']','\\',';',"'",',','.','/','*','+','~','`','=');
	$code_entities_replace = array('-','-','','','','','','','','','','','','','','','','','','','','','','','','');
	$text = str_replace($code_entities_match, $code_entities_replace, $text);
	return $text;
}

function search($search){
	global $settings;

	$search = gpcStripSlashes($search);

	$search=trim($search);

	$q = 'MATCH(`url`,`name`,`description`) AGAINST("'.mysql_safe($search).'")';

	$query = 'SELECT * FROM '.$settings['links_table'].' WHERE `approved`="true" and '.$q;
	$result = mysql_query($query);

	while($row = mysql_fetch_array($result)){

		if($settings['jump_links']){
			$url = $_SERVER['PHP_SELF'].'?jump='.$row['id'];
		} else {
			$url = $row['url'];
		}

		$row['url'] = htmlentities($row['url']);
		$row['name'] = htmlentities($row['name']);
		$row['description'] = htmlentities($row['description']);

		$catquery = 'SELECT path,name FROM `'.$settings['categories_table'].'` WHERE id = "'.mysql_safe($row['category']).'"';
		$catresult = mysql_query($catquery);
		$catrow = mysql_fetch_array($catresult);

		$caturl = rewriteUrl($row['category'],$catrow['name'],$catrow['path']);

		/*$links .= "<li><a href=\"{$url}\" target=\"_blank\"><b>{$row['name']}</b></a> - {$row['description']}<br />
		<small><strong>URL: </strong><font color=\"999999\">{$row['url']}</font><br />Found in: <a href=\"{$_SERVER['PHP_SELF']}?viewcat={$row['category']}\">{$categoryname}</a></small>
		</li>";*/

		$links[] = array('id'=>$row['id'],'name'=>$row['name'],'description'=>$row['description'],'url'=>$row['url'],'linkurl'=>$url,'category'=>$catrow['name'],'categoryurl'=>$caturl);

	}


	if(!empty($links)){
		return $links;
	} else {
		return null;
	}
}

function listUnapprovedLinks(){
	global $settings;

	$query = 'SELECT * FROM '.$settings['links_table'].' WHERE approved="false" ORDER BY date ASC';
	$result = mysql_query($query);

	$links = '<ul>';

	$category = null;

	while($row = mysql_fetch_array($result)){
		$id = $row['id'];
		$name = htmlentities($row['name']);
		$url = htmlentities($row['url']);
		$urlenc = urlencode($row['url']);
		$urlenc2 = urlencode($urlenc);
		$description = htmlentities($row['description']);
		$date = $row['date'];

		$categorypath = categoryPath($row['category']);
		for($i=0; $i<count($categorypath);$i++){
			$category .= '/'.$categorypath[$i]['name'];
		}

		$category .= '/'.categoryName($row['category']);

		$links .= "<li><a href=\"{$url}\" target=\"_blank\"><b>{$name}</b></a> - {$description}<br />
<small>Category: {$category}<br /><strong>URL: </strong><font color=\"999999\">{$url}</font></small><br />
<a href=\"{$_SERVER['PHP_SELF']}?approve={$id}&url={$urlenc}\">[approve]</a> <a href=\"javascript:reject_link('{$id}','{$urlenc2}')\">[reject]</a> <a href=\"edit.php?id={$id}&ref=approve\">[edit]</a>
</li>";
	}

	$links .= '</ul>';
	return $links;
}

function numUnapprovedLinks(){
	global $settings;
	$query = 'SELECT id FROM '.$settings['links_table'].' WHERE approved="false"';
	$result = mysql_query($query);
	return mysql_num_rows($result);
}

function listLinks($catid=''){
	global $settings, $is_administrator, $paginator;

	if($settings['list_type']=='1'){
		$query = 'SELECT * FROM '.$settings['links_table'].' WHERE category="'.mysql_safe($catid).'" and approved="true" ORDER BY clicks DESC';
	} elseif($settings['list_type']=='2'){
		$query = 'SELECT * FROM '.$settings['links_table'].' WHERE category="'.mysql_safe($catid).'" and approved="true" ORDER BY name ASC';
	} else {
		$query = 'SELECT * FROM '.$settings['links_table'].' WHERE category="'.mysql_safe($catid).'" and approved="true" ORDER BY date DESC';
	}

	if($settings['use_pagination']==true){
		$paginator->sql = $query;
		$result = $paginator->get_page_result();
	} else {
		$result = mysql_query($query);
	}

	if(mysql_num_rows($result)>0){
		while($row = mysql_fetch_array($result)){
			if($settings['jump_links']){
				if($settings['url_type'] == '1'){
					$url = $_SERVER['PHP_SELF'].'?jump='.$row['id'];
				} else {
					$url = $settings['mod_rewrite_url'].basename($settings['index_file']).'?jump='.$row['id'];
				}
			} else {
				$url = $row['url'];
			}

			$row['url'] = $row['url'];
			$row['name'] = $row['name'];
			$row['description'] = $row['description'];

			$links[] = array('id'=>$row['id'],'linkurl'=>$url,'url'=>$row['url'],'name'=>$row['name'],'description'=>$row['description']);
		}
	} else {
		$links = null;
	}

	return $links;
}


function latestLinks(){
	global $settings, $is_administrator;

	$query = 'SELECT * FROM '.$settings['links_table'].' WHERE approved="true" ORDER BY date DESC LIMIT '.$settings['num_latest_links'];
	$result = mysql_query($query);

	if(mysql_num_rows($result)>0){
		while($row = mysql_fetch_array($result)){
			if($settings['jump_links']){
				$url = $_SERVER['PHP_SELF'].'?jump='.$row['id'];
			} else {
				$url = $row['url'];
			}

			$row['url'] = $row['url'];
			$row['name'] = $row['name'];
			$row['description'] = $row['description'];

			$links[] = array('id'=>$row['id'],'linkurl'=>$url,'url'=>$row['url'],'name'=>$row['name'],'description'=>$row['description']);
		}
	} else {
		$links = null;
	}

	return $links;
}


// check for subcategories
function subCat($parent){
	global $settings;
	$query = 'SELECT id FROM '.$settings['categories_table'].' WHERE parent="'.mysql_safe($parent).'"';
	$result = mysql_query($query);
	if(mysql_num_rows($result)==0){
		return false;
	} else {
		return true;
	}
}

function hasLinks($catid){
	if(categoryLinks($catid)==0){
		return false;
	} else {
		return true;
	}
}

function countLinks($id){
	global $settings;
	$query = 'SELECT count(id) as cnt FROM '.$settings['links_table'].' WHERE category="'.$id.'" and approved="true"';
	$result = mysql_query($query);
	return mysql_result($result,0,'cnt');
}

function recursiveCountLinks($id){
	$subcats = subCategories($id);
	$count = 0;
	foreach($subcats as $key=>$subid){
		$count += countLinks($subid);
	}
	return $count;
}

// # of links in a category
function categoryLinks($id){
	global $settings;

	if($settings['count_refresh_time']>0){
		$query = 'SELECT link_count,count_last_upd FROM '.$settings['categories_table'].' WHERE id="'.$id.'"';
		$result = mysql_query($query) or die('Query failed. '.mysql_error());
		$row = mysql_fetch_array($result);

		if((time()-$row['count_last_upd'])/60 > $settings['count_refresh_time']){
			// update the time for that field
			if($settings['recursive_counts']){
				$count = recursiveCountLinks($id);
			} else {
				$count = countLinks($id);
			}
			$query = 'UPDATE '.$settings['categories_table'].' SET link_count="'.$count.'", count_last_upd="'.time().'" WHERE id="'.$id.'"';
			mysql_query($query);
			return $count;
		} else {
			return $row['link_count'];
		}
	} else {
		if($settings['recursive_counts']){
			return recursiveCountLinks($id);
		} else {
			return countLinks($id);
		}
	}
}

function printError($error){
	global $settings;
	include($settings['template_header']);
	print $error;
	include($settings['template_footer']);
	exit;
}

function rebuildTree($parent, $left) {
	global $settings;
	// the right value of this node is the left value + 1
	$right = $left+1;

	// get all children of this node
	$result = mysql_query('SELECT id FROM '.$settings['categories_table'].
	' WHERE parent="'.$parent.'" ORDER BY `name`;');
	while ($row = mysql_fetch_array($result)) {
		// recursive execution of this function for each
		// child of this node
		// $right is the current right value, which is
		// incremented by the rebuild_tree function
		$right = rebuildTree($row['id'], $right);
	}

	// we've got the left value, and now that we've processed
	// the children of this node we also know the right value
	mysql_query('UPDATE '.$settings['categories_table'].' SET lft='.$left.', rgt='.
	$right.' WHERE id="'.$parent.'";');

	// return the right value of this node + 1
	return $right+1;
}

function subCategories($root){
	global $settings;
	// retrieve the left and right value of the $root node
	$result = mysql_query('SELECT lft, rgt FROM '.$settings['categories_table'].
	' WHERE id="'.$root.'"');
	$row = mysql_fetch_array($result);

	// now, retrieve all descendants of the $root node
	$query = 'SELECT id, name, lft, rgt FROM '.$settings['categories_table'].
	' WHERE lft BETWEEN '.$row['lft'].' AND '.
	$row['rgt'].' ORDER BY lft ASC';
	$result = mysql_query($query);

	// display each row
	$output = '';
	for ($i=0;$row = mysql_fetch_array($result);$i++) {
		$output[] = $row['id'];
	}
	return $output;
}

function displayTree($root,$call='categories') {
	global $settings;
	// retrieve the left and right value of the $root node
	$result = mysql_query('SELECT lft, rgt FROM '.$settings['categories_table'].
	' WHERE id="'.$root.'"');
	$row = mysql_fetch_array($result);

	// start with an empty $right stack
	$right = array();

	// now, retrieve all descendants of the $root node
	$query = 'SELECT id, name, lft, rgt FROM '.$settings['categories_table'].
	' WHERE lft BETWEEN '.$row['lft'].' AND '.
	$row['rgt'].' ORDER BY lft ASC';
	$result = mysql_query($query);

	// display each row
	$output = 'root<br />';
	for ($i=0;$row = mysql_fetch_array($result);$i++) {
		// only check stack if there is one
		if (count($right)>0) {
			// check if we should remove a node from the stack
			while ($right[count($right)-1]<$row['rgt']) {
				array_pop($right);
			}
		}

		// display indented node title
		if($row['id'] != '1'){
			if($call == 'categories'){
				$catname = urlencode(urlencode($row['name']));
				$output .= str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',count($right)).'|__ '.$row['name']." <a href=\"javascript:delete_category('{$row['id']}','{$catname}')\" title=\"Delete this category and its contents.\">[X]</a> <a href=\"edit.php?catid={$row['id']}&ref=cats\" title=\"Edit this category.\">[Edit]</a> <a href=\"{$_SERVER['PHP_SELF']}?newsubparent={$row['id']}\" title=\"Create a new subcategory.\">[+]</a> <a href=\"{$_SERVER['PHP_SELF']}?move={$row['id']}\" title=\"Move links and subcategories to another category.\">[Move]</a><br />\n";
			} elseif($call == 'newlink'){
				$output .= str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',count($right)).'|__ '.$row['name']." <a href=\"{$_SERVER['PHP_SELF']}?cat={$row['id']}\">[+]</a><br />\n";
			}
		}

		$right[] = $row['rgt'];
	}
	return $output;
}


// update the paths for part of the tree
function updateDbPath($cat,$recursive=false){
	global $settings;

	if(is_array($cat)){
		$cats = $cat;
	} else {
		if($recursive){
			$cats = subCategories($cat);
		} else {
			$cats = array($cat);
		}
	}

	foreach($cats as $id){
		$path = categoryCleanedPath($id);
		$query = 'UPDATE '.$settings['categories_table'].' SET path = "'.mysql_safe($path).'" WHERE id="'.mysql_safe($id).'"';
		mysql_query($query);
	}
}

function rewriteUrl($catid,$catname,$path,$page=null,$site_url=null){
	global $settings;

	switch($settings['url_type']){
		case '1':
			$url = $site_url.$settings['index_file'].'?viewcat='.$catid;
			if(!empty($page) && $page > 0){
				$url .= '&page='.$page;
			}
			break;
		case '2':
			$url = $site_url.$settings['mod_rewrite_url'].$catid.'_'.cleanUrl($catname);
			if(!empty($page) && $page > 0){
				$url .= '/'.$page;
			}
			break;
		case '3':
			$pathparts = explode('/',$path);
			$url = $site_url.$settings['mod_rewrite_url'];
			foreach($pathparts as $part){
				$url .= cleanUrl($part) . '/';
			}
			if($settings['use_pagination'] && !empty($page) && $page > 0){
				$url .= 'page'.$page.'.html';
			}
			break;
	}

	return $url;

}

function catIdByPath($path){
	global $settings;
	$path = trim($path,' /');
	$query = 'SELECT id FROM `'.$settings['categories_table'].'` WHERE path="'.mysql_safe($path).'"';
	$result = mysql_query($query);
	$row = mysql_fetch_array($result);
	return $row['id'];
}

function getSettings(){
	global $settings;

	$query = 'SELECT * FROM `'.$settings['settings_table'].'`';
	$result = mysql_query($query);

	while($row = mysql_fetch_array($result)){
		if($row['setting'] != 'admin_user' && $row['setting'] != 'admin_pass'){
			$val = unserialize($row['value']);
		} else {
			$val = $row['value'];
		}
		$db_settings[$row['setting']] = $val;
	}
	$db_settings['actual_path'] = $db_settings['index_file'];
	if($db_settings['url_type'] != '1'){
		$db_settings['index_file'] = $db_settings['mod_rewrite_url'];
	}

	foreach($settings as $key=>$value){
		$db_settings[$key] = $value;
	}

	return $db_settings;

}

function manual_serialize($str,$type){

	$str = gpcStripSlashes($str);

	if($type=='bool'){
		if($str == '1' || $str == true){
			$ret = 'b:1;';
		} else {
			$ret = 'b:0;';
		}
	} elseif($type=='str'){
		$ret = serialize($str);
	} elseif($type=='int' && is_numeric($str)){
		$ret = 'i:'.$str.';';
	} else {
		return false;
	}

	return $ret;

}

function admin_output($currpage){

	global $yald_head, $yald_body, $template, $settings;

	$active = array('categories'=>'','stats'=>'','broken_links'=>'','add_link'=>'','approve_links'=>'','config'=>'');

	$active[$currpage] = 'class="active"';

	$template = str_replace(array('@YALD_BODY@','@YALD_HEAD@','@APPROVE_LINKS_ACTIVE@','@ADD_LINK_ACTIVE@','@CATEGORIES_ACTIVE@','@BROKEN_LINKS_ACTIVE@','@STATS_ACTIVE@','@CONFIG_ACTIVE@','@DIRECTORY_INDEX@'),array($yald_body,$yald_head,$active['approve_links'],$active['add_link'],$active['categories'],$active['broken_links'],$active['stats'],$active['config'],$settings['index_file']),$template);

	return $template;
}
?>
Return current item: Yet Another Link Directory