Location: PHPKode > scripts > NAH E-Book/Webcomic Displayer > index.php
<?php

include('settings.inc.php');
include('templates.php');

function rss()
{
	$pl = explode("/",trim($_SERVER['REQUEST_URI'],"/"));

	$chapters = array();
	$rss_counter = 0;
	$handle = @opendir(rtrim(CHAPTER_DIR,'\\/').'/');
	while (($file = readdir($handle)) != false)
	{
		if($file != '.' AND $file != '..')
		{
			$chapters[] = $file;
		}
	}
	@closedir($handle);
	if(count($chapters))
		arsort($chapters,SORT_NUMERIC);
	
	foreach($chapters as $key => $value)
	{
		$pages = array();
		list($tmp1,$tmp2) = explode('-',$value,2);
		$handle = @opendir(rtrim(CHAPTER_DIR,'\\/').'/'.$value);
		while (($file = readdir($handle)) != false)
		{
			if($file != '.' AND $file != '..')
			{
				$pages[] = $file;
			}
		}
		@closedir($handle);

		arsort($pages,SORT_NUMERIC);
		foreach($pages as $key2 => $value2)
		{
			$fdate = gmdate("D, d M Y H:m:s",filemtime(rtrim(CHAPTER_DIR,'\\/').'/'.$value.'/'.$value2))." GMT";
			$title = $tmp2.' - Page '.i_name($value2);
			$body = '';
			if($i == 0)
				$lbdate = $fdate;

			if(CLEAN_URLS)
				$link = 'http://'.$_SERVER['SERVER_NAME'].WEB_OFFSET.$tmp1.'/'.i_name($value2).'/';
			else
				$link = 'http://'.$_SERVER['SERVER_NAME'].str_replace('?l=rss','',$_SERVER['REQUEST_URI']).'?l='.$tmp1.'/'.i_name($value2);
			$out .= popt('rss.item', 
				array(
					'title' => str_replace('&','and',$title),
					'bd' => str_replace('&','and',$body),
					'fdate' => $fdate,
					'link' => str_replace('&','-',$link)
				)
			);

			$rss_counter++;
			if(($rss_counter+1) > RSS_ITEMS)
				break;
		}
	}

	return
		popt('rss',array('pn' => str_replace('&','and',TITLE), 'serv' => $_SERVER['SERVER_NAME'], 'tagd' => htmlspecialchars(str_replace('&','and',META_DESC)), 'items' => $out, 'lbdate' => $lbdate));

}

function test_mysql()
{
	$out = 0;
	if(COMMENTS_ENABLED)
	{
		$db = @mysql_connect(MYSQL_HOST, MYSQL_USERNAME, MYSQL_PASSWORD);

		if($db AND MYSQL_HOST AND MYSQL_USERNAME AND MYSQL_PASSWORD AND MYSQL_TABLE)
		{
			$db2 = mysql_select_db(MYSQL_TABLE, $db);
			if($db2)
				$out = 1;
		}
	}
	return $out;
}

function web_offset()
{
	if(CLEAN_URLS)
		return WEB_OFFSET;
	else
		return '';
}

function clean_urls($url)
{
	if(CLEAN_URLS)
		return preg_replace('/\?l=(.*)"/i',web_offset().'$1/"',$url);
	else
		return $url;
}

function i_name($str)
{
	$vars = explode('.',$str);
	return str_replace('.'.$vars[count($vars)-1],'',$str);
}

function getip()
{
   if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
           $ip = getenv("HTTP_CLIENT_IP");
       else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
           $ip = getenv("HTTP_X_FORWARDED_FOR");
       else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
           $ip = getenv("REMOTE_ADDR");
       else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
           $ip = $_SERVER['REMOTE_ADDR'];
       else
           $ip = "unknown";
   return($ip);
}

