Skip to content

Instantly share code, notes, and snippets.

@jeslyvarghese
Created September 17, 2011 15:57
Show Gist options
  • Save jeslyvarghese/1224073 to your computer and use it in GitHub Desktop.
Save jeslyvarghese/1224073 to your computer and use it in GitHub Desktop.
Xstasy
<?php
#this library contains elements necessary for answer validation
include_once('connectTo.php');
include_once('../cult/mania.php');
include_once("sanitizer.php");
function standardizeAnswer($answer)
{ $answer = str_replace(" ","",$answer);
$answer = strtolower($answer);
$standardAnswer = ereg_replace("[^A-Za-z0-9]","",$answer);
#do preg replaces for whitespaces, convert to lowercases
return $standardAnswer;
}
function cryptAnswer($qID,$answer)
{
return crypt(crypt($answer,$qID),$qID);
}
function checkAnswer($qID,$answer)
{ $uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "SELECT Answer FROM answers WHERE qID = '$qID'";
$resultset = mysqli_query($cxn,$sql);
endConnection($cxn);
$flag = 0;
while($resultarray = mysqli_fetch_assoc($resultset))
{
if($resultarray['Answer']==$answer)
{
$flag =1;
break;
}
}
if($flag==1)
return 1;
else
return 0;
}
?>
<?php
#This library establishes a connection to the database and returns the handler
function connect($hostname,$username,$password)
{
$connection = mysqli_connect($hostname,$username,$password);
return $connection;
}
function selectDatabase($cxn,$dbname)
{
mysqli_select_db($cxn,$dbname);
}
function endConnection($cxn)
{
mysqli_close($cxn);
}
?>
<?php
#this library manipulates questions table
include_once('connectTo.php');
include_once('../cult/mania.php');
function questionRender($qID)
{ $uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "SELECT innerHTML FROM questions WHERE qid = '$qID'";
$resultset = mysqli_query($cxn,$sql);
$questionArray = mysqli_fetch_assoc($resultset);
endConnection($cxn);
return $questionArray['innerHTML'];
}
function isCracked($qID)
{ $uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "SELECT Cracked FROM questions WHERE qid = '$qID'";
$resultset = mysqli_query($cxn,$sql);
$questionArray = mysqli_fetch_assoc($resultset);
endConnection($cxn);
return $questionArray['Cracked'];
}
function questionCrack($qID,$tkID)
{ $uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "UPDATE questions SET Cracked = 1,tkID = '$tkID' WHERE qid = '$qID'";
mysqli_query($cxn,$sql);
endConnection($cxn);
}
function addQuestion($innerHTML)
{ $uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "INSERT INTO questions(innerHTML,Cracked) VALUES('$innerHTML',0)";
endConnection($cxn);
}
?>
<?php
#This library is to preprocess all users inputs to prevent sql injection
function sanitizeMail($Mail)
{
return ereg_replace("[^A-Za-z0-9\._@]","",$Mail);
}
function sanitizeText($Text)
{
return ereg_replace("[^A-Za-z0-9]","",$Text);
}
?>
<?php
#this file deals with users-session management
include_once('connectTo.php');
include_once('../cult/mania.php');
include_once('userController.php');
function userExists($tkID)
{ $uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "SELECT * FROM userlog WHERE tkID = '$tkID'";
$resultset = mysqli_query($cxn,$sql);
endConnection($cxn);
if(mysqli_num_rows($resultset)>0)
return 1;
else
return 0;
}
function insertUser($tkID,$qID)
{ $uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "INSERT INTO userlog(tkID,CurrentQID) VALUES('$tkID','$qID')";
mysqli_query($cxn,$sql);
$sessionID = rand(999,9999);
$sql = "UPDATE userlog SET sessionID = '$sessionID' WHERE tkID = '$tkID'";
mysqli_query($cxn,$sql);
endConnection($cxn);
insertEntry($tkID,$qID);
updateSessionID($tkID);
}
function updateQuestion($tkID,$qID)
{ $uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "UPDATE userlog SET currentQID = '$qID' WHERE tkID = '$tkID'";
mysqli_query($cxn,$sql);
endConnection($cxn);
}
function getTimeStamp($tkID)
{ $uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "SELECT timeStamp FROM userlog WHERE tkID = '$tkID'";
$resultset = mysqli_query($cxn,$sql);
$questionArray = mysqli_fetch_assoc($resultset);
endConnection($cxn);
return $questionArray['timeStamp'];
}
function getSessionID($tkID)
{ $uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "SELECT sessionID FROM userlog WHERE tkID = '$tkID'";
$resultset = mysqli_query($cxn,$sql);
$questionArray = mysqli_fetch_assoc($resultset);
endConnection($cxn);
return $questionArray['sessionID'];
}
function updateSessionID($tkID)
{ $uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$timeStamp = getTimeStamp($tkID);
$sessionID = crypt($timeStamp&$tkID,rand(999,9999));
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "UPDATE userlog SET sessionID = '$sessionID' WHERE tkID = '$tkID'";
mysqli_query($cxn,$sql);
endConnection($cxn);
}
function getCurrentQuestion($tkID)
{ $uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "SELECT currentQID FROM userlog WHERE tkID = '$tkID'";
$resultset = mysqli_query($cxn,$sql);
$questionArray = mysqli_fetch_assoc($resultset);
endConnection($cxn);
return $questionArray['currentQID'];
}
function resetSession($tkID)
{ $uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "UPDATE userlog SET sessionID = '0' WHERE tkID = '$tkID'";
mysqli_query($cxn,$sql);
endConnection($cxn);
}
?>
<?php
#this file deals with structure table
include_once('connectTo.php');
include_once('../cult/mania.php');
function navigationTopLeft($qID)
{ $uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "SELECT prevLeftQID FROM structure WHERE qid = '$qID'";
$resultset = mysqli_query($cxn,$sql)or die(mysqli_error($cxn));
$questionArray = mysqli_fetch_assoc($resultset);
endConnection($cxn);
return $questionArray['prevLeftQID'];
}
function navigationTopRight($qID)
{ $uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "SELECT prevRightQID FROM structure WHERE qid = '$qID'";
$resultset = mysqli_query($cxn,$sql)or die(mysqli_error($cxn));
$questionArray = mysqli_fetch_assoc($resultset);
endConnection($cxn);
return $questionArray['prevRightQID'];
}
function navigationBottomRight($qID)
{ $uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "SELECT nextRightQID FROM structure WHERE qid = '$qID'";
$resultset = mysqli_query($cxn,$sql)or die(mysqli_error($cxn));
$questionArray = mysqli_fetch_assoc($resultset);
endConnection($cxn);
return $questionArray['nextRightQID'];
}
function navigationBottomLeft($qID)
{ $uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "SELECT nextLeftQID FROM structure WHERE qid = '$qID'";
$resultset = mysqli_query($cxn,$sql)or die(mysqli_error($cxn));
$questionArray = mysqli_fetch_assoc($resultset);
endConnection($cxn);
return $questionArray['nextLeftQID'];
}
function getLevel($qID)
{ $uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "SELECT level FROM structure WHERE qid = '$qID'";
$resultset = mysqli_query($cxn,$sql)or die(mysqli_error($cxn));
$questionArray = mysqli_fetch_assoc($resultset);
endConnection($cxn);
return $questionArray['level'];
}
function checkqIDExist($qID)
{ $uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "SELECT * FROM structure WHERE qid = '$qID'";
$resultset = mysqli_query($cxn,$sql)or die(mysqli_error($cxn));
$questionArray = mysqli_fetch_assoc($resultset);
endConnection($cxn);
if(mysqli_num_rows($resultset)!=0)
return 1;
else
return 0;
}
function insertqID($qID,$topLeft,$topRight)
{ $uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "INSERT INTO structure(qID,nextLeftQID,nextRightQID,prevLeftQID,prevRightQID) VALUES('$qid',0,0,'$topLeft','$topRight')";
mysqli_query($cxn,$sql)or die(mysqli_error($cxn));
endConnection($cxn);
}
function modifyBottom($qID,$bottomLeft,$bottomRight)
{ $uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "UPDATE structure SET nextLeftQID='$bottomLeft',nextRightQID='$bottomRight' WHERE qID='$qID'";
mysqli_query($cxn,$sql)or die(mysqli_error($cxn));
endConnection($cxn);
}
function modifyTop($qID,$bottomLeft,$bottomRight)
{ $uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "UPDATE structure SET prevLeftQID='$bottomLeft',prevRightQID='$bottomRight' WHERE qID='$qID'";
mysqli_query($cxn,$sql)or die(mysqli_error($cxn));
endConnection($cxn);
}
?>
<?php
#this deals with the users table
include_once('connectTo.php');
include_once('../cult/mania.php');
function insertEntry($tkID,$qID)
{ $uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "INSERT INTO users(tkID,qID,Attempts) VALUES('$tkID','$qID',0)";
mysqli_query($cxn,$sql);
endConnection($cxn);
}
function updateEntry($tkID,$qID)
{ $uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "SELECT Attempts FROM users WHERE tkID='$tkID' AND qID = '$qID'";
$resultset = mysqli_query($cxn,$sql)or die(mysqli_error($cxn));
$attempt = mysqli_fetch_assoc($resultset);
$attempt = $attempt['Attempts']+1;
$sql = "UPDATE users SET Attempts = '$attempt' WHERE tkID='$tkID' AND qID='$qID'";
mysqli_query($cxn,$sql)or die(mysqli_error($cxn));
endConnection($cxn);
}
function getQID($tkID)
{ $uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "SELECT qID FROM users WHERE tkID='$tkID'";
$resultset = mysqli_query($cxn,$sql);
while($qID = mysqli_fetch_assoc($resultset))
{
$qset[] = $qID['qID'];
}
endConnection($cxn);
return $qset;
}
function getAttempts($tkID,$qID)
{
$uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "SELECT Attempts FROM users WHERE tkID='$tkID' AND qID = '$qID'";
$resultset = mysqli_query($cxn,$sql);
$resultarray = mysqli_fetch_assoc($resultset);
return $resultarray['Attempts'];
}
function isUserCracked($tkID,$qID)
{ $uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "SELECT Cracked FROM users WHERE tkID='$tkID' AND qID = '$qID'";
$resultset = mysqli_query($cxn,$sql)or die(mysqli_error($cxn));
$questionArray = mysqli_fetch_assoc($resultset);
endConnection($cxn);
return $questionArray ['Cracked'];
}
function Crack($tkID,$qID)
{ $uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "UPDATE users SET Cracked=1 WHERE tkID='$tkID' AND qID='$qID'";
mysqli_query($cxn,$sql)or die(mysqli_error($cxn));
endConnection($cxn);
}
function leaderBoard()
{
$uname = $GLOBALS['dbuname'];
$password = $GLOBALS['dbpwd'];
$hostname = $GLOBALS['hostname'];$dbname=$GLOBALS['dbname'];
$cxn = connect($hostname,$uname,$password);
selectDatabase($cxn,$dbname);
$sql = "SELECT tkID,SUM(Cracked) FROM users GROUP BY tkID ORDER BY SUM(CRACKED) DESC";
$resultset = mysqli_query($cxn,$sql) or die(mysqli_error($cxn));
$index = 0;
while($resultarray = mysqli_fetch_assoc($resultset))
{
$jsonData[$index]['tkID'] = $resultarray['tkID'];
$jsonData[$index++]['Number'] = $resultarray['SUM(Cracked)'];
}
foreach($jsonData as $key=>$Data)
{
for($i=$key+1;$i<count($jsonData);$i++)
{
if($Data['Number']==$jsonData[$i]['Number'])
{ $tkID = $Data['tkID'];
$sql = "SELECT COUNT(tkID) FROM questions WHERE tkID='$tkID'";
$resultset = mysqli_query($cxn,$sql)or die(mysqli_error($cxn));
$resultArray = mysqli_fetch_assoc($resultset);
$dnum = $resultArray['COUNT(tkID)'];
$tkID = $jsonData[$i]['tkID'];
$sql = "SELECT COUNT(tkID) FROM questions WHERE tkID='$tkID'";
$resultset = mysqli_query($cxn,$sql)or die(mysqli_error($cxn));
$resultArray = mysqli_fetch_assoc($resultset);
$jnum = $resultArray['COUNT(tkID)'];
if($jnum>$dnum)
{
$temp = $Data;
$Data = $jsonData[$i];
$jsonData[$i] = $temp;
$jsonData[$key] = $Data;
}
}
}
}
endConnection($cxn);
return json_encode($jsonData);
}
?>
<?php
#this file deals with users permission to acess different questions as well as granting permission upon correct answer
include_once('questionController.php');
include_once('userController.php');
include_once('structureNavigator.php');
include_once('ideabase.php');
function isUserAllowed($qID,$tkID)
{
$questionIDArray = getQID($tkID);
foreach($questionIDArray as $questionID)
{
if($qID==$questionID)
return true;
}
return false;
}
function userLevelUp($qID,$tkID)
{
if(isUserCracked($tkID,$qID)==0)
{
Crack($tkID,$qID);
$left = navigationBottomLeft($qID);
$right = navigationBottomRight($qID);
if($left!=0)
{
insertEntry($tkID,$left);
}
if($right!=0&&($right!=$left))
{
insertEntry($tkID,$right);
}
if(checkIdeadqn($qID))
{
insertIdeaUser($tkID,$qID);
}
}
}
?>
<?php
#this file gives the navigation pane
include_once('structureNavigator.php');
include_once('userPermissions.php');
function getNavigations($tkID,$qID)
{
$NavigationPane['TopLeft'] = (navigationTopLeft($qID)==0)?"NULL":navigationTopLeft($qID);
$NavigationPane['TopRight'] = (navigationTopRight($qID)==0)?"NULL":navigationTopRight($qID);
$NavigationPane['BottomLeft'] = "NULL";
$NavigationPane['BottomRight'] = "NULL";
if(isUserCracked($tkID,$qID)==1)
{
$NavigationPane['BottomLeft'] = navigationBottomLeft($qID);
$NavigationPane['BottomRight'] = navigationBottomRight($qID);
}
return $NavigationPane;
}
function navigateToQuestion($tkID,$qID)
{
if(isUserAllowed($qID,$tkID)==1)
{
updateQuestion($tkID,$qID);
return 1;
}
else
{
return 0;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment