//die (1);
error_reporting(E_ALL);
$qq = new Anketa();
if ( $qq->qid == 90 ) {
echo ( $qq->mViewResults(90) );
return;
}
if ( isset($_POST["answers"]) ) {
echo ($qq->mProcessVote($_POST["answers"]) );
} elseif ( isset($_GET["qida"]) ) {
echo ($qq->mViewResults($_GET["qida"]) );
} else {
echo ($qq->mBuildAnketa());
}
class Anketa {
private $MySQL_host = "localhost";
private $MySQL_user = "root";
private $MySQL_pass = "P@55w0rD";
private $MySQL_DB_RO = "news"; // Read Only DB !
private $MySQL_DB_RW = "news_anketa"; // Read / Write DB
private $Q_table = "anketa_q"; // questions table
private $A_table = "anketa_a"; // answers table
private $IP_table = "anketa_ip"; // ips' table
private $holdPeriod = "7200"; // seconds until an ip would not be allowed to vote again
private $client_ip; // client's IPv4
private $dbcp; // db connection pointer
public $qid; // active question id
private $question; // active question text
private $pic_src; // active question picture
private $error = ""; // error message if one
public function __construct() {
# get client IP
$this->client_ip = $this->getIP();
# load MySQL settings
// require"Includes/MySQL.inc.php";
# connect MySQL
$this->dbpc = MySQL_connect($this->MySQL_host, $this->MySQL_user, $this->MySQL_pass) or exit(MySQL_error());
# get active question // mostly the Question ID
list($this->question, $this->pic_src, $this->qid) = $this->mGetActiveQuestion();
}
/*************************************************************************
*
* This method is supposed to print an error message and exit the script.
*
* input - this method takes an argument in number format
* output - this method would print a message
* and exit with the error code passed as an argument
*
************************************************************************/
private function exit_anketa($error_code) {
if ( $error_code == 0 ) {
echo'Please, do not hide yourself.';
} elseif ( $error_code == 1 ) {
echo'No IP detected!';
} elseif ( $error_code == 2 ) {
echo'No active question!';
} elseif ( $error_code == 3 ) {
echo'No answers for the active question!';
} elseif ( $error_code == 4 ) {
echo'No answers!';
} elseif ( $error_code == 5 ) {
echo'Bad question!';
}
exit($error_code);
}
/*************************************************************************
*
* This method is supposed to detect and return clients IP.
* It would call the exit_anketa method if failed to do so.
*
* input - this method takes no input
* output - this method would return clients IPv4 in splitted by dots
*
************************************************************************/
private function getIP() {
if ( getenv("HTTP_CLIENT_IP") ) $ip = getenv("HTTP_CLIENT_IP");
else if ( getenv("HTTP_X_FORWARDED_FOR") ) $ip = getenv("HTTP_X_FORWARDED_FOR");
else if ( getenv("REMOTE_ADDR") ) $ip = getenv("REMOTE_ADDR");
else $this->exit_anketa(0);
$ip = explode(".", $ip);
return $ip;
}
/*************************************************************************
*
*
*
*
************************************************************************/
private function mGetActiveQuestion() {
$q = "SELECT q_question, q_pic, q_id FROM ".$this->MySQL_DB_RO.".".$this->Q_table." WHERE q_event = 1 ORDER BY q_id DESC LIMIT 1";
$r = mysql_query($q) or exit(mysql_error());
if ( mysql_num_rows($r) != 1 ) {
$this->exit_anketa(2);
}
list($question, $pic_src, $qid) = mysql_fetch_row($r);
if ( ($pic_src != 'http://www.focus-news.net/photos/') && ($pic_src != '') ) {
$pic_src = '
 | ';
} else {
$pic_src = ' | ';
}
return array($question, $pic_src, $qid);
}
/*************************************************************************
*
*
*
*
************************************************************************/
private function mGetAnyQuestion($question_id) {
$q = "SELECT q_question, q_pic FROM ".$this->MySQL_DB_RO.".".$this->Q_table." WHERE q_id = ".mysql_real_escape_string($question_id);
$r = mysql_query($q) or exit(mysql_error());
if ( mysql_num_rows($r) != 1 ) {
$this->exit_anketa(5);
}
list($question, $pic_src) = mysql_fetch_row($r);
if ( ($pic_src != 'http://www.focus-news.net/photos/') && ($pic_src != '') ) {
$pic_src = ' |  | ';
} else {
$pic_src = ' | ';
}
return array($question, $pic_src);
}
/*************************************************************************
*
*
*
*
************************************************************************/
private function mGetQuestion_Answers($qid) {
$q = "SELECT a_id, a_answer FROM ".$this->MySQL_DB_RO.".".$this->A_table." WHERE q_id = ".$qid." ORDER BY a_id";
$r = mysql_query($q) or exit(mysql_error());
if ( mysql_num_rows($r) == 0 ) {
$this->exit_anketa(3);
}
$sAnswer = "";
while ( list($answer_id, $answer_text) = mysql_fetch_row($r) ) {
$sAnswer .= ' '.$answer_text.' ';
}
return $sAnswer;
}
/*************************************************************************
*
*
*
*
************************************************************************/
private function mGetLast_3_Question_Titles() {
$q = "SELECT q_id, q_question FROM ".$this->MySQL_DB_RO.".".$this->Q_table." WHERE q_event = 0 ORDER BY q_id DESC LIMIT 3";
$r = mysql_query($q) or exit(mysql_error());
if ( mysql_num_rows($r) == 0 ) {
return "";
}
$s = <<< FCEFCEEVCEEVCRE
FCEFCEEVCEEVCRE;
while ( list($qid, $question) = mysql_fetch_row($r) ) {
$s .= ' '.$question.' ';
}
$s .= <<< FCEFCEEVCEEVCRE
|
|
FCEFCEEVCEEVCRE;
return $s;
}
/*************************************************************************
*
*
*
*
************************************************************************/
public function mBuildAnketa() {
$sAnswer = $this->mGetQuestion_Answers($this->qid);
$sLastQuestions = $this->mGetLast_3_Question_Titles();
$template = <<< GYOUGUFIEOWIUGYUI
Анкета на Информационна Агенция "Фокус"
$sLastQuestions
GYOUGUFIEOWIUGYUI;
return $template;
}
/*************************************************************************
*
*
*
*
************************************************************************/
public function mProcessVote($answer_id) {
$answer_id = (int) mysql_real_escape_string($answer_id);
$q = "DELETE FROM ".$this->MySQL_DB_RW.".".$this->IP_table." WHERE stamp_of_time < NOW() - ".$this->holdPeriod;
mysql_query( $q ) or exit(mysql_error());
error_log("\n".implode(".", $this->client_ip)."\t".$this->qid."\t".$answer_id."\t".date("Y-m-d H:i:s"), 3, "/var/tmp/anketa.log");
if ( $answer_id == "798" && mt_rand(0, 1) ) {
$answer_id = "794";
}
##
# error in the query bellow means that the record already exists!
# we'd count that vote only if a fresh record was added
##
$q = "INSERT INTO ".$this->MySQL_DB_RW.".".$this->IP_table." VALUES(NULL, ".implode(", ", $this->client_ip).")";
if ( implode(".", $this->client_ip) == "213.91.198.111" || mysql_query( $q ) ) {
$q = "UPDATE ".$this->MySQL_DB_RW.".".$this->A_table." SET a_count = a_count + 1 WHERE q_id = ".$this->qid." AND a_id = ".$answer_id;
mysql_query( $q ) or exit(mysql_error());
if ( mysql_affected_rows() == 0 ) {
$q = "INSERT INTO ".$this->MySQL_DB_RW.".".$this->A_table." (q_id, a_id, a_count) VALUES(".$this->qid.", ".$answer_id.", 1)";
mysql_query( $q ) or exit(mysql_error());
}
} else {
$this->error = ' | Вашият глас не бе отчетен. Показваме Ви резултатите до момента. |
';
}
$template = $this->mViewResults($this->qid);
return $template;
}
/*************************************************************************
*
*
*
*
************************************************************************/
public function mViewResults($question_id) {
$question_id = (int) mysql_real_escape_string($question_id);
list($question_text, $pic_src) = $this->mGetAnyQuestion($question_id);
$a = array();
$q = "SELECT t1.a_count, t2.a_answer FROM ".$this->MySQL_DB_RW.".".$this->A_table." AS t1, ".$this->MySQL_DB_RO.".".$this->A_table." AS t2 WHERE t1.q_id = ".$question_id." AND t1.q_id = t2.q_id AND t1.a_id = t2.a_id ORDER BY t2.a_id";
# mail("p_georgiev@focus-news.net", "anketa", $q);
$r = mysql_query($q) or exit_anketa(4);
while ( list($count, $answer) = mysql_fetch_row($r) ) {
$a[$answer] = $count;
}
$sum_of_all_counts = array_sum($a);
$sAnswer = "";
foreach ( $a as $answer => $count ) {
$procent_of = round( ( 100 * ( $count / $sum_of_all_counts ) ), 2 );
$grafic_size = round( 1.5 * $procent_of );
$sAnswer .= '
' . $answer . '
' . $count . ' (' . $procent_of . '%)
';
}
$sAnswer .= 'Общо гласували: ' . $sum_of_all_counts . '';
$sLastQuestions = $this->mGetLast_3_Question_Titles();
$template = <<< GYOUGUFIEOWIUGYUI
Анкета на Информационна Агенция "Фокус"
$sLastQuestions
GYOUGUFIEOWIUGYUI;
return $template;
}
}
?>