<?php
/*  
 * SAMS (Squid Account Management System)
 * Author: Dmitry Chemerik chemerik@mail.ru
 * (see the file 'main.php' for license details)
 */

function NotUsersTreeUserAuth()
{
  global $SAMSConf;

     if(isset($_GET["userid"])) $password=$_GET["userid"];
     if(isset($_GET["user"])) $userdomain=$_GET["user"];

  $grauditor=0;
  $SAMSConf->domainusername="";
  if($SAMSConf->AUTH=="adld")
    {
	require_once("adldap.php");
	//create the LDAP connection
	$pdc=array("$SAMSConf->LDAPSERVER");
	$options=array(account_suffix=>"@$SAMSConf->LDAPDOMAIN", base_dn=>"$SAMSConf->LDAPBASEDN",domain_controllers=>$pdc, 
	ad_username=>"$SAMSConf->LDAPUSER",ad_password=>"$SAMSConf->LDAPUSERPASSWD","","","");
	$ldap=new adLDAP($options);
      if ($ldap->authenticate($userdomain,$password))
         {
            $aflag=1;
           $SAMSConf->domainusername=$userdomain;
	 }   
    }
  if($SAMSConf->AUTH=="ntlm")
    {
      
//  $e = escapeshellcmd("$SAMSConf->WBINFOPATH $userdomain $password");
//  $aaa=exec("testwbinfopasswd $e");
echo "<h1>$aaa</h1>";
exit(0);
      $aflag=0;

      $finp=fopen("data/passwd","r");
      if($finp==FALSE)
        {
          echo "can't open file data/passwd<BR>";
          exit(0);
        }
      while(feof($finp)==0)  
         {
           $string=fgets($finp,10000);
           print("$string<BR>\n");
           if(stristr($str1,"succeeded" )!=false||stristr($str1,"NT_STATUS_OK" )!=false)
             { 
                $aflag=1;
	     }  

         }
      fclose($finp);

/*
      $str1=strtok($aaa,"\n");
      if(stristr($str1,"succeeded" )!=false||stristr($str1,"NT_STATUS_OK" )!=false)
        { 
          $aflag=1;
	}  
      for($i=0;$i<10;$i++)
         {
            $str1=strtok("\n");
//echo "$str1<BR>";
            if(stristr($str1,"succeeded" )!=false||stristr($str1,"NT_STATUS_OK" )!=false)
              { 
                $aflag=1;
	      }  

	 }
*/
      if($aflag>0)
        {
           if($SAMSConf->NTLMDOMAIN=="Y")
             {
               //echo "SAMSConf->NTLMDOMAIN==$SAMSConf->NTLMDOMAIN<BR> ";
	       if(strrpos($userdomain,"+" )!=false)
	         {
                   $user=strtok($userdomain,"+");
                   $SAMSConf->domainusername=strtok("+");
		 }
	       if(stristr($userdomain,"\\" )!=false)
	         {
                   $user=strtok($userdomain,"\\");
                   $SAMSConf->domainusername=strtok("\\");
		 }
	       if(stristr($userdomain,"@" )!=false)
	         {
                   $user=strtok($userdomain,"@");
                   $SAMSConf->domainusername=strtok("@");
		 }
	     }
	   else
             $SAMSConf->domainusername=$userdomain;
       
           db_connect($SAMSConf->SAMSDB) or exit();
           mysql_select_db($SAMSConf->SAMSDB);
           $result=mysql_query("SELECT nick,passwd,domain,gauditor,squidusers.group,autherrorc,autherrort FROM squidusers WHERE nick=\"$SAMSConf->domainusername\" ");
           $row=mysql_fetch_array($result);
           $SAMSConf->domainusername="$row[nick]";
           //$gauditor=$row['gauditor'];
       }

 //exit(0);   
    }
  if($SAMSConf->AUTH=="ip"||$SAMSConf->AUTH=="ncsa")
    {
       db_connect($SAMSConf->SAMSDB) or exit();
       mysql_select_db($SAMSConf->SAMSDB);
       $result=mysql_query("SELECT nick,passwd,domain,gauditor,squidusers.group,autherrorc,autherrort,id FROM squidusers WHERE nick=\"$userdomain\"&&passwd=\"$password\" ");
       $row=mysql_fetch_array($result);
       //$gauditor=$row['gauditor'];
       if(strlen($row['nick'])>0||strlen($row['passwd'])>0)
         {
           $SAMSConf->domainusername="$row[nick]";
         }
     }
  $grauditor=0;
  if($row['gauditor']>0&&strlen($SAMSConf->domainusername)>0)
     $grauditor=$row['group'];
     
 //echo "<P>USER = $SAMSConf->domainusername SAMSConf->NTLMDOMAIN==$SAMSConf->NTLMDOMAIN SAMSConf->domainusername=$SAMSConf->domainusername<BR> ";
 //exit(0);    
 return($grauditor);
}
 
