<?PHP
//abo-cbq-w.php features: arpupdate, speedupdate, speedcalculate, etc
$error5="error5 Przykro nam, serwis jest dostepny wylacznie dla
autoryzowanych uzytkownikow";

if(file_exists(".config.php")) include(".config.php");


//$komputery="/home/www/netsatmedia_portal/abonenci/www/komputery";
$czy_error_5=exec("cat $komputery |grep $REMOTE_ADDR");
//echo "$REMOTE_ADDR $czy_error_5 <br>" ;
if($czy_error_5) {

$ADM_USER="$SQL_USER";
//$SQL_DATABASE="superusers";
//$INTERFACE_ADDR="localhost";
$ADM_USER_PASS="$SQL_USER_PASS";
$SQL_TABLE_USERS="USERS2";
$SQL_TABLE_USERS_HIST="USERS2_HIST";
$SQL_TABLE_ADMIN_USERS="ADMIN_USERS";
//if ($REMOTE_ADDR == "62.233.148.234") $AREA_LIMIT="and S_IP like '192.168.1.%'";

if($test) echo "OK"; else  if($hw and $dana1) {
     $conn=mysql_pconnect($INTERFACE_ADDR,$ADM_USER,$ADM_USER_PASS);
     if (!$conn) die("ERROR HTTP_ERROR503; podczas tworzenia polaczenia do bazy $SQL_DATABASE !\n");
     mysql_select_db($SQL_DATABASE) or die("ERROR HTTP_ERROR503; podczas wyboru bazy!");

 
     $zapSQLHIST="SELECT ID,S_HWSTAT from $SQL_TABLE_USERS  where S_IP='$dana1' $AREA_LIMIT LIMIT 0,1;";
     $result = mysql_query($zapSQLHIST,$conn) or die("Blad zapytania2!");
while ($h=mysql_fetch_object($result))
{
$ID_USERS2="$h->ID";
$S_HWSTAT="$h->S_HWSTAT";
}
if(strstr($S_HWSTAT,DETECT)) {
$zapSQLHIST="INSERT into $SQL_TABLE_USERS_HIST (ID,ID_USERS2,A_RECORD_UPDATE_DATE,A_RECORD_UPDATE_AUTHOR,S_HW,S_HWSTAT) values ('','$ID_USERS2',CURRENT_DATE,'$REMOTE_HOST','$hw','FIXED');";
     $result = mysql_query($zapSQLHIST,$conn) or die("Blad zapytania2!");
echo " OK HISTORY SUBMITED";
     $zapSQL="UPDATE $SQL_TABLE_USERS set S_HW='$hw', S_HWSTAT='FIXED' where S_IP='$dana1' and S_HWSTAT='DETECT';";
     $result = mysql_query($zapSQL,$conn) or die("Blad zapytania!");
     if($result=1) echo "OK"; else echo "FAILED, ERROR $hw $dana1";
} else echo "ERROR - NOT SUBMITED $hw $dana1 - maybe already done?";

}
else  if($SPD and $dana1) {
//weryfikacja danych
if($U_LD and $U_TB and $U_TS) { //echo  "ZMIENNE-OK" ; 

     $conn=mysql_pconnect($INTERFACE_ADDR,$ADM_USER,$ADM_USER_PASS);
     if (!$conn) die("ERROR HTTP_ERROR503; podczas tworzenia polaczenia do bazy $SQL_DATABASE !\n");
     mysql_select_db($SQL_DATABASE) or die("ERROR HTTP_ERROR503; podczas wyboru bazy!");
     $zapSQL="SELECT ID,S_IP,N_AVG_TRAFFIC,P_SERVICE_QUOTA,P_SERVICE_QUOTA_PROFILE from $SQL_TABLE_USERS  where S_IP='$dana1' $AREA_LIMIT LIMIT 0,1;";
     $result = mysql_query($zapSQL,$conn) or die("Blad zapytania2!");
while ($h=mysql_fetch_object($result))
{
$ID_USERS2="$h->ID";
$WYPIS="$h->N_AVG_TRAFFIC";
$P_SERVICE_QUOTA="$h->P_SERVICE_QUOTA"; //FOR AUTOSPEED CHANGE CALCULATE
$P_SERVICE_QUOTA_PROFILE="$h->P_SERVICE_QUOTA_PROFILE"; //FOR AUTOSPEED CHANGE CALCULATE

function VARDEF() {
global $SPEED ;
$VAR = " Array ( ";
for (;(key($SPEED)); next($SPEED)) 
  { 
$VAR = $VAR . "\"" .  key($SPEED) . "\" => Array ( ";
for (;(key($SPEED[key($SPEED)])); next($SPEED[key($SPEED)])) 
  { 
$VAR = $VAR . " \"" . key($SPEED[key($SPEED)]) . "\"=>\"" . $SPEED[key($SPEED)][key($SPEED[key($SPEED)])] . "\"," ;
  }
$VAR = $VAR . " ), ";
  }
$VAR = $VAR . " ) " ;
//echo $VAR ;
return $VAR;
}



if (! "$TARIFY" ) $TARIFY=T1 ;
if(! "$WYPIS" ) { echo  "Pierwszy Wpis"  ; 


					//$SPEED["AS"]["0"]="$U_LS"	;		//	$AS="$U_LS" ; //TOT_AVG_SPD
//$SPEED["TB"]["T0"]="$U_TB"	;		//	$TB="$U_TB" ; //TOT_BYTES
//$SPEED["TT"]["T0"]="$U_LD" ;		// 	$TT="$U_LD" ; //TOT_TIME
					//$SPEED["AS"]["$TARIFY"]="$U_LS"	;	//	$AS[$TARIFY]="$U_LS" ; //AVG_SPD
$SPEED["TB"]["$TARIFY"]="0"	;	//	$TB[$TARIFY]="$U_TB" ; 		//TOT_BYTES
$SPEED["LB"]["$TARIFY"]="$U_TB"	;	//			 ; 		//LAST_BYTES
$SPEED["TT"]["$TARIFY"]="0"	;	//	$TT[$TARIFY]="$U_LD" ; 		//TOT_TIME
//$SPEED["CD"]["$TARIFY"]="$U_LD"	;	//	$LD[$TARIFY]="$U_LD" ; 		//CURR_DELTA					//$SPEED["LS"]["$TARIFY"]="$U_LS" ; 	//	$LS[$TARIFY]="$U_LS" ; 		//LAST_SPD
$SPEED["LD"]["$TARIFY"]="$U_LD"	;	//	$LD[$TARIFY]="$U_LD" ; 		//LAST_DELTA
$SPEED["TS"]["$TARIFY"]="$U_TS"	;	//	$TS[$TARIFY]="$U_TS" ; 		//TIMESTAMP

 ;

print "$C_WYPIS";

////
//VAR array definiton begins

//EOF $VAR definition
//echo "<HR>";
//print_r($SPEED);
//echo "<HR>";
$C_WYPIS=VARDEF();
     $zapSQL="UPDATE $SQL_TABLE_USERS set N_AVG_TRAFFIC='$C_WYPIS' where S_IP='$dana1' $AREA_LIMIT ;" ;
//echo $zapSQL ;
     $result_1 = mysql_query($zapSQL,$conn) or die("Blad zapytania!");
     if($result_1=1) echo "OK"; else echo "FAILED, ERROR $C_WYPIS $dana1";
$result_1="";

} // EOF Pierwszy Wpis
 else { //echo "Sprawdzamy obecne wpisy<br>";
eval("\$SPEED = $WYPIS ;");
//echo "<HR>";
//print_r($SPEED);
//echo "<HR>";
//echo "Porownujemy czy aktualizujemy - czy nie<br>";
//echo  $SPEED["TS"]["$TARIFY"] . " = " . $U_TS . " TB= " . $SPEED["TB"]["$TARIFY"] . ". " ;
if($SPEED["TS"]["$TARIFY"] == $U_TS) {
   $SPEED["LB"]["$TARIFY"] = $U_TB ;
   $SPEED["LD"]["$TARIFY"] = $U_LD ;

				    }
     else  
   { $SPEED["TB"]["$TARIFY"] = $SPEED["TB"]["$TARIFY"] + $SPEED["LB"]["$TARIFY"] ;
   $SPEED["TT"]["$TARIFY"] =  $SPEED["TT"]["$TARIFY"] +  $SPEED["LD"]["$TARIFY"] ;
   $SPEED["LB"]["$TARIFY"] = $U_TB ;
//echo "diff";
  ;}

$SPEED["LD"]["$TARIFY"] = $U_LD ;
//echo $SPEED["TB"]["$TARIFY"] ;
//$SPEED["TB"]["$TARIFY"]="$U_TB"	;	//TOT_BYTES
//$SPEED["LB"]["$TARIFY"]="$U_TB"	;	//LAST_BYTES
//$SPEED["TT"]["$TARIFY"]="$U_LD"	;	//TOT_TIME
//$SPEED["LD"]["$TARIFY"]="$U_LD"	;	//LAST_DELTA
$SPEED["TS"]["$TARIFY"]="$U_TS"	;	//TIMESTAMP
//$SPEED["TB"]["0"]="$U_TB"	;	 //TOT_BYTES
//$SPEED["TT"]["0"]="$U_LD" ;		 //TOT_TIME

//print_r($SPEED);

$C_WYPIS=VARDEF();
     $zapSQL="UPDATE $SQL_TABLE_USERS set N_AVG_TRAFFIC='$C_WYPIS' where S_IP='$dana1' $AREA_LIMIT ;" ;
//echo $zapSQL ;
    $result_1 = mysql_query($zapSQL,$conn) or die("Blad zapytania!");
     if($result_1=1) echo "OK"; else echo "FAILED, ERROR $C_WYPIS $dana1";
$result_1="";
//SPEED AUTOCHANGE OPTION
if($TARIFY) {
if ($P_SERVICE_QUOTA_PROFILE) eval("\$LIMITS = $P_SERVICE_QUOTA_PROFILE ;"); else {

//VAR DECLARATION

$LIMITS["LBS_TBS_MAX_ANDOR"]["T1"]="OR"; //OPERATOR AND lub OR (zmniejszanie speeda IF)
$LIMITS["LBS_TBS_MIN_ANDOR"]["T1"]="AND"; //OPERATOR AND lub OR (zwiekszanie speeda IF)
$LIMITS["LBS_MIN_OK"]["T1"]="3000"; //in Bps LAST BYTES jezeli mniej to zwiekszamy
$LIMITS["LBS_MAX_OK"]["T1"]="6000"; //in Bps LAST BYTES jezeli wiecej to zmniejszamy
$LIMITS["TBS_MIN_OK"]["T1"]="2000"; //in Bps TOTAL BYTES jezeli mniej to zwiekszamy
$LIMITS["TBS_MAX_OK"]["T1"]="4000"; //in Bps TOTAL BYTES jezeli wiecej to zwiekszamy
$LIMITS["INC_STEP"]["T1"]="10"; //in kilobits O tyle jest zwiekszany speed 
$LIMITS["DEC_STEP"]["T1"]="10"; //in kilobits O tyle jest zmniejszany speed
$LIMITS["MAX_SPD"]["T1"]="160"; //in Kilobits per second - MAX speed dla abonenta
$LIMITS["MIN_SPD"]["T1"]="70"; //in Kilobits per second - MIN speed dla abonenta
$LIMITS["LBS_DELTA_MIN"]["T1"]="3600"; //in secons - MIN delta for speed calculate
} //EOF if ($P_SERVICE_QUOTA_PROFILE) else

//EOF VAR DELARATION
//echo " * SPEED[TB][TARIFY] :";
//echo $SPEED["TB"]["$TARIFY"]  ;
//echo " * SPEED[TT][TARIFY] :";
//echo $SPEED["TT"]["$TARIFY"]  ;
//echo " * SPEED[LB][TARIFY] :";
//echo $SPEED["LB"]["$TARIFY"]  ;
//echo " * SPEED[LD][TARIFY] :";
//echo $SPEED["LD"]["$TARIFY"]  ;
//print_r($LIMITS);
 if(strlen($LIMITS["LBS_TBS_MAX_ANDOR"]["$TARIFY"])>0 and
    strlen($LIMITS["LBS_TBS_MIN_ANDOR"]["$TARIFY"])>0 and
    strlen($LIMITS["LBS_MIN_OK"]["$TARIFY"])>0 and
    strlen($LIMITS["LBS_MAX_OK"]["$TARIFY"])>0 and
    strlen($LIMITS["TBS_MIN_OK"]["$TARIFY"])>0 and
    strlen($LIMITS["TBS_MAX_OK"]["$TARIFY"])>0 and
    strlen($LIMITS["INC_STEP"]["$TARIFY"])>0 and
    strlen($LIMITS["DEC_STEP"]["$TARIFY"])>0 and
    strlen($LIMITS["MAX_SPD"]["$TARIFY"])>0 and
    strlen($LIMITS["MIN_SPD"]["$TARIFY"])>0 and
    strlen($LIMITS["LBS_DELTA_MIN"]["$TARIFY"])>0) {
  if ($SPEED["LD"]["$TARIFY"] > $LIMITS["LBS_DELTA_MIN"]["$TARIFY"] ) {
  //POD katem zwiekszenia limitow
  if ($SPEED["TB"]["$TARIFY"]/$SPEED["TT"]["$TARIFY"] < $LIMITS["TBS_MIN_OK"]["$TARIFY"]) $VAR_INC_TOT="OK";
  if ($SPEED["LB"]["$TARIFY"]/$SPEED["LD"]["$TARIFY"] < $LIMITS["LBS_MIN_OK"]["$TARIFY"]) $VAR_INC_LAST="OK";
  if ($SPEED["TB"]["$TARIFY"]/$SPEED["TT"]["$TARIFY"] > $LIMITS["TBS_MAX_OK"]["$TARIFY"]) $VAR_DEC_TOT="OK";
  if ($SPEED["LB"]["$TARIFY"]/$SPEED["LD"]["$TARIFY"] > $LIMITS["LBS_MAX_OK"]["$TARIFY"]) $VAR_DEC_LAST="OK";
  //echo " * VAR INCDECs $VAR_INC_TOT , $VAR_INC_LAST , $VAR_DEC_TOT , $VAR_DEC_LAST EOF";
  if (($VAR_INC_TOT=="OK" or $VAR_INC_LAST=="OK") and $LIMITS["LBS_TBS_MIN_ANDOR"]["$TARIFY"]=="OR")  $VAR_INC_PERM="OK";
  if (($VAR_INC_TOT=="OK" and $VAR_INC_LAST=="OK") and $LIMITS["LBS_TBS_MIN_ANDOR"]["$TARIFY"]=="AND")  $VAR_INC_PERM="OK";
  if (($VAR_DEC_TOT=="OK" or $VAR_DEC_LAST=="OK") and $LIMITS["LBS_TBS_MAX_ANDOR"]["$TARIFY"]=="OR")  $VAR_DEC_PERM="OK";
  if (($VAR_DEC_TOT=="OK" and $VAR_DEC_LAST=="OK") and $LIMITS["LBS_TBS_MAX_ANDOR"]["$TARIFY"]=="AND")  $VAR_DEC_PERM="OK";
 } //EOF if ($SPEED["LD"]["$TARIFY"] > $LIMITS["LBS_DELTA_MIN"]["$TARIFY"] )
 if ($VAR_DEC_PERM=="OK" or $VAR_INC_PERM=="OK") {
   //echo " * CHANGING VALUES of P_SERVICE_QUOTA: $P_SERVICE_QUOTA * ";
   //Czy dodajemy czy odejmujemy oraz ile
   if ($VAR_INC_PERM=="OK") $C_P_SERVICE_QUOTA=$P_SERVICE_QUOTA + $LIMITS["INC_STEP"]["$TARIFY"] ;
   if ($VAR_DEC_PERM=="OK") $C_P_SERVICE_QUOTA=$P_SERVICE_QUOTA - $LIMITS["DEC_STEP"]["$TARIFY"] ;
   if ($VAR_INC_PERM=="OK" and ($C_P_SERVICE_QUOTA > $LIMITS["MAX_SPD"]["$TARIFY"] )) $C_P_SERVICE_QUOTA=$LIMITS["MAX_SPD"]["$TARIFY"] ;
   if ($VAR_DEC_PERM=="OK" and ($C_P_SERVICE_QUOTA < $LIMITS["MIN_SPD"]["$TARIFY"] )) $C_P_SERVICE_QUOTA=$LIMITS["MIN_SPD"]["$TARIFY"] ;
   //mysql_select_db($SQL_DATABASE) or die("ERROR podczas wyboru bazy!");
    $zapSQL3="UPDATE $SQL_TABLE_USERS set P_SERVICE_QUOTA='$C_P_SERVICE_QUOTA'  where S_IP='$dana1' $AREA_LIMIT ;";
    echo "Zapytanie do bazy odnosnie zmiany: $zapSQL3 *";
    $result3 = mysql_query($zapSQL3,$conn) or die("Blad zapytania3!");
  } //EOF if ($VAR_DEC_PERM=="OK" or $VAR_INC_PERM="OK") 
 } else echo "*PROBLEM-at-USERSSETTINGS*"; //EOF LONG... Variables Check
 } //EOF SPEED AUTOCHANGE OPTION

 }
} 






//EOF ZMIENNEOK
} else {
echo "TUTAJJJJ" ;
//##

} //EOF zmienne NIEOK

//##
} else {
     $conn=mysql_pconnect($INTERFACE_ADDR,$ADM_USER,$ADM_USER_PASS);
     if (!$conn) die("ERROR HTTP_ERROR503; podczas tworzenia polaczenia do bazy $SQL_DATABASE !\n");
     mysql_select_db($SQL_DATABASE) or die("ERROR HTTP_ERROR503; podczas wyboru bazy!");
     $zapSQL="SELECT * FROM $SQL_TABLE_USERS where S_IP='$dana1' $AREA_LIMIT LIMIT 0,1 ;";
//echo "$zapSQL";
     $result = mysql_query($zapSQL,$conn) or die("ERROR HTTP_ERROR503; Blad zapytania!");
 //   $user=mysql_fetch_object($result);
 //    if ((mysql_num_rows($result)>0) && ($user->pass==$PHP_AUTH_PW))
while ($h=mysql_fetch_object($result))
{
echo "$h->S_IP;";
echo "$h->P_NAME;";
echo "$h->S_HOST;";
//echo "$h->S_HW;";
//echo "$h->S_HW;";
//S_HW
if(strstr($h->S_HWSTAT,BLOCK)) echo "BLOCK $h->A_STATUS_INFO $h->S_HW;"; else 
if(strstr($h->S_HWSTAT,FREE)) echo "FREE $h->S_HW $h->A_STATUS_INFO;";  else  
if(strstr($h->S_HWSTAT,DETECT)) echo "FREE DETECT $h->S_HW $h->A_STATUS_INFO;";  else {
//if(strstr($h->S_HWSTAT,DETECT)) echo "FREE DETECT $h->S_HW $h->A_STATUS_INFO;";  else {


if(strstr($h->S_HW,BLOCK)) {
echo "$h->S_HW;";
} else {
if(strstr($h->S_HW,FREE)) {
echo "FREE;";
} else echo "$h->S_HW;"; 
}
} 
//S_HWSTAT_DETECT
//S_HW KONIEC

echo "$h->S_ADDRESS_STREET;";
echo "$h->P_SERVICE_QUOTA;";
//echo "$h->S_HWSTAT;";
//echo "nowa";
}
}
} else {
echo "$error5 HTTP_ERROR503;" ;
}
//echo "KONIEC";
?>

