<?php
include_once('db_fns.php');

function extract_dbpath($alias)
{
  $lines = file('alias.txt');
  
  $line = $lines[$alias];
  
  $startpos = strpos($line, '[');
  $endpos = strpos($line, ']'); 
  
  return substr($line, $startpos + 1, $endpos - $startpos - 1);   
}

function extract_dbname($alias)
{
  $lines = file('alias.txt');
  
  $line = $lines[$alias];
  
  $startpos = strpos($line, '{');
  $endpos = strpos($line, '}'); 
  
  return substr($line, $startpos + 1, $endpos - $startpos - 1);   
}

function extract_systemtype($alias)
{
  $lines = file('alias.txt');
  
  $line = $lines[$alias];
  
  $startpos = strpos($line, '(');
  $endpos = strpos($line, ')'); 
  
  return substr($line, $startpos + 1, $endpos - $startpos - 1);   
}

function login($username, $password, $alias)
{
  $_SESSION['dbpath']     = extract_dbpath($alias);
  $_SESSION['dbname']     = extract_dbname($alias);
  $_SESSION['systemtype'] = extract_systemtype($alias);
  
  $conn = db_connect();

  if (!$conn)
    return false;
  else
  {
    
    return true;
    //$newpassword = decrypt_password($password);
 
    /*$query = "select count(*) from PROVIDER
                           where PRACNUM='$username'
                            and WEBPWORD = '$password' ";
    $result = ibase_query($conn,$query);
    if (!$result)
      return false;
  
    $row = ibase_fetch_row($result);  

    if($row[0] > 0)
      return true;
    else
      return false;

    ibase_close($conn);*/
  }      
}

function check_logged_in()
{
  return ( check_normal_user() || check_admin_user() );
}

function check_normal_user()
// see if somebody is logged in and notify them if not
{
  if (isset($_SESSION['normal_user']))
    return true;
  else
    return false;
}

function check_admin_user()
// see if somebody is logged in and notify them if not
{
  if (isset($_SESSION['admin_user']))
    return true;
  else
    return false;
}

function decrypt_password($password)
{
  $arrPass = array();
  $newpassword  = '';
  
  for($i =0; $i <strlen($password); $i++)
  {
   $tmpchar = $password[$i];
   $arrPass[$i] = chr(ord($tmpchar) - 10);
   
   if ($arrPass[$i] == ';')
     $arrPass[$i] = '~';
  
   $newpassword = $newpassword.$arrPass[$i];  
  }
  
  return $newpassword;
}

  function load_alias()
  {
    $lines = file('alias.txt');
 
    foreach ($lines as $line_num => $line) 
    {
     $startpos = strpos($line, '[');
     $endpos = strpos($line, ']'); 
       
     echo '<option value="'.$line_num.'">'.substr($line, 0, $startpos);
     echo '</option>';
    }
  }


  function display_login_form($action)
  {
  ?>
    <style>
          #loginheader{top : 150; margin-top:120; }
    </style>
    
    <script>
      $(function() {
         $('#btnLogin').button({ icons: { primary: 'ui-icon-locked'} });
      });
    </script>

   <p></p>
   <center>
    <form method="post" action="index.php?action=<?php echo $action?>">
    <table  border = 0 cellpadding = 6 cellspacing = 0 class="shadetable" >
     <tr>
       <th colspan = 2 bgcolor = '#184214'>
         Please Log In
       </th>
     </tr>
       <td>Practice No.:</td>
       <td><input type="text" name="username" class="roundedit"></td></tr>
     <tr>
       <td>Password:</td>
       <td><input type="password" name="password" class="roundedit"></td></tr>
     <tr>
       <td colspan=2 align="center">
       <button id = "btnLogin">Login</button>

       </td></tr>
     <tr>
   </table></form>
    </center>
  <?php
  }


function check_form_permissions($status, $form)
{
 
    $username = $_SESSION['admin_user'];
    $password  = $_SESSION['password'];

    $newpassword = decrypt_password($password);


    $result = ExecuteQuery("SELECT HFUSERS_KEY 
                                              FROM HFUSERS 
                                              WHERE USERNAME ='$username'
                                               AND USERPASSWORD = '$newpassword'
                                               AND HIDEUSER = 0 ");
    $hfuserkey = $result[0][0];

    $permissions = ExecuteQuery("SELECT COUNT(*) 
                                                FROM USERLINK U
                                                LEFT OUTER JOIN ACCESSFORMS A ON A.ACCESSFORMS_KEY = U.ACCESSFORMS_KEY
                                                WHERE U.HFUSERS_KEY = $hfuserkey AND U.USERRIGHTS LIKE '%$status%';");

    if($permissions[0][0] > 0)
      return true;
    else
      return false;

}

?>
