<?php
if(count(get_included_files()) == 1)die("Really sweet of you!");

// DEVELOPMENT CREDENTIALS //
$website = "127.0.0.1:3306";
$mysqli_username  = "root";
$pw      = "mysql";
$dbase   = "bpa_admin";
$Version = "1.73";

// MAX ALLOWED ENTITIES //
$max_users = 3000;
$max_sims_per_user = 4;

// FILE NAMES AND PATHS //
$proj_file_path      = "../../../../../Users/las05110/Dropbox/Hit2Gap_job_files/";
$proj_file_path_deep = "../../../../../../Users/las05110/Dropbox/Hit2Gap_job_files/";
$proj_file_path      = "../../../../../models/Hit2Gap_job_files/Calibro";
$proj_file_path_deep = "../../../../../models/Hit2Gap_job_files/Calibro/";
// TO MOVE FROM DROPBOX TO MODELS FOLDER
// set both proj_file_path_deep to the below string and 
// suitably adjust proj_file_path -- test where proj_file_path is used
// The only occurrence of the below variable is in 
// Pages/add_model.php and RESTful/AddModel.php
// search for 'true' in these file and delete
//$proj_file_path_true = "../../../../../models/Hit2Gap_job_files/Calibro";

// FUNCTIONS //
function log_user_in($userid,$con){
  // get ip address of this computer
  $IP = $_SERVER['REMOTE_ADDR'];

  // add this to Current Toble
  mysqli_query($con,"DELETE FROM Current WHERE ip = '{$IP}'");
  mysqli_query($con,"INSERT INTO Current(user_id,ip) VALUES(\"{$userid}\",\"{$IP}\")");

  //echo "<script>window.opener.location.reload();</script>";
}

function get_userid($con){
  $user_id = NULL;

  // get ip address of this computer
  $IP = $_SERVER['REMOTE_ADDR'];
  $res = mysqli_query($con,"SELECT user_id FROM Current WHERE ip = '{$IP}'");
  while($row = mysqli_fetch_array($res))$user_id = $row[0];
  return $user_id;
}

function user_credentials_form($con,$uid){
  $fname  = "";
  $lname  = "";
  $email  = "";
  $star = "";
  if($uid == 0)$star = "*";
  if($uid != 0){
    $res = mysqli_query($con,"SELECT fname,lname,email,auth_code FROM User WHERE user_id = {$uid}");
    while ($row = mysqli_fetch_array($res)){
      $fname  = $row[0];
      $lname  = $row[1];
      $email  = $row[2];
	  $auth   = $row[3];
    }
  }
  echo("
  <table style='margin-left:10px'>");
  if($uid == 0){
    echo("<tr><td></td><td><h5>Sign up</h5></td></tr>");
  }else{
    echo("<tr><td></td><td><h5>Edit profile</h5></td></tr>");
  }

  echo("
    <tr>
      <td>First Name{$star}</td>
      <td><input type='text' pattern='[a-zA-Z][a-zA-Z0-9]*' title='Only alphanumerics allowed with no spaces' name='fname' size='30' maxlength='30' value='{$fname}'></td>
    </tr>
    <tr>
      <td>Last Name{$star}</td>
      <td><input type='text' pattern='[a-zA-Z][a-zA-Z0-9]*' title='Only alphanumerics allowed with no spaces' name='lname' size='30' maxlength='30' value='{$lname}'></td>
    </tr>
    <tr>
      <td>Email{$star}</td>
      <td><input type='text' pattern='[a-zA-Z0-9@._-]*' title='Only alphanumerics @ _ and . allowed' name='email' size='30' maxlength='80' value='{$email}'></td>
    </tr>
  ");
  if($uid == 0){
    echo("
      <tr>
        <td>Enter password*</td>
        <td><input type='password' name='pwd1' size='30' maxlength='30'></td>
      </tr><tr>
        <td>Re-enter password*</td>
        <td><input type='password' name='pwd2' size='30' maxlength='30'></td>
      </tr>
      <tr>
        <td></td>
        <td>
          <span class='weak'>By signing up you agree to <a href='https://www.gnu.org/licenses/gpl-3.0.en.html' target='_blank'><u>terms and conditions</u></a></span>
        </td>
      </tr>
	  </table>
    ");
  }else{
    echo("
      <tr>
        <td>Enter old password*</td>
        <td><input type='password' name='pwd0' size='30' maxlength='30'></td>
      </tr><tr>
      <tr>
        <td>Enter new password</td>
        <td><input type='password' name='pwd1' size='30' maxlength='30'></td>
      </tr><tr>
        <td>Re-enter password</td>
        <td><input type='password' name='pwd2' size='30' maxlength='30'></td>
      </tr>
      </tr><tr>
        <td>Authentication Code</td>
        <td><input type='text' name='auth' size='30' maxlength='30' value='{$auth}'></td>
      </tr>
	  </table>
    ");
  }
}

// Get result code from result string
function get_result_string($prog){
  $prog = trim($prog);

  if($prog == "0"){
    $result = "Submitted";

  }elseif($prog == "1"){
    $result = "Running";

  }elseif($prog == "2"){
    $result = "Failed";

  }elseif($prog == "3"){
    $result = "Compliant";

  }elseif($prog == "4"){
    $result = "Critical_perf_issue";

  }elseif($prog == "5"){
    $result = "Mild_perf_issue";

  }elseif($prog == "6"){
    $result = "Perf_improvement_opp";

  }elseif($prog == "7"){
    $result = "Cancel";

  }elseif($prog == "8"){
    $result = "Cancelled";

  }elseif($prog == "9"){
    $result = "Error";

  }else{
    $result = "-1";
  }
  return $result;
}

function generateRandomString($length) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

function is_unique($con,$code){
	$res = mysqli_query($con,"SELECT COUNT(auth_code) FROM User WHERE auth_code = '{$code}'");
	$row = mysqli_fetch_array($res);
	if($row[0] > 0){
		return false;
	}else{
		return true;
	}
}

?>






















