<?php

  if (!isset($_POST['login']) && isset($_GET['login']))
  {
    $_POST['login']=$_GET['login'];
    header('Location: '.$_SERVER['HTTP_REFERER']);
  }

  include("html_functions.php");

  if (isset($_POST['redirect_to'])) {$redirectURL=$_POST['redirect_to'];}
  else {$redirectURL="";}
  if (!check_local($redirectURL)) {$redirectURL="";}

  ################## PROCESSING #########################
  if (isset($_GET['logout'])&&($_GET['logout']==1))
  {
	  clear_session();
  }

  if (isset($_POST['login']))
  {
      if (!isset($_POST['actAs'])) {$_POST['actAs']="";}
	  $loginSuccess=session_login($_POST['username'],$_POST['password'],"",$_POST['actAs']);
	  if (($loginSuccess==1)&&($redirectURL!=""))
	  {
		  header("Location: ".$redirectURL);
		  exit;
	  }
  }

  ################## PAGE INITIALIZATION #########################
  # Title Bar
  $title='StatsLab Login';

  if (!isset($_SESSION['user_id'])) {page_head($title,$_SESSION['lgpath'],"","login.password");}  # Not logged in
  else {page_head($title,$_SESSION['lgpath']);}

  echo " <div id='topbar'>\n";

  main_titlebar($_SESSION['lgid'],$_SESSION['lgpath'],$title);

  echo " </div>   <!-- END topbar DIV -->\n";

  echo " <div id='contentpane'>\n";

  ################## START OF PAGE CONTENT #########################
  include($_SESSION['adminpath']."/dbopen.php");

  if (!isset($_SESSION['user_id']))  # Not logged in
  {
	  echo " <div class='textbox'>\n";
	  echo "  <table cellpadding=2 cellspacing=0>\n";
	  echo "  <form method='post' action='login.php' name='login'>\n";
	  echo "   <input type='hidden' name='login' value='login' />";

	  if (isset($_GET['showq'])&&($_GET['showq']==1)) {echo "   <input type='hidden' name='showq' value='1' />";}

	  if (check_local($_SERVER['HTTP_REFERER'])) {echo "   <input type='hidden' name='redirect_to' value=\"".$_SERVER['HTTP_REFERER']."\" />";}

	  echo "   <tr class='title'><td colspan=2>Login<input type='hidden' name='login' value='login' /></td></tr>\n";
	  
	  if (isset($_SESSION['loginerror']))
	  {
	  	  echo "   <tr><td class='error' colspan=2>".$_SESSION['loginerror']."</td></tr>\n";
		  unset($_SESSION['loginerror']);
	  }

	  echo "   <tr>\n";
	  echo "    <td><label for='username'>Username: </label></td>\n";
	  echo "    <td>\n";

	  echo "     <select name='username'>\n";

	  # Get Leagues
	  $leagues[9999]="Unaffiliated";

	  $query="SELECT league_id,name FROM leagues";

	  if ($_GET['showq']==1) {echo "$query<br/>\n";}

	  $result=mysqli_query($db,$query);

	  while ($row=mysqli_fetch_array($result))

	  {

		  $league_id=$row['league_id'];

		  $leagues[$league_id]=$row['name'];

	  }



	  # Check user login setting

	  $query="SELECT value FROM ootp_sql_admin WHERE setting='showAllUsers'";

	  if ($_GET['showq']==1) {echo "$query<br/>\n";}

	  $result=mysqli_query($db,$query);

	  $row=mysqli_fetch_array($result);

	  $settings['showAllUsers']=$row['value'];



	  # Get dedicated Commishes

	  $query="SELECT identifier as human_manager_id FROM ootp_sql_admin WHERE setting='commish' AND value=1";

	  if ($_GET['showq']==1) {echo "$query<br/>\n";}

	  $result=mysqli_query($db,$query);

	  $rowCnt=0;
	  $hmCommishList="";
	  while ($row=mysqli_fetch_array($result))
	  {
		  if ($rowCnt>0) {$hmCommishList=$hmCommishList.",";}
		  $hmCommishList=$hmCommishList.$row['human_manager_id'];
		  $rowCnt++;
	  }

	  if ($hmCommishList=="") {$hmCommishList=0;}

      # Get Users

	  $query="SELECT hm.*,t.name,t.nickname FROM teams as t RIGHT JOIN (SELECT human_manager_id,is_commish,first_name,last_name,team_id,if(league_id=0,9999,league_id) as league_id FROM human_managers";

	  if ($settings['showAllUsers']!=1) {$query.=" WHERE is_commish=1 OR team_id>0 OR organization_id>0 OR human_manager_id IN ($hmCommishList)";}

	  $query.=") as hm ON t.team_id=hm.team_id ORDER BY league_id,last_name,first_name";

	  if ($_GET['showq']==1) {echo "$query<br/>\n";}

	  $result=mysqli_query($db,$query);

	  $lastLgID=-1;

	  while ($row=mysqli_fetch_array($result))

	  {

		  if ($row['team_id']>0) {$tname=$row['name']." ".$row['nickname'];}

		  #elseif ($row['is_commish']==1) {$tname="Commissioner";}

		  else {$tname="No team";}



		  $league_id=$row['league_id'];

		  if ($lastLgID!=$league_id) {echo "      <optgroup label='".$leagues[$league_id]."'>\n";}



		  echo "      <option value='".$row['human_manager_id']."'";
          if ($row['human_manager_id']==$_COOKIE['human_manager_id']) {echo " selected";}
          echo ">";

		  echo $row['first_name']." ".$row['last_name']." ($tname)";

		  echo "</option>\n";



		  $lastLgID=$league_id;

	  }

	  echo "     </select>\n";

	  echo "    </td>\n";

	  echo "   </tr>\n";

     	  echo "   <tr>\n";

	  echo "    <td><label for='password'>Password: </label></td>\n";

     	  echo "    <td><input type='password' id='password' name='password' /></td>\n";

	  echo "   </tr>\n";

     	  echo "   <tr>\n";

	  echo "    <td colspan=2><input type='submit' value='Login' /></td>\n";

     	  echo "   </tr>\n";

	  echo "  </table>\n";

     	  echo "  </form>\n";

	  echo " </div>\n";

  }

  else  # Logged in

  {

	  # Get User Settings

	  $query="SELECT setting,value FROM ootp_sql_admin WHERE module='users' AND identifier=".$_SESSION['user_id'];

	  if ($_GET['showq']==1) {echo "$query<br/>\n";}

	  $result=mysqli_query($db,$query);

	  if (mysqli_num_rows($result))

	  {

		  while ($row=mysqli_fetch_array($result))

		  {

			  $setting=$row['setting'];

			  $userSettings[$setting]=stripslashes($row['value']);

		  }

	  }

	  if (!isset($userSettings['hideinfo'])) {$userSettings['hideinfo']=1;}

     

	  ##### Contact Information & User Settings #####

     	  echo "     <div class='textbox' style='width:400px;'>\n";

	  echo "      <table cellpadding=2 cellspacing=0 border=0 style='width:100%;'>\n";

	  echo "       <form action='save_settings.php' method='post'>\n";

	  echo "        <input type='hidden' name='module' value='users'>\n";

	  echo "        <input type='hidden' name='userID' value='".$_SESSION['user_id']."'>\n";

	  echo "       <tr class='title'><td colspan=2>Contact Information</td></tr>\n";

     	  echo "       <tr><td><b>Email:</b></td><td><input type='text' name='email' value=\"".$userSettings['email']."\" /></td></tr>\n";

	  echo "       <tr><td><b>Forum Name:</b></td><td><input type='text' name='fname' value=\"".$userSettings['fname']."\" /></td></tr>\n";

     	  echo "       <tr><td><b>Forum PM Link:</b></td><td><input type='text' name='flink' value=\"".$userSettings['flink']."\" /></td></tr>\n";

	  echo "       <tr><td><b>AIM:</b></td><td><input type='text' name='aim' value=\"".$userSettings['aim']."\" /></td></tr>\n";

     	  echo "       <tr><td><b>Yahoo IM:</b></td><td><input type='text' name='yim' value=\"".$userSettings['yim']."\" /></td></tr>\n";

	  echo "       <tr><td><b>MSN:</b></td><td><input type='text' name='msn' value=\"".$userSettings['msn']."\" /></td></tr>\n";

	  echo "       <tr><td><b>ICQ:</b></td><td><input type='text' name='icq' value=\"".$userSettings['icq']."\" /></td></tr>\n";

     	  echo "       <tr><td><b>Hide from guests:</b></td><td><input type='checkbox' name='hideinfo' value=1 ";

	  if ($userSettings['hideinfo']==1) {echo " checked";}

     	  echo "/></td></tr>\n";

	  echo "       <tr class='headline'><td colspan=2>Miscellaneous Settings</td></tr>\n";

     	  echo "       <tr><td><b>Hide Scores by Default:</b></td><td><input type='checkbox' name='hideScores' value=1 title='Boxscores and Playoffs pages will not show results until turned on' ";

	  if ($userSettings['hideScores']==1) {echo " checked";}

     	  echo "/></td></tr>\n";

	  echo "       <tr class='headline'><td colspan=2>Social Media & Web</td></tr>\n";

	  echo "       <tr><td><b>Facebook:</b></td><td><input type='text' name='facebook' value=\"".$userSettings['facebook']."\" /></td></tr>\n";

     	  echo "       <tr><td><b>Twitter:</b></td><td><input type='text' name='twitter' value=\"".$userSettings['twitter']."\" /></td></tr>\n";

	  echo "       <tr><td><b>External URL:</b></td><td><input type='text' name='extlink' value=\"".$userSettings['extlink']."\" /></td></tr>\n";

	  echo "       <tr class='headline'><td colspan=2>Subscriptions</td></tr>\n";

	  echo "       <tr><td colspan=2 style='font-style:italic;'>Sent to the email address above</td></tr>\n";

     	  echo "       <tr><td><b>StatsLab Load:</b></td><td><input type='checkbox' name='slUpdateSubscribe' value=1 ";

	  if ($userSettings['slUpdateSubscribe']==1) {echo " checked";}

     	  echo "/></td></tr>\n";

     	  echo "       <tr><td><b>Draft Updates:</b></td><td><input type='checkbox' name='draftSubscribe' value=1 ";

	  if ($userSettings['draftSubscribe']==1) {echo " checked";}

     	  echo "/></td></tr>\n";

	  echo "       <tr><td colspan=2><input type='submit' value='Save' /></td></tr>\n";

     	  echo "       </form>\n";

	  echo "      </table>\n";

     	  echo "     </div>\n";



	  ##### User Information #####

	  echo "     <div class='textbox' style='float:right;clear:right;width:400px;'>\n";

     	  echo "      <table cellpadding=2 cellspacing=0 border=0 style='width:100%;'>\n";

	  echo "       <tr class='title'><td colspan=2>Your User Settings</td></tr>\n";

     	  if (isset($_SESSION['error']))

	  {

	  	  echo "       <tr><td class='error' colspan=2>".$_SESSION['error']."</td></tr>\n";

		  unset($_SESSION['error']);

    	  }

     

	  echo "       <tr><td colspan=2>You are logged in as:</td></tr>\n";

	  $settingArray=Array('user_id','username','user_league_id','user_team_id','user_is_commish');

	  foreach ($settingArray as $key => $value)

    	  {

	  	  echo "       <tr><td>".$value.":</td><td>".$_SESSION[$value]."</td></tr>\n";

	  }

	  if ($userSettings['commish']==1)

	  {

	  	  echo "       <tr><td colspan=2>StatsLab Commish Access Enabled</td></tr>\n";

	  }



	  echo "      </table>\n";

     	  echo "     </div>\n";



	  ##### Password Change Form #####

	  echo "     <div class='textbox' style='float:right;clear:right;width:400px;'>\n";

     	  echo "  <form method='post' action='update_password.php'>\n";

	  echo "   <table cellpadding=2 cellspacing=0 style='width:100%;'>\n";

	  echo "      <tr class='title'><td colspan=2>Password Update\n";

	  echo "       <input type='hidden' name='user_id' value='".$_SESSION['user_id']."' />\n";

	  if ($_GET['showq']==1) {echo "       <input type='hidden' name='showq' value='".$_GET['showq']."' />\n";}

	  echo "      </td></tr>\n";

	  if (isset($_SESSION['loginerror']))

	  {

		  echo "       <tr><td colspan=2>".$_SESSION['loginerror']."</td></tr>\n";

		  unset($_SESSION['loginerror']);

	  }

     	  echo "       <tr><td>Old Password:</td><td><input type='password' name='old_password' /></td></tr>\n";

	  echo "       <tr><td>New Password:</td><td><input type='password' name='new_password' /></td></tr>\n";

     	  echo "       <tr><td>Confirm New Password:</td><td><input type='password' name='new_password1' /></td></tr>\n";

	  echo "       <tr><td>&nbsp;</td><td><input type='submit' value='Change Password' /></td></tr>\n";

     	  echo "      </form>\n";

	  echo "      </table>\n";

     	  echo "     </div>\n";

  }



  mysqli_close($db);



  ################## END OF PAGE #########################

  echo " </div>   <!-- END contentpane DIV -->\n";

  page_foot($_SESSION['lgpath']);





?>

