<?php
class Shoutbox {
public $response;
public function Shoutbox($nick = null, $msg = null, $op = null) {
if(!$nick && !$msg && $op == 'tick') {
$this->returnShouts();
}
if($op == 'newShout') {
$this->validateData($nick, $msg);
}
}
public function returnShouts($status = null, $error = null) {
$query = @mysql_query('SELECT * FROM `shouts` ORDER BY `id` DESC LIMIT 0, 10');
$response = '';
if(!$query) {
$response .= '<response>';
$response .= '<error>Database connection problem!</error>';
$response .= '</response>';
} else {
$noShouts = mysql_num_rows($query);
if($noShouts == 0) {
$response .= '<response>';
$response .= '<error>No shouts yet!</error>';
$response .= '</response>';
} else {
$response .= '<response>';
while ($shout = mysql_fetch_object($query)) {
$response .= '<shout>';
$response .= '<nickname>'.$shout->nickname.'</nickname>';
$response .= '<message>'.$shout->message.'</message>';
$response .= '<date>'.$this->showTime($shout->date_added).'</date>';
$response .= '</shout>';
}
$response .= '<status>'.$status.'</status>';
$response .= '<error>'.$error.'</error>';
$response .= '</response>';
}
}
$this->response = $response;
}
private function validateData($nick = null, $msg = null) {
$nick = $this->strFilter($nick);
$msg = $this->strFilter($msg);
if(empty($nick)) {
$this->returnShouts(null, 'Don\'t be shy, enter your nickname.');
return false;
}
elseif(empty($msg)) {
$this->returnShouts(null, 'It doesn\'t seem that you want to shout at all! Write the message.');
return false;
}
else {
$this->addShout($nick, $msg);
}
}
private function strFilter($string) {
$string = str_replace("http://", "", $string);
$string = str_replace("<", "", $string);
$string = str_replace(">", "", $string);
$string = addslashes($string);
return $string;
}
private function showTime($data_comment = null) {
$now = date('Y-m-d H:i:s');
$data_comment = urldecode($data_comment);
$data_comment_data = substr($data_comment, 0, 10);
$data_comment_curenta = substr($now, 0, 10);
$nrsecunde = (strtotime($now) - strtotime($data_comment));
$nrminute = $nrsecunde / (60);
$nrore = $nrminute / (60);
$nroreafisat = intval($nrore);
$nrminuteafisat = intval($nrminute - ($nroreafisat * 60));
$nrsecundeafisat = intval($nrsecunde - ($nroreafisat * 60 * 60) - ($nrminuteafisat * 60));
if ($nroreafisat == 1 ) {$ore = 'hour';} else {$ore = 'hours';}
if ($nrminuteafisat == 1 ) {$minute = 'minute';} else {$minute = 'minutes';}
if ($nrsecundeafisat == 1 ) {$secunde = 'second';} else {$secunde = 'seconds';}
if($nroreafisat > 0 && $nroreafisat <= 23 && $nrminuteafisat > 0) {
return $nroreafisat.' '.$ore.' '.'and'.' '.$nrminuteafisat.' '.$minute.' '.'ago';
}
elseif($nroreafisat == 0 && $nrminuteafisat > 0) {
return $nrminuteafisat.' '.$minute.' '.'and'.' '.$nrsecundeafisat.' '.$secunde.' '.'ago';
}
elseif($nroreafisat == 0 && $nrminuteafisat == 0) {
return $nrsecundeafisat.' '.$secunde.' '.'ago';
}
elseif ($nroreafisat > 24) {
return $data_comment;
}
}
private function addShout($nickname = null, $message = null) {
$time = date('Y-m-d H:i:s');
$query = mysql_query("INSERT INTO `shouts` (`nickname`, `message`, `date_added`) VALUES ('".$nickname."', '".$message."','".$time."');");
if(!$query) {
$this->returnShouts(null, 'Database connection problem!');
} else {
$this->returnShouts('Shout added successfully');
}
}
}
?>