function ajax()
{
	$mode = $_GET['mode'];
	$id = $_GET['id'];

	switch($mode)
	{
	case 'add_com':
		sleep(1);
		$name = $_GET['name'];
		$id = urldecode(str_replace('[--26]','&',stripslashes($id)));
		$name = urldecode(str_replace('[--26]','&',stripslashes($name)));
		$c = $_GET['c'];
		$p = $_GET['p'];
		if(strlen($id) > COM_MAX_LEN OR !strlen($id))
			echo 'err: '.strlen($id).' characters (Required: 1..'.COM_MAX_LEN.')';
		else
		{
			if(!mysql_num_rows(mysql_query("SELECT id FROM instant WHERE target_c='$c' AND target_p='$p' AND owner_ip='.getip().' LIMIT 1")))
			{
				mysql_query("INSERT INTO instant (target_c,target_p,owner_name,text,owner_ip,date) VALUES ('$c', '$p', '".addslashes(strip_tags($name))."',  '".addslashes(strip_tags($id))."', '".getip()."', '".time()."')");
				if(mysql_affected_rows())
				{
					setcookie('uname',$name,time()+31536000,'/'); 
					$class = '';
					$cd = mysql_query("SELECT count(id) as count FROM instant WHERE target_c='$c' AND target_p='$p'");
					$tot = mysql_fetch_object($cd);
					if($tot->count > 1)
						$class = '2';
					echo popt('display_page.instant.comments.item', array('class' => $class, 'txt' => nl2br($id),  'uname' => $name, 'fdate' => date('H:i d-M-Y')));
				}
			}
		}
	break;
	}
}

function list_comments($revid,$tarp)
{
	$query = mysql_query("SELECT owner,owner_name,text,date FROM instant WHERE target_c='$revid' AND target_p='$tarp' ORDER BY date DESC");
	$n = mysql_num_rows($query);
	if($n)
	{
		for($i=0;$i<$n;$i++)
		{
			mysql_data_seek($query,$i);
			$dat = mysql_fetch_object($query);
			

			$class = '';
			if($n >= 0  AND $i != $n-1)
				$class = '2';

			$items .= popt('display_page.instant.comments.item', array('class' => $class, 'txt' => nl2br(trim($dat->text)), 'uname' => $dat->owner_name, 'fdate' => date('H:i d-M-Y', $dat->date)));
		}
		$out = $items;
	}

	return($out);
} 

function popt($templt,$arr)
{
	global $T;

	if(!isset($T[$templt]))
		include('templates.php');

	$out = $T[$templt];

	if(is_array($arr))
	{
		foreach($arr as $key => $value)
			$out = str_replace("{".$key."}",$arr[$key],$out);
	}

	return($out);

}

