require './php/mysql.php';
#require './php/access.php';
$db=0;
$db=oc();
$mycookie='_ziToolkitvis'; # echo getenv("HTTP_COOKIE");
$fuserID=get_cookie($mycookie); $specialjs='';
$setUID=(isset($_GET['u']))?$_GET['u']:'';$setT=(isset($_GET['t']))?$_GET['t']:'';
$fuserID=($setUID)?$setUID:$fuserID; $fuserID=(strlen($fuserID))?$fuserID:0;
$userID=0; $ouserID=0; $tutorialKey=0;
getinwardlogon();
if(preg_match("/^[0-9]+\_[0-9]+$/",$fuserID,$g)){$a=split("_",$fuserID); $userID=$a[0];}
#echo "$userID cc $fuserID";
$ispasschange=(preg_match("/$passchangePageName/",getenv('SCRIPT_NAME')))?1:0;
#echo 'ispasschange '.$ispasschange.'
';
if($ispasschange && (!$userID)){goLog($dbLogURL.'?or='.goHex(getenv("HTTP_REFERER")).'&cp=1');close_conn($db); exit; }
$now=time();
$logout=($userID)?'javascript:logMove(\''.$mycookie.'\',\''.$dbLogURL.'?logout='.$userID.'_3'.'\')':
$dbLogURL.'?or='.goHex(getenv("HTTP_REFERER"));
$logoutText=($userID)?'logout':'login';
$groupID=(isSet($groupID))?$groupID:0; #echo 'gg '.$groupID;
$error=' '; $User_name=$Password='';
$errorspan='ERROR! ';
##### change username and password
if($ispasschange){ $error='';
$isupdate=(isset($_POST{'changepass'}))?1:0;
if($isupdate){
$User_name=(isset($_POST{'User_name'}))?trim($_POST{'User_name'}):'';
$Password=(isset($_POST{'Password'}))?trim($_POST{'Password'}):'';
$User_name=preg_replace("/[^A-Za-z0-9\-\_\.\@]/",'',$User_name);
$Password=preg_replace("/[^A-Za-z0-9\-\_\.\@]/",'',$Password);
if(!(strlen($User_name)>5 && strlen($User_name)<=70)){$error.='
Username must be 6-70 characters long';}
if(!(strlen($Password)>5 && strlen($Password)<=70)){$error.='
Password must be 6-70 characters long';}
$error=($error)?$errorspan.$error.'':'';
### check duplicates
if(!$error){
$crit="User_name = '$User_name'";
$x=get_one($db,'HBadmin_primaryuser','userID',$crit);
if($x && !($x==$userID)){$error='Sorry - Username '.$User_name.' is already in use';}
$error=($error)?$errorspan.$error.'':'';
} # end if(!$error){
if(!$error){
$crit="userID = $userID"; $fldset="User_name = '$User_name',Password = '$Password'";
update_data($db,'HBadmin_primaryuser',$fldset,$crit);
$error="Your Login details have been updated:
Username: $User_name
Password: $Password";
} # end if(!$error){
#$Password=($error)?'':$Password;
} # end if($isupdate){
else{
$crit="userID = $userID";
$User_name=get_one($db,'HBadmin_primaryuser','User_name',$crit);
$Password=get_one($db,'HBadmin_primaryuser','Password',$crit);
}
} ## end if($ispasschange){
### correct urls sent in
$eq=(isset($_GET{'eq'}))?$_GET{'eq'}:''; $or=(isset($_GET{'or'}))?$_GET{'or'}:'';
$or=preg_replace("/\?.*/",'',$or);
$eq=preg_replace("/\?.*/",'',$eq);
$or=(preg_match("/$loginPageName/",$or)||preg_match("/$registerPageName/",$or)||preg_match("/$passchangePageName/",$or)||preg_match("/$passreminderPageName/",$or))?'':$or;
$eq=(preg_match("/$loginPageName/",$eq)||preg_match("/$registerPageName/",$eq)||preg_match("/$passchangePageName/",$eq)||preg_match("/$passreminderPageName/",$or))?'':$eq;
$originalReferer=$or; $lastEntryURL=$eq;
$loginlink=$loginPageName.(($or || $eq)?'?':'').(($eq)?'eq='.goHex($eq):'').(($or && $eq)?'&':'').(($or)?'or='.goHex($or):'');
$registerlink=$registerPageName.(($or || $eq)?'?':'').(($eq)?'eq='.goHex($eq):'').(($or && $eq)?'&':'').(($or)?'or='.goHex($or):'');
### add organisation,owner,tutorial if not present
#### organisation - iT_Organisations ###############################
if(isset($organisationID) && $db){
$crit="organisationID = '$organisationID'";
$fldset="dateUpdated = $now";
$flds='orgKey,userID,organisationID,organisationName,Title,Forename,Surname,eMail,dateCreated,dateUpdated';
$vals="'',0,'$organisationID','','','','','',$now,$now";
$orgKey=get_one($db,'iT_Organisations','orgKey',$crit);
if(!$orgKey){
$ret=insert_data($db,'iT_Organisations',$flds,$vals); #echo $ret.'
';
$orgKey=get_one($db,'iT_Organisations','orgKey',$crit);}}
#echo "orgKey $orgKey
";
#### owner - iT_Owners ###############################
if(isset($ownerID) && $db){ $orgKey=($orgKey)?$orgKey:0;
$crit="ownerID = '$ownerID'";
$fldset="dateUpdated = $now";
$flds='ownerKey,userID,orgKey,ownerID,Title,Forename,Surname,eMail,dateCreated,dateUpdated';
$vals="'',0,$orgKey,'$ownerID','','','','',$now,$now";
$ownerKey=get_one($db,'iT_Owners','ownerKey',$crit);
if(!$ownerKey){
$ret=insert_data($db,'iT_Owners',$flds,$vals); #echo $ret.'
';
$ownerKey=get_one($db,'iT_Owners','ownerKey',$crit);}}
#echo "ownerKey $ownerKey
";
#### tutorial - iT_Tutorials ###############################
if(isset($tutorialID) && $db){ $ownerKey=($ownerKey)?$ownerKey:0; $orgKey=($orgKey)?$orgKey:0;
$crit="tutorialID = '$tutorialID'";
$fldset="dateUpdated = $now";
$flds='tutorialKey,ownerKey,tutorialID,tutorialName,tutorialURL,dateCreated,dateUpdated';
$vals="'',$ownerKey,'$tutorialID','','',$now,$now";
$tutorialKey=get_one($db,'iT_Tutorials','tutorialKey',$crit);
if(!$tutorialKey){
$ret=insert_data($db,'iT_Tutorials',$flds,$vals); # echo "$ret".'
';
$tutorialKey=get_one($db,'iT_Tutorials','tutorialKey',$crit);}}
#echo "tutorialKey $tutorialKey
";
#### delete tutorial responses (requires $tutorialKey) #######
$deletemessage='';
$isdelresponses=(isset($_POST{'deleteresponses'}))?1:0;
$delmethod=(isset($_POST{'deleteresponsesoption'}))?$_POST{'deleteresponsesoption'}:'';
if($isdelresponses){
$deletemessage=(preg_match("/all\_tutorials/",$delmethod))?'All your responses for all tutorials now deleted.':'All your responses for this tutorial now deleted.';
$crit="userID = $userID";
$crit.=(preg_match("/all\_tutorials/",$delmethod))?'':" AND tutorialKey = $tutorialKey";
if($userID && $tutorialKey){ delete_data($db,'iT_Responses',$crit);}
#echo $crit;
} # end if($isdelresponses){
##### login #######
if(isset($_POST{'login'})){ $error='';
$User_name=(isset($_POST{'User_name'}))?trim($_POST{'User_name'}):'';
$Password=(isset($_POST{'Password'}))?trim($_POST{'Password'}):'';
if(!strlen($User_name)){$error.='Username';}
if(!strlen($Password)){$error.=(($error)?' and ':'').'Password';}
$error=($error)?$errorspan.'Please supply '.$error.'':'';
$Password=($error)?'':$Password;
if((!$error) && $db){
$crit="User_name = '$User_name' AND Password = '$Password'";
$userID=get_one($db,'HBadmin_primaryuser','userID',$crit);
$userID=isVal($userID)?$userID:0;
if(!$userID){$Password='';$error=$errorspan.'User unknown or password incorrect.';}
#echo $userID.' '.$crit; die;
} # end if(!$error){
if(!$error){
### update user data
$now=time();
$crit="userID = $userID";
$fldset="dateUpdated = $now";
$fldset.=($originalReferer)?",originalReferer = '$originalReferer'":'';
$fldset.=($lastEntryURL)?",lastEntryURL = '$lastEntryURL'":'';
update_data($db,'iT_Tusers',$fldset,$crit);
if(!$lastEntryURL){$lastEntryURL=get_one($db,'iT_Tusers','lastEntryURL',$crit);}
## update logtables
$userType='Tuser';
$userTable='Tuserstable';
$ulIP=getenv('REMOTE_ADDR');
$ulFacts=getenv("HTTP_USER_AGENT").'|'.getenv("HTTP_ACCEPT_LANGUAGE");
$ulFacts=preg_replace("/[\r\n\s]/",'',$ulFacts);$ulFacts=preg_replace("/\'/",'$#39;',$ulFacts);
$ulTime=$now;
$flds=$userlogfields;
$vals="$userID,'$userType',0,'$ulIP','$ulFacts',$ulTime,'$userTable'";
$fldset="ulIP = '$ulIP',ulFacts = '$ulFacts',ulTime = $ulTime";
$crit="userID = $userID AND userType = '$userType' AND userTable = '$userTable'";
$ret=insOrUp($db,'HBfunctions_userlog',$flds,$vals,$fldset,$crit,'userID',1);
# echo('did ulog '.$ret);
## update usage log
$flds=$UsageLogfields;
$vals="'',$userID,$now,0,0,0";
insert_data($db,'HBadmin_UsageLog',$flds,$vals);
# echo('did uulog '.$ret);
#echo "le $lastEntryURL
or $originalReferer";
$lastEntryURL=(isset($goafterlogin))?$goafterlogin:$lastEntryURL;
#$lastEntryURL='';
if($lastEntryURL){
$nloc=$lastEntryURL.((preg_match("/\?/",$lastEntryURL))?'&':'?').'t='.$now.'&u='.$userID.'_3';
#print 'gotin '.$go;
print "\n