<?

function users_cstr_role($myroletype) {
  if ($myroletype==1) {
    return "Standard User"; 
  } else if ($myroletype==2) {
    return "System Administrator"; 
  } else {
    return "Unknown";
  }
}

/*
function users_cstr_role($myroletype) {
  if ($myroletype==1) {
    return "Executive"; 
  } else if ($myroletype==2) {
    return "Senior Executive"; 
  } else if ($myroletype==3) {
    return "Head of Department"; 
  } else if ($myroletype==4) {
    return "Head of Division"; 
  } else if ($myroletype==5) {
    return "COO"; 
  } else if ($myroletype==6) {
    return "System Administrator"; 
  } else {
    return "Unknown";
  }
}
*/

function users_getfullname($myuserpkey) {
  global $db;
  $myquery="Select userfullname from general_users where userpkey='".$myuserpkey."'"; 
  $myresult=&$db->query($myquery);
  $mycount=$myresult->numRows();
  if ($mycount!=0) {
    while($myrow=$myresult->fetchRow()) {
      return $myrow['userfullname'];
    }
  } else {
    return "";
  }
}

// note: later when add profile, ask user if want to create user for it!!!
// TODO: insert to cbox table!!!

function users_add($usergrouppkey, $username, $userpassword, $userpasswordquestion, $userpasswordanswer, $userfullname, $useralternateemail, $userrole, $useraccesslevelpkey, $useravatar, $userbosspkey, $userstatus)
{
  // validate..
  if (strpbrk($username, '\\/:*?"<>|')!=FALSE) {
    $e=error_n_goto(-1,"Ralat: Nama Pengguna tidak boleh mengandungi simbol-simbol berikut: \\\/:*?\"<>|");
    echo $e;
    return;
  }
  // convert params..
  $userpkey=md5(microtime());
  $realpassword=md5(strtolower($userpassword));
  $realanswer=md5(strtolower($userpasswordanswer));
  $creationdate=gettimestamp();
  // update user..
  $sa = add_data("Profil [Tambah Pengguna]", "general_users", "userpkey, usergrouppkey, username, userpassword, userpasswordquestion, userpasswordanswer, userfullname, useralternateemail, userrole, useraccesslevelpkey, useravatar, userbosspkey, userstatus, usercreationdate, usercreatedby, userupdatedby",
                 "'".$userpkey."','".$usergrouppkey."','".$username."','".$realpassword.
                 "','".$userpasswordquestion."','".$realanswer.
                 "','".$userfullname."','".$useralternateemail."',".$userrole.",'".$useraccesslevelpkey.
                 "','".$useravatar."','".$userbosspkey."',".$userstatus.",'".$creationdate.
                 "','".$_SESSION['Xuserpkey']."','".$_SESSION['Xuserpkey']."'");
  // 3/2/08 - add profile..  
  $pa = profile_add_noredirect($userfullname, "", "", "", "", "", "", "", "", "", "", "", "", $useralternateemail, "", "", "", "", "", "", "", "", "", $usergrouppkey, "", "", $userpkey);
  // 28/2/08 - add cbox record..
  $ba = cbox_adduser($userpkey);
  // 3/3/08 - add user folder..
  $fa = folder_adduserfolder($username,$userpkey);
  // return result..
  if ($sa==1) {
    $m=msg_n_goto("index.php?module=admin&task=listusers","Profil Pengguna Berjaya Ditambah!");
    echo $m;
  } else {
    $e=error_n_goto("index.php?module=admin&task=listusers","Ralat: Profil Pengguna Gagal Ditambah!");
    echo $e;
  }
}

function users_edit($userpkey, $usergrouppkey, $username, $userfullname, $useralternateemail, $userrole, $useraccesslevelpkey, $useravatar, $userbosspkey, $userstatus)
{
  // convert params..
  // other
  $myoldname=users_getusername($userpkey);
  // update user..
  $sa = update_data("Profil [Pinda Pengguna]", "general_users", "usergrouppkey = '".$usergrouppkey.
                      "', username = '".$username.
                      "', userfullname = '".$userfullname.
                      "', useralternateemail = '".$useralternateemail.
                      "', userrole = ".$userrole.
                      ", useraccesslevelpkey = '".$useraccesslevelpkey.
                      "', useravatar = '".$useravatar.
                      "', userbosspkey = '".$userbosspkey.
                      "', userstatus = ".$userstatus.
                      ", userupdatedby = '".$_SESSION['Xuserpkey']."'",
                      "where userpkey = '".$userpkey."'");
  // 3/3/08 - rename user folder..
  $fe = folder_edituserfolder($username,$myoldname,$userpkey);
  // return result..
  if ($sa==1) {
    $m=msg_n_goto("index.php?module=admin&task=listusers","Profil Pengguna Berjaya Dipinda!");
    echo $m;
  } else {
    $e=error_n_goto("index.php?module=admin&task=listusers","Ralat: Profil Pengguna Gagal Dipinda!");
    echo $e;
  }
}

