<?php

class bin {
	
	var $db;
	
	function __construct($db) {
		$this->db = $db;
	}
	
	public function view() {
		$id = $_GET['view'];
		
		$sql = "SELECT * FROM `Bin` WHERE `id`='$id'";
		if ($result = $this->db->query($sql)) {
			if ($row = $result->fetch_array(MYSQLI_ASSOC)) {
                echo "<b>Id:</b> ".$id."<br>\n";
                echo "<b>Subject:</b> ".$row['subject']."<br>\n";
                echo "<b>Author:</b> ".$row['author']."<br>\n";
                echo "<b>Added on</b> ".$row['time']."<br><br>\n";
                $code = stripslashes($row['content']);
				echo $code;
			}
		} else {
			echo "Invalid id.<br />";
		}
	}
	
	public function showall() {
		?>

<br />
<table width="100%" cellspacing="0" cellpadding="3" style="border: 1px solid #ce0000">
<tr style="background-color: #333333; color: #FFFFFF; font-weight: bold;">
  <td width="10%">Id</td>
  <td width="10%">Syntax</td>
  <td width="40%">Subject</td>
  <td width="20%">Author</td>
  <td width="20%">Time Added</td>
</tr>
<?php
		$sql = "SELECT * FROM `Bin` ORDER BY `id` DESC";
		if ($result = $this->db->query($sql)) {
			while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
				foreach($row as $key => $value)
					$$key = $value;
				echo "<tr><td>$id</td><td>$syntax</td><td><a href=\"bin.php?view=$id\">$subject</a></td><td>$author</td><td>$time</td></tr>\n";
			}
		}
		echo "</table><br />";
	}
	
	function add() {
		foreach ($_POST as $key => $value) 
			$$key = $value;
		if (empty($author) or empty($subject) or empty($content)) {
			echo "<div id=\"form_error\">Please fill out the form completely.</div><br>\n";
			include ('binhtml.php');
			return;
		}

		$options = array('numbers' => HL_NUMBERS_TABLE,'tabsize' => 4);
		$renderer =& new Text_Highlighter_Renderer_HTML($options);
		$hl =& Text_Highlighter::factory($syntax); // make sure $_POST['syntax'] is legit
		$hl->setRenderer($renderer);
		$content = stripslashes($content);
		$html = $hl->highlight($content);
		
		$html = $this->db->real_escape_string($html);
		$sql = "INSERT INTO `Bin` (`subject`,`syntax`,`author`,`content`) VALUES ('$subject','$syntax','$author','$html')";
		if ($result = $this->db->query($sql)) {
			echo "<br />Your entry has been added.<br />";
		} else {
			echo "<div id=\"error\">There was an error while attempting to process your request.</div>";
		}
	}
	
	function show_add_form() {
	?>
		<form id="bin" name="bin" method="post" action="bin.php">
		  <table width="75%" border="0" cellspacing="0" cellpadding="2">
			<tr>
			  <td width="15%">Your Name </td>
			  <td width="85%"><label>
				<input name="author" type="text" id="textfield" maxlength="25"/>
				</label></td>
			</tr>
			<tr>
			  <td>Subject</td>
			  <td><input name="subject" type="text" id="textfield" size="50" maxlength="75"/></td>
			</tr>
			<tr>
			  <td>Syntax</td>
			  <td><label>
				<select name="syntax" id="textfield" />
				
				<option value="cpp" selected="selected">C/C++</option>
				<option value="php">PHP</option>
				<option value="java">Java</option>
				<option value="perl">Perl</option>
				<option value="ruby">Ruby</option>
				<option value="python">Python</option>
				</select>
				</label></td>
			</tr>
			<tr>
			  <td colspan="2">Content</td>
			</tr>
			<tr>
			  <td colspan="2"><label>
				<textarea name="content" cols="100" rows="10" id="textfield"></textarea>
				</label></td>
			</tr>
			<tr>
			  <td colspan="2"><label>
				<input type="submit" name="add" value="Add" id="textfield" />
				</label></td>
			</tr>
		  </table>
		  <label></label>
		</form>
	<?php
	}
	
}

?>