Skip to content

Instantly share code, notes, and snippets.

@Hormold
Created October 2, 2012 05:36
Show Gist options
  • Save Hormold/3816404 to your computer and use it in GitHub Desktop.
Save Hormold/3816404 to your computer and use it in GitHub Desktop.
Guestbook. Source code example
<div class="alink"><i onClick="location.href='?admin=1';">Login</i></div>
 body{
}
#main{margin-top:10px;margin-left:30px;}
#header{font-size:42px;font-family:sans-serif;font-weight:bold;border-bottom:2px solid black;width:80%;}
.msg{margin-top:10px;}
#page{font-family:sans-serif;margin-top:10px;width:70%;text-align:left;}
.alink{position:absolute;right:10px;top:5px;font-weight:italic;}
#info{border-bottom:1px dashed black;width:65%;}
#author{}
#date{font-style:italic;margin-top:6px;}
#text{margin-top:5px;margin-left:20px;width:76%;}
form{width:60%;margin-top:25px;}
legend{font-size:30px;font-family:sans-serif;font-weight:bold;}
#copiright{position:relative;left:100px;margin-top:20px;border-top:1px solid #8d8d8d;width:70%;text-align:center;color:#8d8d8d;font-style:italic;margin-bottom:20px;}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> gBook </title>
<style type="text/css">
.alink{
position:absolute;
right:20px;top:10px;
font-weight:italic;
}
#main{
margin-top:50px;
margin-left:170px;
}
#header{
font-size:42px;
font-family: sans-serif; /* Ðóáëåíûé øðèôò */
font-weight: bold; /* Æèðíîå íà÷åðòàíèå */
border-bottom: 2px solid black;
width:70%;
}
.msg{
margin-top:10px;
}
#info{border-bottom: 1px dashed black;width:35%;}
#author{}
#date{font-style:italic;margin-top:6px;}
#text{margin-top:5px;margin-left:20px;width:76%;}
form{width:60%;margin-top:25px;}
legend{font-size:30px;
font-family: sans-serif; /* Ðóáëåíûé øðèôò */
font-weight: bold; /* Æèðíîå íà÷åðòàíèå */}
#copiright{
position:relative;
left:100px;
margin-top:20px;
border-top: 1px solid #8d8d8d;
width:55%;
text-align:center;
color:#8d8d8d;
font-style:italic;
margin-bottom:20px;
}
</style>
</head>
<body>
<div class="alink"><i onClick="location.href='?admin=1';">Login</i></div>
<div id="main">
<div id="header">guestBook</div>
<div class="msg">
<div id="info"><span id="auhor">Name: Hormold</span> <span id="date">(28.01.2011)</span></div>
<div id="text">Estoy probando este guestbook, al parecer es muy bueno y tal vez pueda ser customizable, veremos que pasa</div>
</div>
<div class="msg">
<div id="info"><span id="auhor"><b>Name:</b> Hormold</span></div>
<div id="text">Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
sed diem nonummy nibh euismod tincidunt ut lacreet dolore magna
aliguam erat volutpat.Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
sed diem nonummy nibh euismod tincidunt ut lacreet dolore magna
aliguam erat volutpat.Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
sed diem nonummy nibh euismod tincidunt ut lacreet dolore magna
aliguam erat volutpat.Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
sed diem nonummy nibh euismod tincidunt ut lacreet dolore magna
aliguam erat volutpat.</div>
<span id="date">Posted: Friday, January 07 2011 - 01:38 AM</span>
</div>
<div class="msg">
<div id="info"><span id="auhor"><b>Name:</b> Hormold</span> <span id="date">(28.01.2011)</span></div>
<div id="text">Estoy probando este guestbook, al parecer es muy bueno y tal vez pueda ser customizable, veremos que pasa</div>
</div>
<form method="post" action="index.php">
<fieldset>
<legend>Write</legend>
<label for="name">Name:<br /></label>
<input type="text" name="name" id="name" maxlength="128" class="inputText" />
<br />
<label for="comments">Comments (<span title="[b]...[/b], [i]...[/i]">bb-tags</span> support):<br /></label>
<textarea name="comments" id="comments" cols="40" rows="4" class="inputTextArea">
</textarea>
<br />
<!--label for="electricsheep">Enter Code:</label>
<input type="text" maxlength="5" size="5" class="inputText" />
<img src="index.php?action=challengeimage"/>
<br /-->
<input type="submit" value="Add" class="submit" />
</fieldset>
</form>
<div id="copiright">(C) Hormold 2012</div>
</div>
</body>
</html>
 <h1>Вход для модераторов</h1>