function display_page($chapter,$page)
{
	global $TITLE, $mysql_con;

	if($_POST['create_table'])
	{
		if(mysql_query(popt('mysql_table', '')))
			echo 'MYSQL TABLE Successfully Created';
		else
			echo 'MYSQL TABLE creation failed!';
	}

	$clist = $plist = array();

	$handle = @opendir(rtrim(CHAPTER_DIR,'\\/').'/');
	while (($file = readdir($handle)) != false)
	{
		if($file != '.' AND $file != '..')
			$clist[] = $file;
	}
	@closedir($handle);
	sort($clist,SORT_NUMERIC);

	if(!$chapter)
		list($last_chapter_id,$last_chapter_title) = explode('-',$clist[count($clist)-1],2);
	else
	{
		foreach($clist as $key => $value)
		{
			list($tmp1,$tmp2) = explode('-',$value,2);
			if($tmp1 == $chapter)
			{
				$last_chapter_id = $tmp1;
				$last_chapter_title = $tmp2;
				break;
			}
		}
	}

	$handle = @opendir(rtrim(CHAPTER_DIR,'\\/').'/'.$last_chapter_id.'-'.$last_chapter_title.'/');
	while (($file = @readdir($handle)) != false)
	{
		if($file != '.' AND $file != '..')
			$plist[] = $file;
	}
	@closedir($handle);
	sort($plist, SORT_NUMERIC);
	if(!$page)
	{
		$curr_page = i_name($plist[count($plist)-1]);
		$curr_page_s = $plist[count($plist)-1];
	}
	elseif($chapter)
	{
		foreach($plist as $key => $value)
		{
			if(i_name($value) == $page)
			{
				$curr_page = i_name($value);
				$curr_page_s = $value;
				break;
			}
		}
	}
	if($chapter AND !$page)
	{
		$curr_page = i_name($plist[0]);
		$curr_page_s = $plist[0];
	}

	$TITLE .= ' - '.str_replace('_',' ',$last_chapter_title).', Page '.$curr_page;

	$c_options = $p_options = '';

	foreach($clist as $key => $value)
	{
		list($v1,$v2) = explode('-', $value, 2);
		$c_options .= popt('index.prevnext.clist.option', array('v1' => $v1, 'v2' => $v2));
	}

	foreach($plist as $key => $value)
		$p_options .= popt('index.prevnext.plist.option', array('v1' => i_name($value), 'v2' => i_name($value)));

	$curr_id = array_search($curr_page_s,$plist);
	$previous = popt('index.prevnext.link_p_inactive', '');
	if($curr_page_s != $plist[0] AND $curr_page_s)
		$previous = clean_urls(popt('index.prevnext.link_p', array('v1' => $last_chapter_id, 'v2' => i_name($plist[$curr_id-1]))));

	$next = popt('index.prevnext.link_n_inactive', '');
	$next_s = $next_e = '';
	if($curr_page_s != $plist[count($plist)-1])
	{
		$next = clean_urls(popt('index.prevnext.link_n', array('v1' => $last_chapter_id, 'v2' => i_name($plist[$curr_id+1]))));
		$next_s = clean_urls(popt('display_page.next_s', array('v1' => $last_chapter_id, 'v2' => i_name($plist[$curr_id+1]))));
		$next_e = popt('display_page.next_e', '');
	}

	$instant_form = $comments = '';
	if(COMMENTS_ENABLED AND $curr_page AND $mysql_con)
	{
		$test_table = mysql_query("SHOW tables like 'instant'");
		if(!mysql_num_rows($test_table))
			$comments = popt('mysql_err2', array('REQUEST_URI' => $_SERVER['REQUEST_URI']));
		else
		{
			if(!mysql_num_rows(mysql_query("SELECT id FROM instant WHERE target_c='".intval($last_chapter_id)."' AND target_p='".addslashes(stripslashes($curr_page))."' AND owner_ip='".getip()."'")))
			{
				$name = '';
				if($_COOKIE['uname'])
					$name = $_COOKIE['uname'];
				$instant_form = popt('display_page.instant.form', array('c' => $last_chapter_id, 'p' => $curr_page, 'name' => $name, 'LEN' => COM_MAX_LEN));
			}
			$comments = popt('display_page.instant', array('items' => list_comments(intval($last_chapter_id),$curr_page), 'form' => $instant_form));
		}
	}
	if(COMMENTS_ENABLED AND $curr_page AND !$mysql_con)
		$comments = popt('mysql_err', '');
	

	$CURR = web_offset().trim(CHAPTER_DIR,'\\/').'/'.$last_chapter_id.'-'.$last_chapter_title.'/'.$curr_page_s;
	if(!$curr_page)
		$CURR = web_offset().'invalid_page.jpg';

	return popt('display_page',
			array(
				'curr' => $CURR,
				'prevnext' =>
					popt('index.prevnext',
						array(
							'options_chpt' => str_replace('value="'.$last_chapter_id.'"','value="'.$last_chapter_id.'" SELECTED',$c_options),
							'options_page' => str_replace('value="'.$curr_page.'"','value="'.$curr_page.'" SELECTED',$p_options),
							'next' => $next,
							'previous' => $previous,
							'CLEAN_URLS' => CLEAN_URLS
						)
					),
				'next_s' => $next_s,
				'next_e' => $next_e,
				'instant' => $comments
			)
		);
}

function main()
{
	return popt('main', array('last_page' => display_page(DEFAULT_CHAPTER,DEFAULT_PAGE)));
}

error_reporting(E_ALL ^ E_NOTICE);

$mysql_con = test_mysql();

if(!ini_get('register_globals')) { 
$HTTP_USER_AGENT = $_SERVER['HTTP_USER_AGENT'];
}

$TITLE = TITLE;

$load = $_GET['l'];
if(strstr($load,'/') OR intval($load))
{
	list($chapter,$page) = explode('/',trim($load,'\\/'),2);
	$CONTENTS = display_page($chapter,$page);
}
else
{
	if($load)
	{
		switch($load)
		{
			case 'ajax':
				$CONTENTS = ajax();
			break;
			case 'rss':
				$CONTENTS = rss();
			break;
			default:
				$CONTENTS = '<h1>404: Not Found</h1>';
			break;
		}
	}
	else
		$CONTENTS = main();
}


$pattern =
		array(
			'{CONTENTS}',
			'{TITLE}',
			'{TITLE_MAIN}',
			'{META_DESC}',
			'{META_KEYS}',
			'{WEB_OFFSET}'
		);

$replacement =
		array( 
			$CONTENTS,
			$TITLE,
			TITLE,
			META_DESC,
			META_KEYS,
			web_offset()
		);

if($load != 'ajax' AND $load != 'rss')
	echo str_replace($pattern, $replacement, clean_urls($T['index']));
else
	echo $CONTENTS;

?>
Return current item: NAH E-Book/Webcomic Displayer