function UserAuth()
{
  global $SAMSConf;
  
  if(isset($_GET["usernick"])) $user=$_GET["usernick"];
  if(isset($_GET["userdomain"])) $domain=$_GET["userdomain"];
  if(isset($_GET["userid"])) $password=$_GET["userid"];
  if(isset($_GET["id"])) $id=$_GET["id"];
  $SAMSConf->grauditor=0;
  $SAMSConf->domainusername="";
  $aflag=0;

  if($SAMSConf->AUTH=="ntlm")
    {
      if($SAMSConf->NTLMDOMAIN=="Y")
        {
           $mas=array();
           $mas=$SAMSConf->SEPARATOR;
           $slashe="\\";

           for($j=strlen($SAMSConf->SEPARATOR)-1;$j>0;$j--)
            {
	      if($mas[$j]==$slashe)
	        {
		  $separator="\\";
		} 
	      else
	        {	
		  $separator=$mas[$j];
		}  
              $userdomain="$domain$separator$user";

	$e = escapeshellcmd("$SAMSConf->WBINFOPATH $userdomain $password");
	$aaa=exec("testwbinfopasswd $e");
        	$aflag=0;
		$finp=fopen("data/passwd","r");
		if($finp==FALSE)
	  	  {
			echo "can't open file data/passwd<BR>";
			exit(0);
	  	  }
		while(feof($finp)==0)  
         	  {
			$string=fgets($finp,10000);
			print("$string<BR>\n");
			if(stristr($string,"succeeded" )!=false||stristr($string,"NT_STATUS_OK" )!=false)
		  	  { 
				$aflag=1;
				$SAMSConf->domainusername=$user;
		  	  }  
         	  }
		fclose($finp);
	    
	    } 
           if($aflag==0)
	     {
	       $userdomain="$user";
	$e = escapeshellcmd("$SAMSConf->WBINFOPATH $userdomain $password");
	$aaa=exec("testwbinfopasswd $e");
        	$aflag=0;
		$finp=fopen("data/passwd","r");
		if($finp==FALSE)
	  	  {
			echo "can't open file data/passwd<BR>";
			exit(0);
	  	  }
		while(feof($finp)==0)  
         	  {
			$string=fgets($finp,10000);
			print("$string<BR>\n");
			if(stristr($string,"succeeded" )!=false||stristr($string,"NT_STATUS_OK" )!=false)
		  	  { 
				$aflag=1;
				$SAMSConf->domainusername=$user;
		  	  }  
         	  }
		fclose($finp);
	     }
         }
       else
         {
           $userdomain="$user";
	$e = escapeshellcmd("$SAMSConf->WBINFOPATH $userdomain $password");
	$aaa=exec("testwbinfopasswd $e");
        	$aflag=0;
		$finp=fopen("data/passwd","r");
		if($finp==FALSE)
	  	  {
			echo "can't open file data/passwd<BR>";
			exit(0);
	  	  }
		while(feof($finp)==0)  
         	  {
			$string=fgets($finp,10000);
			print("$string<BR>\n");
			if(stristr($string,"succeeded" )!=false||stristr($string,"NT_STATUS_OK" )!=false)
		  	  { 
				$aflag=1;
				$SAMSConf->domainusername=$user;
		  	  }  
         	  }
		fclose($finp);
          }
	 
       db_connect($SAMSConf->SAMSDB) or exit();
       mysql_select_db($SAMSConf->SAMSDB);
       $result=mysql_query("SELECT nick,passwd,domain,gauditor,squidusers.group,autherrorc,autherrort FROM squidusers WHERE id=\"$id\" ");
       $row=mysql_fetch_array($result);
       $gauditor=$row['gauditor'];
 
// echo"USER = $SAMSConf->domainusername<BR>";
// exit(0);   
    }
  if($SAMSConf->AUTH=="ip"||$SAMSConf->AUTH=="ncsa")
    {
      db_connect($SAMSConf->SAMSDB) or exit();
       mysql_select_db($SAMSConf->SAMSDB);
       
       $result2=mysql_query("SELECT nick,id FROM squidusers WHERE id=\"$id\" ");
       $row2=mysql_fetch_array($result2);
       $passwd=$password;
       $password=crypt($passwd,$row2['nick']);
       //echo "password=$password<BR>";
       
       $result=mysql_query("SELECT nick,passwd,domain,gauditor,squidusers.group,autherrorc,autherrort,id FROM squidusers WHERE id=\"$id\"&&passwd=\"$passwd\" ");
       $row=mysql_fetch_array($result);
       $gauditor=$row['gauditor'];
       if(strlen($row['nick'])>0||strlen($row['passwd'])>0)
         {
           $SAMSConf->domainusername="$row[nick]";
         }
     }
  if($SAMSConf->AUTH=="adld")
    {

	require_once("adldap.php");
	//create the LDAP connection
	$pdc=array("$SAMSConf->LDAPSERVER");
	$options=array(account_suffix=>"@$SAMSConf->LDAPDOMAIN", base_dn=>"$SAMSConf->LDAPBASEDN",domain_controllers=>$pdc, 
	ad_username=>"$SAMSConf->LDAPUSER",ad_password=>"$SAMSConf->LDAPUSERPASSWD","","","");
	$ldap=new adLDAP($options);

        if($ldap==NULL)
          {
             //print("Connection not created!");
             exit(0);
          }
        else
          {
            $result=mysql_query("SELECT nick,passwd,domain,gauditor,squidusers.group,id FROM squidusers WHERE id=\"$id\" ");
            $row=mysql_fetch_array($result);

	    if($ldap -> authenticate( $row['nick'], $password ))
            {
              $SAMSConf->domainusername=$row['nick'];
            }
          }
     }
  $grauditor=0;
  if($row['gauditor']>0&&strlen($SAMSConf->domainusername)>0)
     $grauditor=$row['group'];
     
    
 return($grauditor);
}