function users_chgpwd($currentpwd, $newpwd, $confirmpwd) {
  global $db;	
  if (strlen($newpwd)<3) {
    $e=error_n_goto("-1","Error: Password must be at least 3 characters long!");
    echo $e;
  } else if ($newpwd!=$confirmpwd) {
    $e=error_n_goto("-1","Error: New Passwords do not match!");
    echo $e;
  } else {
    $userpkey = $_SESSION['Xuserpkey'];
    $currentpassword=md5(strtolower($currentpwd));		
    $finalpassword=md5(strtolower($newpwd));
    $myquery="Select * from general_users"; 
    $myresult =& $db->query($myquery);
    $mycount=$myresult->numRows();
    $userfound=0;
    if ($mycount!=0) {
      while($myrow=$myresult->fetchRow()) {
        if (($myrow['user_pkey']==$userpkey)&&($myrow['user_password']==$currentpassword)) {
          $userfound++;
        }
      }
    } 
    if ($userfound!=0) {
      $updateresult=update_data("Profile [Change Password]", "general_users","user_password='".$finalpassword."'","where user_pkey='".$userpkey."'");
      if ($updateresult==1) {
        $m=msg_n_goto("index.php","Password changed successfully!");
        echo $m;
      }
    } else {
      $e=error_n_goto("-1","Error: Current Password entered is not valid!");
      echo $e;
    }
  }
}

function users_resetpwd($userpkey)
{
  // get username..
  global $db;
  $mynewpwd="";
  $myquery="Select username from general_users where user_pkey='".$userpkey."'"; 
  $myresult=&$db->query($myquery);
  $mycount=$myresult->numRows();
  if ($mycount!=0) {
    while($myrow=$myresult->fetchRow()) {
      $mynewpwd=$myrow['user_name'];
    }
    users_setpwd($userpkey, $mynewpwd);
  }
  // return result..
  if ($mynewpwd!="") {
    $m=msg_n_goto("index.php?module=user&task=list","Reset Password Successfull!");
    echo $m;
  } else {
    $e=msg_n_goto("index.php?module=user&task=list","Error: Failed to Reset Password!");
    echo $e;
  }
}

function users_setpwd($userpkey, $newpwd) {
  global $db;	
  $finalpassword=md5(strtolower($newpwd));
  $updatepwd=update_data("Profile [Set Password]", "general_users","user_password='".$finalpassword."'","where user_pkey='".$userpkey."'");
}

function users_validpassword($username, $userpassword) {
  global $db;	
  $actualpassword=md5(strtolower($userpassword));		
  $myquery="Select * from general_users"; 
  $myresult =& $db->query($myquery);
  $mycount=$myresult->numRows();
  $userfound=0;
  $userpkey="";
  if ($mycount!=0) {
    while($myrow=$myresult->fetchRow()) {
      if (($myrow['username']==$username)&&($myrow['userpassword']==$actualpassword)) {
        $userpkey=$myrow['userpkey'];
        $userfound++;
      }
    }
  } 
  if ($userfound!=0) {
    $_SESSION['Xtempuserpkey']=$userpkey;
    return true;
  } else {
    return false;
  }
}

function users_validusername($username) {
  global $db;	
  $myquery="Select * from general_users where username='".$username."'"; 
  $myresult =& $db->query($myquery);
  $mycount=$myresult->numRows();
  if ($mycount!=0) {
    while($myrow=$myresult->fetchRow()) {
      $_SESSION['Xtempuserpkey']=$myrow['userpkey'];
      return true;
    }
  } else {
    return false;
  }
}

function users_validanswer($tempuserpkey, $useranswer) {
  global $db;	
  $actualanswer=md5(strtolower($useranswer));		
  $myquery="Select * from general_users"; 
  $myresult =& $db->query($myquery);
  $mycount=$myresult->numRows();
  $userfound=0;
  if ($mycount!=0) {
    while($myrow=$myresult->fetchRow()) {
      if (($myrow['userpkey']==$tempuserpkey)&&($myrow['userpasswordanswer']==$actualanswer)) {
        $setresult=users_setpwd($tempuserpkey,"123456");
        $userfound++;
      }
    }
  } 
  unset($_SESSION['Xtempuserpkey']);
  if ($userfound!=0) {
    return true;
  } else {
    return false;
  }
}

// misc functions..

function users_getusername($userpkey) {
  global $db;	
  $myquery="Select username from general_users where userpkey='".$userpkey."'"; 
  $myresult =& $db->query($myquery);
  $mycount=$myresult->numRows();
  if ($mycount!=0) {
    while($myrow=$myresult->fetchRow()) {      
      return $myrow['username'];
    }
  } else {
    return "";
  }
}

function users_getuserpkey($username) {
  global $db;	
  $myquery="Select userpkey from general_users where username='".$username."'"; 
  $myresult =& $db->query($myquery);
  $mycount=$myresult->numRows();
  if ($mycount!=0) {
    while($myrow=$myresult->fetchRow()) {      
      return $myrow['userpkey'];
    }
  } else {
    return "";
  }
}

?>