<?php

$core = new acidroot();
$core->main();

class acidroot
{
	#############################################
	var $inc   = 'dainc/';
	var $head  = 'header.html';
	var $foot  = 'footer.html';
	#############################################
	var $url     = 'http://acid-root.new.fr';
	var $content = '';
	var $ls_pg   = '16';
	#############################################
	var $downl = array('rar','zip');
	#############################################
	var $ctype = array( 'html' =>  'text/html',
			    'txt'  =>  'text/html',
			    'xml'  =>  'application/xml',
			    'jpg'  =>  'image/jpeg', 
			    'rar'  =>  'application/x-rar',
			    'css'  =>  'text/css');
	#############################################
	var $pages = array( '0' => array('Advisories', 'html', TRUE),
			    '1' => array('Exploits', 'html', TRUE),
			    '2' => array('Tools', 'html', TRUE),
			    '3' => array('Rss', 'xml', FALSE),
			    '4' => array('Images', '', TRUE));
	#############################################
	
	function main()
	{
		$this->req  = explode(':', $_SERVER['QUERY_STRING']);
		$this->int1 = (int)$this->req[0];
		$this->int2 = (int)$this->req[1];
		$this->int3 = (int)$this->req[2];
		
		if($this->pages[$this->int1] == FALSE)
		$this->redirect($this->url);
		
		list($this->dir,$this->index,$this->out) =
		$this->pages[$this->int1];

		$this->c_dir      = $this->inc.$this->dir;
		$this->dir_handle = opendir($this->c_dir);
		
		# 0:x 1:x 2:x 3:x
		if(!empty($this->index))
		{
			$this->files = array('0' => 'index.'.$this->index);
			
			# 0:0 1:0 2:0 3:0
			if(empty($this->int2))
			{
				# 0:0 1:0 2:0
				if($this->index === 'html')
				{
					$this->cont  = $this->content_by_arr(
							       array(
							       $this->inc.$this->head,
							       $this->c_dir.'/'.$this->files[0],
							       $this->inc.$this->foot));
				}
				else
				{	# 3:0
					$this->cont = $this->content_by_arr(array($this->c_dir.'/'.$this->files[0]));
				}

				$this->print_ex($this->cont, $this->index);
			}
			
			$this->i = 1;
		}
		else
		{	# 4:x
			$this->files = array();
			$this->i = 0;
		}
		
		# 0:x 1:x 2:x 4:x
		if($this->out)
		{
			while(($this->dir_file = readdir($this->dir_handle)) !== FALSE)
			{
				if(!in_array($this->dir_file, array('.','..'))
				&& !eregi('index', $this->dir_file))
				{
					$this->files[$this->i] = $this->dir_file;
					$this->i++;
				}
			}
		}
		
		# 0:x<>count 1:x<>count 2:x<>count 4:x<>count 3:!=0
		if($this->files[$this->int2] == FALSE)
		$this->redirect($this->url);
		
		$this->file = $this->c_dir.'/'.$this->files[$this->int2];
		$this->cont = $this->content_by_arr(array($this->file));
		
		# 0:x_ok 1:x_ok 2:x_ok 4:x_ok
		$this->print_ex($this->cont, $this->file);
		exit(0);
	}
	
	function print_ex($content, $ext)
	{
		$this->filn = basename($ext);
		$this->ext  = ereg('\.', $this->filn) ? strtolower(substr(strrchr($this->filn, '.'),1)) : $this->filn;
		$this->type = array();

		# On the server but the extension isn't allowed
		if($this->ctype[$this->ext] == FALSE)
		$this->redirect($this->url);
		
		else
		$this->type[] = 'Content-Type: '.$this->ctype[$this->ext];
		
		if(in_array($this->ext, $this->downl))
		$this->type[] = 'Content-Disposition: attachment; filename="'.$this->filn.'"';
		
		foreach($this->type as $this->header)
		header($this->header);
		
		if($this->ext === 'txt')
		{
			$this->content =
			'<html><body bgcolor=#000000 text=#FFFFFF><pre>'
			.htmlentities($this->content)
			.'</pre></body></html>';
		}
		
		print $this->content;
		exit(0);
	}
	
	function content_by_arr($page_array)
	{
		foreach($page_array as $path)
		{
			if(count($page_array) > 1)
			$this->content .= "\r\n";
			
			if($this->int3 !== ''
			&& $page_array[1] === $path)
			{
				$this->file_arr = explode("\n", file_get_contents($path));
				$this->count    = count($this->file_arr);
				
				if($this->count <= $this->ls_pg)
				{
					$this->ls_pg = $this->count-1;
					$this->int3  = 0;
				}
				
				$this->dep = ($this->int3)*$this->ls_pg;
				$this->end = $this->dep+$this->ls_pg;
				
				if($this->dep >= $this->count
				|| $this->end >= $this->count)
				{
					$this->dep = $this->ls_pg;
					$this->end = $this->dep + $this->ls_pg;
				}

				for($i=$this->dep;$i<=$this->end;$i++)
				$this->content .= $this->file_arr[$i]."\n";

				$this->nb_page  = round(count($this->file_arr) / $this->ls_pg);
				$this->content .= '</table>';
				
				if($this->nb_page >1)
				{
					$this->content .= "<div align='center' class='normal'><b>";
				
					for($i=0;$i<$this->nb_page;$i++)
					$this->content .= '<a href=?'.$this->int1.':'.$this->int2.':'.$i.'>'.$i.'</a>'
							  .($i == $this->nb_page-1 ? '' : ' | ');
				
					$this->content .= '</b>';
				}
				
			}

			else
			$this->content .= file_get_contents($path);
		}

		return $this->content;
	}
	
	function redirect($url)
	{
		header('Location: '.trim($url));
		exit(1);
	}

}

?>