<?php

// Credentials to access SQL database.
// Server address assumes SQL is running on local machine.
// If database credentials are changed, these must be updated to match.
$server   = "127.0.0.1:3306";
$user     = "username";
$password = "password";
$db       = "marathon";

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

// If you change the below numbers then also change in DB
// format used for names = tablename_columnname
$users_name     = 240;
$users_email    = 240;
$users_password = 48;

// FILE NAMES AND PATHS //
$proj_file_path      = "../Models/";

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

   // check if this user is logged in elsewhere
   $res = mysqli_query($con,"SELECT COUNT(ip) FROM current WHERE id = {$userid}");
   $k   = mysqli_fetch_array($res);

   if($k > 0){
     $res = mysqli_query($con,"SELECT ip FROM current WHERE id = {$userid}");
     $row = mysqli_fetch_array($res);
     if($row[0] != $IP){
		$logged_in_elsewhere = $row[0];
	   //echo "You are logged in at computer with IP {$row[0]}. You are now logged out from there";exit;
     }
   }

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

   return $logged_in_elsewhere;
 }

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

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

function is_unique_str($con,$val,$col,$tab){
	$res = mysqli_query($con,"SELECT COUNT({$col}) FROM {$tab} WHERE $col = '{$val}'");
	$row = mysqli_fetch_array($res);
	if($row[0] > 0){
		return false;
	}else{
		return true;
	}
}

function get_res_string1($n){
	switch($n){
		case "0":
			return "pending";
		case "1":
			return "running";
		case "2":
			return "suspended";
		case "3":
			return "passed";
		case "4":
			return "abandoned";
		case "5":
			return "unused";
		case "6":
			return "unused";
		case "7":
			return "cancelled";
		case "8":
			return "cancelled";
		case "9":
			return "error";
		case "11":
			return "12% completed";
		case "12":
			return "27% completed";
		case "13":
			return "36% completed";
		case "14":
			return "43% completed";
		case "15":
			return "54% completed";
		case "16":
			return "65% completed";
		case "17":
			return "70% completed";
		case "18":
			return "82% completed";
		case "19":
			return "95% completed";
	}
  return "pending"; // for results.result = NULL i.e. not submitted to simulation server
}

function get_res_string2($con,$n,$r){
/*
  $res = mysqli_query($con,"SELECT requested_at,portal_result_id FROM results WHERE id = {$r}");
  $row = mysqli_fetch_array($res);
  $prid = $row['portal_result_id'];
  if(is_string($prid) && $prid != ""){
    $saveYN="<a href='#' onclick='echoprid(\"{$prid}\")' class='grey-text'>saved</a>";
  }else{
    $saveYN="<a href='Pages/save_to_portal.php?rid={$r}' class='grey-text'>save</a>";
  }
*/

	switch($n){
		case "0":
			return "<a href='Pages/submit.php?id={$r}&cancel=Y' class='grey-text'>cancel</a>";
		case "1":
			return "<a href='Pages/submit.php?id={$r}&cancel=Y' class='grey-text'>cancel</a>";
		case "2":
      $res = mysqli_query($con,"SELECT model FROM results WHERE id = {$r}");
      while($row = mysqli_fetch_array($res))$model=$row[0];
			return "<a href='Results/{$r}/feedback.pdf' class='grey-text'>feedback</a>
              <a href='Results/{$r}/res.tar.gz' class='grey-text'>detailed results</a>
              <a href='Pages/edit_estate_or_model.php?id={$model}&editing=model&submittedpreviously={$r}' class='grey-text'>revise model</a>
              <a href='Pages/submit.php?id={$r}&abandon=Y' class='grey-text'>abandon</a>";
    case "3":      
      $res = mysqli_query($con,"SELECT presets.is_custom FROM results INNER JOIN presets ON results.preset=presets.id WHERE results.id = {$r}");
      $row = mysqli_fetch_array($res);
      $is_custom = $row[0];
      if ($is_custom==0) {return "<a href='Results/{$r}/certificate.pdf' class='grey-text'>certificate</a>";}
      else {return "";}        
		case "4":
      return "<a href='Results/{$r}/feedback.pdf' class='grey-text'>feedback</a>
              <a href='Results/{$r}/res.tar.gz' class='grey-text'>detailed results</a>";
		case "5":
      return "";
		case "6":
      return "";
		case "7":
			return "";
		case "8":
			return "";
		case "9":
			return "";
		case "11":
			return "<a href='Pages/submit.php?id={$r}&cancel=Y' class='grey-text'>cancel</a>";
		case "12":
			return "<a href='Pages/submit.php?id={$r}&cancel=Y' class='grey-text'>cancel</a>";
		case "13":
			return "<a href='Pages/submit.php?id={$r}&cancel=Y' class='grey-text'>cancel</a>";
		case "14":
			return "<a href='Pages/submit.php?id={$r}&cancel=Y' class='grey-text'>cancel</a>";
		case "15":
			return "<a href='Pages/submit.php?id={$r}&cancel=Y' class='grey-text'>cancel</a>";
		case "16":
			return "<a href='Pages/submit.php?id={$r}&cancel=Y' class='grey-text'>cancel</a>";
		case "17":
			return "<a href='Pages/submit.php?id={$r}&cancel=Y' class='grey-text'>cancel</a>";
		case "18":
			return "<a href='Pages/submit.php?id={$r}&cancel=Y' class='grey-text'>cancel</a>";
		case "19":
			return "<a href='Pages/submit.php?id={$r}&cancel=Y' class='grey-text'>cancel</a>";
	}
}

function get_res_string3($con,$n,$r){
  $res = mysqli_query($con,"SELECT requested_at,portal_result_id FROM results WHERE id = {$r}");
  $row = mysqli_fetch_array($res);
  return "requested @ {$row['requested_at']}";
}

function get_string_between($string, $start, $end){
    $string = ' ' . $string;
    $ini = strpos($string, $start);
    if ($ini == 0) return '';
    $ini += strlen($start);
    $len = strpos($string, $end, $ini) - $ini;
    return substr($string, $ini, $len);
}
?>
