<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Guestbook OOPhp
*
* @author Vincy6
*/
class db{
private $host;
private $name;
private $user;
private $pass;
private $connect;
public function __construct($host,$name,$user,$pass){
$this->host=$host;
$this->name=$name;
$this->user=$user;
$this->pass=$pass;
}
public function connect(){
$this->connect=mysql_connect($this->host,$this->user,$this->pass);
if(!$this->connect){
throw new Exception(".:!Errore durante la connessione al database, se l'errore persiste contattare l'admin!:.");
}else{
$select=mysql_select_db("my_vincy6web",$this->connect);
if(!$select){
exit(".:!Errore durante la selezione!:.");
}
}
}
public function disconnect(){
mysql_close($this->connect);
}
public function query($sql) {
$query = mysql_query($sql);
if(!$query)
throw new Exception("Query non eseguita!");
return $query;
}
public function fetch($query) {
return mysql_fetch_array($query);
}
}
class captcha{
private $background;
private $colortext;
#private $colorline;
public $text;
private $world=array("a","b","c","d","f","g","h","i","l","m","n","o","p","q","r","s","t","u","v","z","k","y","j","1","2","3","4","5","6","7","8","9");
private $img;
public $ctext;
public function __construct($img,$background,$colortext){
$this->img=imageCreatetruecolor($img[0],$img[1]);
$this->background=imageColorAllocate($this->img,$background[0],$background[1],$background[2]);
$this->colortext=$colortext;
}
public function __destruct(){
imageDestroy($img);
}
public function text(){
$text=array();
for($i=0;$i<8;$i++){
$u=rand(0,32);
array_push($text,$this->world[$u]);
}
$this->text=$text[0];
for($i=1;$i<8;$i++){
$this->text=$this->text." ".$text[$i];
}
$this->ctext=$text[0];
for($i=1;$i<8;$i++){
$this->ctext=$this->ctext."".$text[$i];
}
}
public function write(){
$this->colortext=imageColorAllocate($this->img,$this->colortext[0],$this->colortext[1],$this->colortext[2]);
$texto=imageString($this->img,20,75,17,$this->text,$this->colortext);
imagefill($this->img,0,0,$this->background);
imagepng($this->img,"immagine.png");
echo "<img src=\"immagine.png\">";
}
public function control($cap,$capt){
if(strcmp($cap,$capt)==0){
return true;
}else{
throw new Exception("Inserire correttamente il testo soprastante,grazie.");
return false;
}
}
}
class guestbook{
private $nome;
private $testo;
private $data;
private $ora;
public $db;
public $read=array();
public function __construct($db){
$this->db=$db;
$db->connect();
}
public function __destruct(){
$this->db->disconnect();
}
public function control($nome,$testo,$data,$ora){
$nome=trim($nome);
$testo=trim($testo);
if(empty($nome) || empty($testo)){
throw new Exception(".:!Compilare tutti i campi, grazie!:.");
return false;
}else{
$nome=htmlentities($nome);
$testo=htmlentities($testo);
$this->nome=$nome;
$this->testo=$testo;
$this->ora=$ora;
$this->data=$data;
return true;
}
}
public function read(){
$query=$this->db->query("SELECT * FROM guestbook ORDER BY id DESC");
while($all=$this->db->fetch($query)){
$this->nome=$all['nome'];
$this->testo=$all['testo'];
$this->data=$all['data'];
$this->ora=$all['ora'];
array_push($this->read,array("nome"=>$this->nome,"testo"=>$this->testo,"data"=>$this->data,"ora"=>$this->ora));
}
}
public function write($nome,$testo,$data,$ora){
if($this->control($nome,$testo,$data,$ora)){
$query=mysql_query("INSERT INTO guestbook (nome,testo,data,ora) VALUE ('$nome','$testo','$data','$ora')");
if(!$query){
throw new Exception(".:!Commento non inserito correttamente, riprovare se l'errore persiste contattatare l'admin!:.");
}else{
throw new Exception(".:!Commento inserito correttamente!:.");
}
}
}
}
?>