function UserAuthForm()
{
  global $SAMSConf;
  
  $lang="./lang/lang.$SAMSConf->LANG";
  require($lang);

  if(isset($_GET["userid"])) $userid=$_GET["userid"];
  
  $result=mysql_query("SELECT * FROM squidusers WHERE id=\"$userid\" ");
  $row=mysql_fetch_array($result);

  PageTop("getpassword.jpg","$usertray_UserAuthForm_1 <FONT COLOR=\"BLUE\">$row[nick]</FONT>");
  print("<P>\n");
  print("<FORM NAME=\"USERPASSWORD\" ACTION=\"main.php\">\n");
  print("<INPUT TYPE=\"HIDDEN\" NAME=\"show\" value=\"exe\">\n");
  print("<INPUT TYPE=\"HIDDEN\" NAME=\"function\" value=\"userauth\">\n");
  print("<INPUT TYPE=\"HIDDEN\" NAME=\"id\" value=\"$row[id]\">\n");
  print("<INPUT TYPE=\"HIDDEN\" NAME=\"usergroup\" value=\"$row[group]\">\n");
  print("<INPUT TYPE=\"HIDDEN\" NAME=\"userdomain\" value=\"$row[domain]\">\n");
  print("<INPUT TYPE=\"HIDDEN\" NAME=\"usernick\" value=\"$row[nick]\">\n");
  print("<INPUT TYPE=\"HIDDEN\" NAME=\"id\" value=\"$row[id]\">\n");
  print("<TABLE WIDTH=\"90%\">\n");
  print("<TR>\n");
  print("<TD><B>login:</B>\n");

  print("<TD><B>$row[nick]\n");
  print("<TR>\n");
  print("<TD><B>password:</B>\n");
  print("<TD><INPUT TYPE=\"PASSWORD\" NAME=\"userid\" SIZE=30> \n");
  print("</TABLE>\n");
  print("<BR><INPUT TYPE=\"SUBMIT\" value=\"Ok\">\n");
  print("</FORM>\n");
}