<p>Если вы &mdash; модератор, то введите свои логин и пароль в поля ниже:</p>
<form method="post">
<input type="text" class="text" name="login" />
<input type="password" class="text" name="password" />
<input type="submit" class="button" value="Вход!" />
</form>
CREATE TABLE `records` (
`id` int(11) NOT NULL auto_increment,
`time` varchar(256) NOT NULL,
`name` longtext NOT NULL,
`comment` longtext NOT NULL,
`ip` varchar(256) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
<?php
session_start();
$_POST=gbook::secure_array($_POST);
$_GET=gbook::secure_array($_GET);
if(!isset($_GET["page"])){$_GET["page"]=1;}
$book=new gbook;
$book->admin=Array(
"hormold"=>"123456"
);
$book->tpl_load("main");
$book->tpl("main","title","guest Book");
$tmp["main"]=$book->load_rec($_GET["page"]);
if(count($_POST)){
if(isset($_COOKIE["last"]) and (time()-$_COOKIE["last"])<20){
$tmp["main"]="<h2>Too fast!</h2>";
}else{
$o=$book->post_rec($_POST);
setcookie("last",time());
if($o!==false){$tmp["main"]=$o;}
}
}
$tmp["page"]=$book->page;
if(isset($_GET["admin"])){
if($book->admin_check($_POST)){echo $book->admin_login();}else{$tmp["main"]=$book->mini_tpl("login",Array());$tmp["page"]="";}
}
if(isset($_GET["delete"]) and $book->isadmin()){
if($book->admin_delete($_GET["delete"])){
echo $book->redirect("?");
}
}
echo $book->compilate("main",$tmp);
class gbook{
var $link;
var $cachetime=20;
var $pp=5; //per page
var $mtpl_arr,$error,$admin;
var $page="";
function gbook(){
$this->link=mysql_connect("127.0.0.1","root","*****") or die('Could not connect: ' . mysql_error());
mysql_set_charset("UTF-8");
mysql_select_db("gbook",$this->link);
}
function __destruct() {
mysql_close($this->link);
}
function load_rec($page=1){
if($page<=0){$page=1;}
$this->page="";
$out="";
if($page==1 or $page==0){$p1="0";$p2=$this->pp;}else{$p=$page*5;$p1=$p-5;$p2=$p;}
$sql=mysql_query("SELECT * FROM `records` ORDER BY `id` DESC LIMIT $p1,5",$this->link) or die('MySQL Error: ' . mysql_error());;
$ar=Array();$i=0;
while ($row = mysql_fetch_array($sql,MYSQL_ASSOC)) {
$out.=$this->mini_tpl("rec",$row);$i++;
}$np=$page-1;
if($i==0){return $this->redirect("?page=".$np);}
$this->page.="Page: <b>$page</b>. ";
if($i>=4 or $page!==1){
$back=$page-1;
$to=$page+1;
if($page==1){
unset($back);
}
if(isset($back)){$this->page.="<a href='?page=$back' id='previous_page' title='Ctrl+Left'>&lt;- Back</a> | ";}
if(isset($to) and $i>=5 ){$this->page.="<a href='?page=$to' id='next_page' title='Ctrl+Right'>To -&gt;</a>";}
}
return $out;
}
function post_rec($input){
if((isset($input["name"]) and trim($input["name"])!=="") and (isset($input["comments"]) and trim($input["comments"])!=="")){
$ip=$_SERVER['REMOTE_ADDR'];
$sql=mysql_query("INSERT INTO `records` (`id` ,`time` ,`name` ,`comment`, `ip`) VALUES (NULL , '".time()."', '".$input["name"]."', '".$input["comments"]."', '".$ip."');",$this->link) or die("MySQL Error:".mysql_error());
if($sql){
return $this->load_rec();
}else{
return false;
}
}else{
$out="<h2>Error, check fields</h2>";
return false;
}
}
function tpl_load($filename){
$file="tpl/".$filename.".tpl.php";
$this->tpl_arr[$filename]=file_get_contents($file);
return true;
}
function tpl($tpl,$key,$value){
$this->tpl_arr[$tpl]=str_replace("{%".$key."%}",$value,$this->tpl_arr[$tpl]);
return $this->tpl_arr[$tpl];
}
function mini_tpl($tpl,$array){
if(1){$this->mtpl_arr[$tpl]=file_get_contents("tpl/".$tpl.".tpl.php");}
if($this->isAdmin()){$array["control"]="<a href='?delete=".$array["id"]."'>Delete</a>";}else{$array["control"]="";}
if(isset($array) and count($array)){
foreach($array as $k=>$v){
if($k=="time"){$v=date("H:i:s, d/m/Y",$v);}
if($k=="comment"){$v=$this->bbcode($v);}
$this->mtpl_arr[$tpl]=str_replace("{%".$k."%}",$v,$this->mtpl_arr[$tpl]);
}
}
return $this->mtpl_arr[$tpl];
}
function compilate($tpl,$array=Array()){
if(is_file("tpl/".$tpl.".html")){
$t=time()-filemtime("tpl/".$tpl.".html");
if($t<$this->cachetime){$out=file_get_contents("tpl/".$tpl.".html");}else{unlink("tpl/".$tpl.".html");$out=$this->compilate($tpl);}
}else{
if(!$this->tpl_arr[$tpl]){$this->tpl_load($tpl);}
$out=$this->tpl_arr[$tpl]=preg_replace_callback("/{#(.*?)#}/is", create_function(
'$matches',
'return file_get_contents("tpl/$matches[1].tpl.php");'
), $this->tpl_arr[$tpl]);
$out=preg_replace_callback("/{!(.*?)!}/is", create_function(
'$matches',
'return eval("$matches[1]");'
), $this->tpl_arr[$tpl]);
file_put_contents("tpl/".$tpl.".html",$out);
}
if(isset($array) and count($array)){
foreach($array as $k=>$v){
$out=str_replace('{$'.$k.'$}',$v,$out);
}
}
return $out;
}
function bbcode($code){
return preg_replace(
Array(
"#\r\n#",
"#&lt;br /&gt;#",
"#\[b\](.*?)\[/b\]#U",
"#\[s\](.*?)\[/s\]#U",
"#\[i\](.*?)\[/i\]#U",
"#\[u\](.*?)\[/u\]#U"
),
Array(
"<br />",
"<br />",
"<b>$1</b>",
"<s>$1</s>",
"<i>$1</i>",
"<u>$1</u>"
)
,$code);
}
///admin module
function admin_check($input){
if(isset($this->admin[strtolower($input["login"])])){
if($this->admin[$input["login"]]==$input["password"]){return true;}
}
}
function admin_login(){
$_SESSION["login"]=true;
return $this->redirect("?");
}
function admin_delete($id){
$sql=mysql_query("DELETE FROM `records` WHERE `id`=$id",$this->link) or die("MySQL Error: ".mysql_error());
if($sql){return true;}else{return false;}
}
function isadmin(){
if(isset($_SESSION["login"])){
return true;
}else{
return false;
}
}
static function secure_array($input){
$output=Array();
foreach($input as $k=>$v){
if(!is_array($v) and !is_array($k)){
$output[$k]=htmlspecialchars(str_replace("'","`",$v));
}
}
return $output;
}
function redirect($where="?"){
return '<script>location.href="'.$where.'";</script>';
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru" lang="ru" dir="ltr">
<head>
<title> gBook</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
 body{
}
#main{margin-top:10px;margin-left:30px;}
#header{font-size:42px;font-family:sans-serif;font-weight:bold;border-bottom:2px solid black;width:80%;}
.msg{margin-top:10px;}
#page{font-family:sans-serif;margin-top:10px;width:70%;text-align:left;}
.alink{position:absolute;right:10px;top:5px;font-weight:italic;}
#info{border-bottom:1px dashed black;width:65%;}
#author{}
#date{font-style:italic;margin-top:6px;}
#text{margin-top:5px;margin-left:20px;width:76%;}
form{width:60%;margin-top:25px;}
legend{font-size:30px;font-family:sans-serif;font-weight:bold;}
#copiright{position:relative;left:100px;margin-top:20px;border-top:1px solid #8d8d8d;width:70%;text-align:center;color:#8d8d8d;font-style:italic;margin-bottom:20px;}
</style>
<script>
function init() {
document.onkeydown = hotkeys;
}
function hotkeys(e) {
if (!e) e = window.event;
var k = e.keyCode;
if (e.ctrlKey) {
if (k == 37) { d = document.getElementById('previous_page'); } // Ctrl+Left
if (k == 39) { d = document.getElementById('next_page'); } // Ctrl+Right
}
if (d) location.href = d.href;
}
function ctrl_enter(e, form) {
if (((e.keyCode == 13) || (e.keyCode == 10)) && (e.ctrlKey == true)) form.submit();
}
</script>
</head>
<body onload="init()" onpageshow="if (event.persisted) {init();}">
<div class="alink">Welcome, admin!</div>
<div id="main">
<div id="header"><a href='index.php' style='text-decoration: none;color:black !important;'>gBook</a></div>
{$main$}
<div id="page">{$page$}</div>
 <form method="post" action="index.php">
<fieldset>
<legend>Write</legend>
<label for="name">Name:<br /></label>
<input type="text" name="name" id="name" maxlength="128" class="inputText" />
<br />
<label for="comments">Comments (<span title="[b]...[/b], [i]...[/i]">bb-tags support</span>):<br /></label>
<textarea name="comments" onkeypress="return ctrl_enter(event, this.form);" id="comments" style="margin: 2px;width: 895px;height: 98px;" class="inputTextArea"></textarea>
<br />
<!--label for="electricsheep">Enter Code:</label>
<input type="text" maxlength="5" size="5" class="inputText" />
<img src="index.php?action=challengeimage"/>
<br /-->
<input type="submit" value="Add new comment" style="width:150px" class="submit" />
</fieldset>
</form>
<div id="copiright">(C) Hormold 2012</div>
</div>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru" lang="ru" dir="ltr">
<head>
<title> {%title%}</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
{#css#}
</style>
<script>
function init() {
document.onkeydown = hotkeys;
}
function hotkeys(e) {
if (!e) e = window.event;
var k = e.keyCode;
if (e.ctrlKey) {
if (k == 37) { d = document.getElementById('previous_page'); } // Ctrl+Left
if (k == 39) { d = document.getElementById('next_page'); } // Ctrl+Right
}
if (d) location.href = d.href;
}
function ctrl_enter(e, form) {
if (((e.keyCode == 13) || (e.keyCode == 10)) && (e.ctrlKey == true)) form.submit();
}
</script>
</head>
<body onload="init()" onpageshow="if (event.persisted) {init();}">
<div class="alink">{!if(isset($_SESSION["login"])){return 'Welcome, admin!';}else{return "<i onClick=\"location.href='?admin=1';\">Login</i>";}!}</div>
<div id="main">
<div id="header"><a href='index.php' style='text-decoration: none;color:black !important;'>{%title%}</a></div>
{$main$}
<div id="page">{$page$}</div>
{#form#}
<div id="copiright">(C) Hormold 2012</div>
</div>
</body>
</html>
<div class="msg">
<div id="info"><span id="auhor"><b>Name:</b> {%name%}</span> <span id="date">({%time%})</span> {%control%}</div>
<div id="text">{%comment%}</div>
<div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment