Location: PHPKode > projects > phpMyPrepaid > phpmyprepaid/www/include/account/DBinsert.php
<?php
// ---> CLEAN ME  Need to look into one big insertion
// frist we make sure the session locID is set.
// if it is not set then we give them an error

include("include/configuration/".$_SESSION['gatewayType'].".attr");
//DATE IN CHILLI FORMAT echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));
if (!isset($oreon))
                exit();
if(isset($error)){ unset($error);} //just to be sure

$creator=$oreon->user->get_alias();

if(!isset($error)){
  $loc = $_SESSION['locID'];
  if (isset( $_POST['FirstName']))
  	$FirstName = $_POST['FirstName'];
	
  if (isset( $_POST['LastName']))
  	$LastName = $_POST['LastName'];
	
  if (isset( $_POST['CustID']))
  	$CustID = $_POST['CustID'];
	
  if (isset( $_POST['Email']))
  	$Email = $_POST['Email'];
	
  if (isset( $_POST['UploadLimit']))
  	$UploadLimit = $_POST['UploadLimit'];
	
  if (isset( $_POST['DownloadLimit']))
  	$DownloadLimit = $_POST['DownloadLimit'];

  if (isset( $_POST['TimeToFinish']))
  {
	if ($_POST['TimeToFinish']=="true")
		$timeToFinish="1";
	else
	   $timeToFinish="0";
  }
  else
     $timeToFinish=$default_timeToFinish;
  //WE AFFECT VALUE FOR INSERTION

   $locationIdValue= get_location_string($loc);
if ($debug) echo "<br> BILLING PLAN :".$idBp;
if ($debug) echo "<br> UPLOAD :".$UploadLimit;
if ($debug) echo "<br> DOWNLOAD :".$DownloadLimit;
if ($debug) echo "<br> BUTTON ID ".$buttonID;

//FOR FUTUR USE
   //$macAuthValue=
   // $serviceTypeValue=

if($buttonID == "CR_Quick"){
    $Type = "Hourly";
    $Attribute = $sessionTimeAttr;
    $value=$min;
    $dbtable= $sessionTimeAttrdb;
}

if($buttonID == "CR_Expiration"){
    $Type = "Expiration";
    $Attribute = $expirationWisprAttr;
    $dbtable= $expirationWisprAttrdb;
    if (!check_date(split(' ',$exp)))
       {
         unset($buttonID);
			return $error="<p class=error>Date format invalid !!!!!<p>";
       }
    print '<br>/////////////// VALUE '.$exp.' GATEWA '.$gatewayType;
  	$value=date_to_attribute($exp,$gatewayType);
     }  
  if($buttonID == "CR_Timed"){
    $Type = "Hourly";
    $Attribute = $sessionTimeAttr;
    $value=$min;
    $MaximumtimeBp= $MaximumtimeBp;
    $dbtable= $sessionTimeAttrdb;
  }
								
  if($buttonID == "CR_Octets"){
    $Type = "Octets";
    $tab= get_octet_type_attr($octet_type);
    $Attribute = $tab[0];
    $dbtable= $tab[1];
    $MaximumoctetBp= $MaximumoctetBp;
    $value=$moctets;
  }

  if($buttonID == "CR_subscription_time"){
    $Type="Subscription Time";
    $Attribute = $sessionTimeAttr;
    $dbtable= $sessionTimeAttrdb;
    $value=$min;
    $MaximumtimeBp= $MaximumtimeBp;
  }
	
  if($buttonID == "CR_subscription_octets"){
    $Type="Subscription Octets";
    $tab= get_octet_type_attr($octet_type);
    $Attribute = $tab[0];
    $dbtable= $tab[1];
    $value=$moctets;
    $MaximumoctetBp= $MaximumoctetBp;
  }

  if($buttonID == "CR_subscription_mac"){
    $Type="Subscription Mac";
    $Attribute = $macAuthAttr;
    $dbtable= $macAuthAttrdb;
    $value=$val;
  }



  if($buttonID == "CR_subscription_expiration"){
    $Type="Subscription Expiration";
    $Attribute = $expirationWisprAttr;
    $dbtable= $expirationWisprAttrdb;
    if (!check_date(split(' ',$exp)))
       {
         unset($buttonID);
			return $error="<p class=error>Date format invalid !!!!!<p>";
       }
  	$value=date_to_attribute($exp,$gatewayType);
     }  

  if ($debug) {
	  echo " DBTABLE 1".$dbtable;
	  echo " DBTABLE 2".$dbtable2;

  } 


 
// WE AFFECT VALUE FOR BILLING PLAN
   $q1 = "select * From BillingPlan where Id='$idBp'";
   $result=$oreon->database->database->query($q1);
   while ($row = $oreon->database->database->fetch_array($result)) {
	   if ($row['TimetofinishBp']!=0)
		   $timeToFinish=$row['TimetofinishBp'];
	   if ($row['SimultanousBp']!=0)
		   $simUse=$row['SimultanousBp'];
	   if ($row['RedirectionurlBp']!="")
		$redirectionUrlValue=$row['RedirectionurlBp'];
	   if (is_numeric($row['MaximumtimeBp']) and $row['MaximumtimeBp']!=0)
		$MaximumtimeBp=$row['MaximumtimeBp'];
	   if (is_numeric($row['MaximumoctetBp']) and $row['MaximumoctetBp']!=0)
		$MaximumoctetBp=$row['MaximumoctetBp'];
	   if ($row['ExpirationdateBp']!="" and $row['ExpirationdateBp']!=0)
		   $ExpirationdateBp=$row['ExpirationdateBp'];
	   if ($row['BandwidthuploadBp']!=0)
	   {
		   if (!isset($UploadLimit))
			   $UploadLimit=$row['BandwidthuploadBp']*1000;
	   }
	   if ( $row['BandwidthdownloadBp']!=0)
		   if (!isset($DownloadLimit))
		   $DownloadLimit=$row['BandwidthdownloadBp']*1000;

	}
if ($debug) echo "<br> UPLOAD FINAL :".$UploadLimit;
if ($debug) echo "<br> DOWNLOAD FINAL :".$DownloadLimit;
   

 //WE START THE INSERTION 
  foreach($UserPass as $id => $pass ) { 

 //INSERT RADIUS PROFILE FOR BILLING PLAN
	  if ($idBp) $bpname=get_plan_name($idBp);
	  if ($bpname!="") insert_radius_profile($id,$bpname, $loc );
  //INSERT RADIUS PROFILE FOR BILLING PLAN
	  if ($idBp)
	     $gp_name= get_group_name(get_plan_group($idBp));
      if ($gp_name) insert_group($id,$gp_name, $loc);
      
   if ($radiusprofile!="None" and $radiusprofile!='' and  $radiusprofile!=-1) {
		  if ($debug) print "We insert user $id in radius profile $radiusprofile";
		  insert_user_radius_profile( $id,get_radiusprofile_name($radiusprofile),$loc);
   }
   
	  if ($group!="None" and $group!='' and $group!=-1) {
		   if ($debug) print "We insert user $id in group $group";
	   insert_group( $id, get_group_name($group),$loc);
   }

    if ($LastName==''){
      $LastName=$id;
    }
    
    if (existUser($id)=='True')
      {
			$error="<p class=error>You can't add two users with same name !!!!!<p>";
	return;
      }

    //USER PASSWORD
     $q0= "INSERT INTO radcheck (UserName," .
		" Attribute, op, Value ) " .
      "VALUES" .
		"('$id', 'User-Password'," .
		" ':=', '".crypt_acct_passwd($crypt,$pass)."')";
    $numresults=$oreon->database->database->query($q0);

    //SIM USE
    if ($simUse!='' && $simUse!='0')
    {    
	$q1= "INSERT INTO radcheck (UserName," .
			" Attribute, op, Value ) " .
      "VALUES" .
			"('$id'," .
			" 'Simultaneous-Use', ':=', '$simUse')";
    $numresults=$oreon->database->database->query($q1);
    }

    //ATTRIBUTE MAX ALL SESSION OR MAX OUTPUT OCTE OR EXPIRATION DATE 
    $q2= "INSERT INTO $dbtable (UserName, " .
		" Attribute, op, Value)" .
		" VALUES('$id', " .
		" '$Attribute', ':=', '$value' )";
    
    $numresults=$oreon->database->database->query($q2);
    if ($debug) {
	  echo "<br> EXPIRATION ".$ExpirationdateBp;
	  echo "<br> TIME ".$MaximumtimeBp;
	  echo "<br> OCTET ".$MaximumoctetBp;
     }

//BILLING PLAN EXP
    if ($ExpirationdateBp!="")
    {
	    if ($debug) echo " DATE :".$ExpirationdateBp;
	    if (!check_date(split(' ',$ExpirationdateBp)))
	    {
		    return $error="<p class=error>Date format invalid in Billing Plan definition!!!!!<p>";
	    }
	    $ExpirationdateBpf=date_to_attribute($ExpirationdateBp,$gatewayType);
	    $dbtable2= $expirationWisprAttrdb;
	    $Attribute2 = $expirationWisprAttr;
	    $q2= "INSERT INTO $dbtable2 (UserName, " .
		    " Attribute, op, Value)" .
		    " VALUES('$id', " .
		    " '$Attribute2', ':=', '$ExpirationdateBpf' )";
	    $numresults=$oreon->database->database->query($q2);
	    
    }
    
//BILLING PLAN TIME
    if ($MaximumtimeBp!="" and $MaximumtimeBp!=0)
    {
	    $dbtable2= $sessionTimeAttrdb;
	    $Attribute2 = $sessionTimeAttr;
	     $q2= "INSERT INTO $dbtable2 (UserName, " .
	                      " Attribute, op, Value)" .
	                                       " VALUES('$id', " .
	                                                       " '$Attribute2', ':=', '$MaximumtimeBp' )";
    $numresults=$oreon->database->database->query($q2);

	   
    }

//BILLING PLAN OCTE
    if ($MaximumoctetBp!="" and $MaximumoctetBp!=0)
    {
	  $tab= get_octet_type_attr($octet_type);
	  $Attribute2= $tab[0];
	  $dbtable2= $tab[1];
	   $q2= "INSERT INTO $dbtable2 (UserName, " .
	                      " Attribute, op, Value)" .
	                                       " VALUES('$id', " .
	                                                       " '$Attribute2', ':=', '$MaximumoctetBp' )";
    $numresults=$oreon->database->database->query($q2);

    }

  if ($debug) {
	  echo "<br> DBTABLE 1".$dbtable;
	  echo " <br> DBTABLE 2".$dbtable2;
  }

    //IDLE TIMEOUT 
    if ($idleTimeoutValue!="" and $idleTimeoutAttr!="" and $idleTimeoutValue!="0") {
      $idle=$idleTimeoutValue*60;
    $q3= "INSERT INTO $idleTimeoutAttrdb (UserName," .
      " Attribute, op, Value) " .
      "VALUES" .
      "('$id'," .
      " '$idleTimeoutAttr', ':=', '$idle')";
    $numresults=$oreon->database->database->query($q3);
    }
    
    //REPLY MESSAGE
    if ($replyMessageValue!='' and $replyMessageAttr!="") 
    { 
    $q4= "INSERT INTO $replyMessageAttrdb (UserName," .
      " Attribute, op, Value) " .
      "VALUES" .
      "('$id'," .
      " '$replyMessageAttr', ':=', '$replyMessageValue $LastName')";
    
    $numresults=$oreon->database->database->query($q4);
    }
    
    //Insert NAS location -----------------------------

  if ($locationNasIdentifier==1) { 
        $locN= get_local_name($loc);
   
  
        $q99= "INSERT INTO radcheck (UserName," .
              " Attribute, op, Value ) " .
              "VALUES" .
              "('$id', 'NAS-Identifier'," .
              " ':=', '$locN')";
        $numresults=$oreon->database->database->query($q99);
  }

  //LOCATION ATTRIBUTE
  if ($locationIdValue != "") 
  {  
			$q7= "INSERT INTO $locationIdAttrdb (UserName, " .
			" Attribute, op, Value)" .
			" VALUES('$id'," .
			" '$locationIdAttr', ':=', '$locationIdValue')";
    $numresults=$oreon->database->database->query($q7);
  }
    
  //ACCT INTERIM INTERVAL  ATTRIBU
    if ( $acctIntervalValue!="" and $acctIntervalValue!="0" and $acctIntervalAttr!="") {
      
     $acctiv=$acctIntervalValue*60;
     $q8= "INSERT INTO $acctIntervalAttrdb (UserName," .
          " Attribute, op, Value) " .
          "VALUES" .
          "('$id'," .
          " '$acctIntervalAttr', ':=', '$acctiv')";

    $numresults=$oreon->database->database->query($q8);
    }
    
   //LOGOFF URL ATTRIBUTE
    if ($logoffUrlValue!="" and $logoffUrlAttr!="") {
      
    $q9= "INSERT INTO $logoffUrlAttrdb (UserName," .
      " Attribute, op, Value) " .
      "VALUES" .
      "('$id'," .
      " '$logoffUrlAttr', ':=', '$logoffUrlValue')";
    $numresults=$oreon->database->database->query($q9);
    }
    
    //REDIRECTION URL ATTRIBUTE
    if ($redirectionUrlValue!="" and $redirectionUrlAttr!="") {
            
    $q10= "INSERT INTO $redirectionUrlAttrdb (UserName," .
      " Attribute, op, Value) " .
      "VALUES" .
      "('$id'," .
      " '$redirectionUrlAttr', ':=', '$redirectionUrlValue')";
    $numresults=$oreon->database->database->query($q10);
    }

    // UAM ALLOWED ATTR
    if ($uamAllowedValue!="" and $uamAllowedAttr!="") {
            
    $q10b= "INSERT INTO $uamAllowedAttrdb (UserName," .
      " Attribute, op, Value) " .
      "VALUES" .
      "('$id'," .
      " '$uamAllowedAttr', ':=', '$uamAllowedValue')";
    $numresults=$oreon->database->database->query($q10b);
    }
    
    // MAC ALLOWED ATTR
    if ($macAllowedValue!="" and $macAllowedAttr!="") {
            
    $q10c= "INSERT INTO $macAllowedAttrdb (UserName," .
      " Attribute, op, Value) " .
      "VALUES" .
      "('$id'," .
      " '$macAllowedAttr', ':=', '$macAllowedValue')";
    $numresults=$oreon->database->database->query($q10c);
    }
      
    // SERVICE TYPE ATTR
    if ($serviceTypeValue!="" and $serviceTypeAttr!="") {
            
    $q10d= "INSERT INTO $serviceTypeAttrdb (UserName," .
      " Attribute, op, Value) " .
      "VALUES" .
      "('$id'," .
      " '$serviceTypeAttr', ':=', '$serviceTypeValue')";
    $numresults=$oreon->database->database->query($q10d);
    }

    // MAC AUTH ATTR
    if ($macAuthValue!="" and $macAuthAttr!="") {
            
    $q10e= "INSERT INTO $macAuthAttrdb (UserName," .
      " Attribute, op, Value) " .
      "VALUES" .
      "('$id'," .
      " '$macAuthAttr', ':=', '$macAuthValue')";
    $numresults=$oreon->database->database->query($q10e);
    }
  
   //TIME TO FINISH 
    if ($Type=="Hourly" or $Type=="Subscription Time" and ($timeToFinish==1 and $expirationDateAttr!="")) {
	   
	    $dateradius=date_to_attribute(date('Y-m-d H:i:s',mktime(0, 0, 0, date("m")+$numMonthExpireAccount, date("d"), date("Y") )),"freeradius");    
	    if ($debug) {
		    echo "DATE RADIUS ".$dateradius;
		    echo "MAX MONTH OLD ".$numMonthExpireAccount;
	    }
	    $q12= "INSERT INTO $expirationDateAttrdb (UserName," .
		    " Attribute, op, Value)" .
		    " VALUES('$id', " .
		    " '$expirationDateAttr', ':=','$dateradius')";
	    
	    $numresults=$oreon->database->database->query($q12);
	    
    }
    //BANDWIDTH UPLOAD
    //
	  $Attributeu = $bandwidthMaxUpAttr;
	  
	  if ($Attributeu!="" and $UploadLimit>0)
	  {
		  $q2= "INSERT INTO $bandwidthMaxUpAttrdb (UserName," .
			  " Attribute, op, Value)" .
			  " VALUES('$id'," .
			  " '$bandwidthMaxUpAttr', ':=', $UploadLimit)";
	    $numresults=$oreon->database->database->query($q2);
	  }
	  
    //BANDWIDTH DOWNLOAD
    //
	  $Attributed =$bandwidthMaxDownAttr;
	  
	  if ($Attributed!="" and $DownloadLimit>0)
	  {
		  
		  $q3= "INSERT INTO $bandwidthMaxDownAttrdb (UserName," .
			  " Attribute, op, Value)" .
			  " VALUES('$id'," .
			  " '$bandwidthMaxDownAttr', ':=', $DownloadLimit)";
		  $numresults=$oreon->database->database->query($q3);
	  }
	  
	  $query="INSERT into userinfo (CustID, Firstname, Lastname, Username, LocationID, Mail, UploadLimit, DownloadLimit,Creator,RadiusProfile,Type,BillingPlan,TimeToFinish,CrDate) VALUES('$CustID', '$FirstName', '$LastName', '$id', '$loc', '$Email', '$UploadLimit', '$DownloadLimit','$creator','$radiusprofile','$Type','$idBp','$timeToFinish',NOW())";
	  $numresults=$oreon->database->database->query($query);
	  
	  
	$LastName='';
  }
}  
else{
	echo($error);
}
//------------> END database insertion
Return current item: phpMyPrepaid