function UserForm()
{
  global $SAMSConf;
  
  $lang="./lang/lang.$SAMSConf->LANG";
  require($lang);

  if(isset($_GET["userid"])) $userid=$_GET["userid"];

  $result=mysql_query("SELECT * FROM squidusers WHERE id=\"$userid\" ");
  $row=mysql_fetch_array($result);
  $result2=mysql_query("SELECT * FROM groups WHERE name=\"$row[group]\" ");
  $row2=mysql_fetch_array($result2);

  PageTop("user.jpg","$usertray_UserForm_1 <FONT COLOR=\"BLUE\">$row[nick]</FONT>");

  print("<TABLE>\n");
  print("<TR>\n");
  print("<TD>\n");
  print("<B>Nickname:\n");
  print("<TD>\n");
  print("$row[nick]\n");
  if($SAMSConf->NTLMDOMAIN=="Y")
    {
      print("<TR>\n");
      print("<TD>\n");
      print("<B>$usertray_UserForm_2\n");
      print("<TD>\n");
      print("$row[domain]\n");
    }  
  if($SAMSConf->access==2)
    {
      print("<TR>\n");
      print("<TD>\n");
      print("<B>$usertray_UserForm_3:\n");
      print("<TD>\n");
      print("$row[ip]/$row[ipmask]\n");
	}
  print("<TR>\n");
  print("<TD>\n");
  print("<B>$usertray_UserForm_4: \n");
  print("<TD>\n");
  print("$row[name]\n");
  print("<TR>\n");
  print("<TD>\n");
  print("<B>$usertray_UserForm_5: \n");
  print("<TD>\n");
  print("$row[soname]\n");
  print("<TR>\n");
  print("<TD>\n");
  print("<B>$usertray_UserForm_6: \n");
  print("<TD>\n");
  print("$row[family] \n");
  print("<TR>\n");
  print("<TD>\n");

  print("<B>$usertray_UserForm_7: \n");
  print("<TD>\n");
  print("$row2[nick]\n");
  
  if($SAMSConf->access==2||strcasecmp($SAMSConf->domainusername,$row[nick])==0||$SAMSConf->groupauditor==$row[group])
    {
       print("<TR>\n");
       print("<TD>\n");
       print("<B>$usertray_UserForm_8: \n");
       print("<TD>\n");
             
       if($row['quotes']>0)
          print(" $row[quotes] Mb");
       else  
          print(" unlimited ");
//       print("$row[quotes] Mb\n");
       
       print("<TR>\n");
       print("<TD>\n");
       print("<B>$usertray_UserForm_9: \n");
       print("<TD>\n");

       $syea=strftime("%Y");
       $smon=strftime("%m");
       $eday=strftime("%d");

       $sdate="$syea-$smon-1";
       $edate="$syea-$smon-$eday";
       $stime="0:00:00";
       $etime="0:00:00";
       if($SAMSConf->realtraffic=="real")
	     PrintTrafficSize($row['size']-$row['hit']);
       else
	     PrintTrafficSize($row['size']);
    }
  if($SAMSConf->access==2)
    {
       print("<TR>\n");
       print("<TD>\n");
       print("<B>$usertray_UserForm_10:\n");
       print("<TD>\n");
       if($row['enabled']>0)
          print("$usertray_UserForm_13\n");
       else
          print("$usertray_UserForm_11 \n");

       $result3=mysql_query("SELECT * FROM shablons WHERE shablons.name=\"$row[shablon]\" ");
       $row3=mysql_fetch_array($result3);
       print("<TR>\n");
       print("<TD>\n");
       print("<B>$usertray_UserForm_12: \n");
       print("<TD>\n");
       print("$row3[nick]\n");
       print("</TABLE>\n");
    }
}


function UserTray($userid,$usergroup)
{
  global $SAMSConf;
  
  $lang="./lang/lang.$SAMSConf->LANG";
  require($lang);

  db_connect($SAMSConf->SAMSDB) or exit();
  mysql_select_db($SAMSConf->SAMSDB);

  $result=mysql_query("SELECT * FROM squidusers WHERE id=\"$userid\"&&squidusers.group=\"$usergroup\" ");
  $row=mysql_fetch_array($result);

  $result2=mysql_query("SELECT * FROM groups WHERE groups.name=\"$row[group]\" ");
  $row2=mysql_fetch_array($result2);

  print("<SCRIPT>\n");

  if($SAMSConf->access>0)
    {
      print(" parent.basefrm.location.href=\"main.php?show=exe&function=userform&userid=$row[id]\";\n");
    }
  else
    {
      if($SAMSConf->NTLMDOMAIN!="Y")
        $un="$row[nick]";
      else	
        $un="$row[domain]+$row[nick]";

      if((strlen($SAMSConf->domainusername)>0&&$SAMSConf->domainusername==$row[nick])||$SAMSConf->roupauditor==$row[group])
        {
          print(" parent.basefrm.location.href=\"main.php?show=exe&function=userform&userid=$row[id]\";\n");
        }
      else
        {
          print("parent.basefrm.location.href=\"main.php?show=exe&function=userauthform&userid=$row[id]\";\n");
        }
    }
  print("</SCRIPT> \n");

  print("<TABLE border=0 WIDTH=\"100%\">\n");
  print("<TR>\n");
  print("<TD VALIGN=\"TOP\" WIDTH=\"30%\">");
  print("<B>$usertray_UserTray_1 <BR><FONT SIZE=\"+1\" COLOR=\"blue\">$row[nick]</FONT></B>\n");

      ExecuteFunctions("./src", "userbuttom", $row['id']);

  print("<TD>\n");
  print("</TABLE>\n");


}

?>
