<?php
	include_once('../config.php');   
	include_once(ABS_PATH.'/custom_function/default.php');  
 

        if(isset($_REQUEST['RefId']))
        {
            
             header("Location: ../mob/welcome/register.php?RefId=".$_REQUEST['RefId']);
             die;
        }
             
         
                    
           if(isset($_SESSION['userid']))
            { 
                 header("Location: ../");
                 die;
            }
    
       
    if(isset($_POST['SignIN']))
    {
        header("Location: ".ROOTPATH."/login");
        die;
    }
			
			
		
                if($_REQUEST['CandelReg']==1)
                { 
                
                session_destroy();
                header("Location:   ".ROOTPATH."/registration ");
                die;   
                }
        
			
                if($_REQUEST['resendOtp']==1)
                { 
                
                    $CMB = $_SESSION['MobOtp'];
                    $mob = $_SESSION['MobReg']['registered_mob'];
                    $msg = urlencode(''.$CMB.' is your mobile verification one time password to proceed on DAILYPAY. It is valid for 5 minitus.'); 
                    $URL  = 'http://mobicomm.dove-sms.com//submitsms.jsp?user=DailyPAY&key=d92eabc025XX&mobile=+91'.$mob.'&message='.$msg.'&senderid=DAYPAY&accusage=1&entityid=1201169824017740929&tempid=1207169907610629409';
                    
                    $crl = curl_init();
                    curl_setopt($crl, CURLOPT_URL, $URL);
                    curl_setopt($crl, CURLOPT_FRESH_CONNECT, true);
                    curl_setopt($crl, CURLOPT_RETURNTRANSFER, true); 
                    $response = curl_exec($crl);  
                    header("Location:   ".ROOTPATH."/registration ");
                    die;  
                
                }
            
            
            
            if(isset($_POST['SubmitOtp']))
            {
                if($_SESSION['MobOtp']==$_POST['Eotp']) 
                 {                
                     
                        $_POST = $_SESSION['MobReg'];   
                        $stmt=$conn->prepare("select   id from UI__Rgst where  cuurentId=:cuurentId");   
                        $stmt->bindValue(':cuurentId', $_POST['refId'] , PDO::PARAM_STR); 
                        $stmt->execute();    
                        if($stmt->rowCount() > 0)   
                        $datax = 1;  
                                  
                        if( ($datax > 0) && (strlen($_POST['UserPin'])==4))
                        {   
                        
                        $MCt = 0;
                        $stmt=$conn->prepare("select id from UI__Rgst  where  phm=:phm ");   
                        $stmt->bindValue(':phm',  $_POST['ph_no'] , PDO::PARAM_STR);  
                        $stmt->execute();
                        $MCt  = $stmt->rowCount() ;
                         
                        $CustId = substr(str_shuffle('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'),0, 8);   
                        $digits = 6;
                        $cuurentId = "DP".rand(pow(10, $digits-1), pow(10, $digits)-1);    
                        $string = str_replace('DP', '', $cuurentId)."@dpay"; // Replaces all spaces with hyphens. 
                        $stmt=$conn->prepare("insert into  UI__Rgst (nm,phm,eml,pwd,custId,is_active,RegOn,cuurentId,refId,address,txnpwd,UPI,UserPin,PinDis) values(:nm,:phm,:eml,:pwd,:custId,:is_active,:RegOn,:cuurentId,:refId,:address,:txnpwd,:UPI,:UserPin,1) ");    
                         
                        $stmt->bindValue(':nm', $_POST['Fname'] , PDO::PARAM_STR); 
                        $stmt->bindValue(':phm',  $_POST['ph_no'] , PDO::PARAM_INT); 
                        $stmt->bindValue(':eml', $_POST['email'] , PDO::PARAM_STR); 
                        $stmt->bindValue(':pwd', $_POST['passwprd'] , PDO::PARAM_STR); 
                        $stmt->bindValue(':custId', $CustId, PDO::PARAM_STR);  
                        $stmt->bindValue(':is_active', 1, PDO::PARAM_STR);
                        $stmt->bindValue(':RegOn', date("d-m-Y"), PDO::PARAM_STR);      
                        $stmt->bindValue(':cuurentId', $cuurentId , PDO::PARAM_STR);    
                        $stmt->bindValue(':refId', $_POST['refId'], PDO::PARAM_STR);    
                        $stmt->bindValue(':address', $_POST['address'], PDO::PARAM_STR);   
                        $stmt->bindValue(':txnpwd', $_POST['txnpwd'], PDO::PARAM_STR);  
                        $stmt->bindValue(':UPI', $string, PDO::PARAM_STR);   
                        $stmt->bindValue(':UserPin', $_POST['UserPin'], PDO::PARAM_STR);       
                        $stmt->execute();      
                        
                         
                        $stmt=$conn->prepare("select  id from UI__Rgst order by id desc");           
                        $stmt->execute();    
                        $Mid = $stmt->fetch(PDO::FETCH_OBJ);
                        
                        
                        $stmt=$conn->prepare("insert into  _pkCc__ (Uxd) values(:Uxd) ");   
                        $stmt->bindValue(':Uxd', $Mid->id , PDO::PARAM_STR);   
                        $stmt->execute();        
                        
                      
                    	$subject = "Thanks for registration on ".$SITE_NAME;  
                        $template_type = 1;
                        $attachment = '';
                        $name = $_POST['Fname'];
                        
                        $Password = $_POST['passwprd'];
                           
                            $message="
                            <tr>
                            <td align='left' valign='top'>
                            <h4 style='font-weight:bold; padding:2px 2px; margin:3px; text-align:left; color:#002368;'>Dear user,</h4>
                            <h5 style='font-weight:bold; padding:2px 2px; margin:0px; text-align:center; color:#002368;'>Thanks for registered with us. Your Details are as follows </h5> 
                            <h5 style='font-weight:bold; padding:2px 2px; margin:0px; text-align:center; color:#002368;'> Name: $name , Your ID : $cuurentId , Password : $Password</h5>
                             
                            </td>
                            </tr>";
                            
                           
                      
                        SendMail($_POST['email'],$subject,$message,$template_type,$attachment);        
                        
                        $stmt=$conn->prepare("select * from  UI__DChstr  where  Rid=:cuurentId and typ=1");   
                        $stmt->bindValue(':cuurentId',  $_POST['refId'] , PDO::PARAM_STR);  
                        $stmt->execute();
                        $UI__DChstr = $stmt->fetch(PDO::FETCH_OBJ); 
                        
                        if( ($stmt->rowCount() > 0)  && ($MCt<1) )     
                        {  
                        
                        
                            $stmt=$conn->prepare("insert into  UI__Fundtransfer (userid,Tdate,amt,availableFund) values(:userid,:Tdate,:amt,:availableFund)    "); 
                            $stmt->bindValue(':userid',$cuurentId, PDO::PARAM_STR); 
                            $stmt->bindValue(':Tdate',date("d-M-Y H:i"), PDO::PARAM_STR); 
                            $stmt->bindValue(':amt',20, PDO::PARAM_STR);  
                            $stmt->bindValue(':availableFund', 20 , PDO::PARAM_STR);  
                            $stmt->execute();   
                            
                            
                            LvBal($cuurentId,20,1,"Referral Income",$_POST['refId']);    
                            
                            
                            $stmt=$conn->prepare("select * from UI__Rgst  where  cuurentId=:cuurentId  ");   
                            $stmt->bindValue(':cuurentId',  $cuurentId , PDO::PARAM_STR);  
                            $stmt->execute();
                            $UI__Rgst = $stmt->fetch(PDO::FETCH_OBJ);     	
                            
                            
                            $stmt=$conn->prepare("insert into UI__Receipt(type,bank,mobile,email,amountPaid,Tid,status,Tdate,custId,cdId,Topay,TopayName) values(:type,:bank,:mobile,:email,:amountPaid,:Tid,:status,:Tdate,:custId,:cdId,:Topay,:TopayName)"); 
                            $stmt->bindValue(':type',   15 , PDO::PARAM_STR);   
                            $stmt->bindValue(':bank',  "Referral Balance" , PDO::PARAM_STR);   
                            $stmt->bindValue(':mobile', $UI__Rgst->phm, PDO::PARAM_STR);
                            $stmt->bindValue(':email', $UI__Rgst->eml , PDO::PARAM_STR); 
                            $stmt->bindValue(':amountPaid', 20  , PDO::PARAM_STR); 
                            $stmt->bindValue(':Tid', mt_rand(1234552,23555), PDO::PARAM_STR); 
                            $stmt->bindValue(':status', 1 , PDO::PARAM_STR); 
                            $stmt->bindValue(':Tdate', date("d-M-Y H:i") , PDO::PARAM_STR);      
                            $stmt->bindValue(':custId', $cuurentId, PDO::PARAM_STR);  
                            $stmt->bindValue(':cdId', $_SESSION['LastIntId']  , PDO::PARAM_STR); 
                            $stmt->bindValue(':Topay',  $cuurentId , PDO::PARAM_STR); 
                            $stmt->bindValue(':TopayName', $_POST['refId'] , PDO::PARAM_STR);     
                            $stmt->execute();    
                            unset($_SESSION['LastIntId']);
                            
                            
                            
                             
                            $stmt=$conn->prepare("select  amt,availableFund from  UI__Fundtransfer   where userid=:cuurentId order by id desc");  
                            $stmt->bindValue(':cuurentId', $_POST['refId'], PDO::PARAM_STR);
                            $stmt->execute();  
                            
                            if($stmt->rowCount() > 0)
                            {
                            $AllUsers = $stmt->fetch(PDO::FETCH_OBJ); 
                            $totalAvaib = $AllUsers->availableFund+20;  
                            }else
                            $totalAvaib =  20;     
                            
                            $stmt=$conn->prepare("insert into  UI__Fundtransfer (userid,Tdate,amt,availableFund) values(:userid,:Tdate,:amt,:availableFund)    "); 
                            $stmt->bindValue(':userid',$_POST['refId'], PDO::PARAM_STR); 
                            $stmt->bindValue(':Tdate',date("d-M-Y H:i"), PDO::PARAM_STR); 
                            $stmt->bindValue(':amt',20, PDO::PARAM_STR);  
                            $stmt->bindValue(':availableFund', $totalAvaib , PDO::PARAM_STR);  
                            $stmt->execute(); 
                            
                            
                            LvBal($_POST['refId'],20,1,"Referral Income",$cuurentId);    
                            
                            
                            $stmt=$conn->prepare("select * from UI__Rgst  where  cuurentId=:cuurentId  ");   
                            $stmt->bindValue(':cuurentId',  $_POST['refId'] , PDO::PARAM_STR);  
                            $stmt->execute();
                            $UI__Rgst = $stmt->fetch(PDO::FETCH_OBJ);     	
                            
                            
                            $stmt=$conn->prepare("insert into UI__Receipt(type,bank,mobile,email,amountPaid,Tid,status,Tdate,custId,cdId,Topay,TopayName) values(:type,:bank,:mobile,:email,:amountPaid,:Tid,:status,:Tdate,:custId,:cdId,:Topay,:TopayName)"); 
                            $stmt->bindValue(':type',   15 , PDO::PARAM_STR);   
                            $stmt->bindValue(':bank',  "Referral Balance" , PDO::PARAM_STR);   
                            $stmt->bindValue(':mobile', $UI__Rgst->phm, PDO::PARAM_STR);
                            $stmt->bindValue(':email', $UI__Rgst->eml , PDO::PARAM_STR); 
                            $stmt->bindValue(':amountPaid', 20  , PDO::PARAM_STR); 
                            $stmt->bindValue(':Tid', mt_rand(1234552,23555), PDO::PARAM_STR); 
                            $stmt->bindValue(':status', 1 , PDO::PARAM_STR); 
                            $stmt->bindValue(':Tdate', date("d-M-Y H:i") , PDO::PARAM_STR);      
                            $stmt->bindValue(':custId', $_POST['refId'], PDO::PARAM_STR);  
                            $stmt->bindValue(':cdId', $_SESSION['LastIntId']  , PDO::PARAM_STR); 
                            $stmt->bindValue(':Topay', $_POST['refId']  , PDO::PARAM_STR); 
                            $stmt->bindValue(':TopayName', $cuurentId  , PDO::PARAM_STR);     
                            $stmt->execute();    
                            unset($_SESSION['LastIntId']); 
                            
                        }
            
                          
                        $_SESSION['Regis'] = "account created successfully!!";
                        $_SESSION['Regis_name'] = $_POST['Fname'] ;
                        $_SESSION['Regis_ph'] = $_POST['ph_no'] ;
                        $_SESSION['Regis_email'] = $_POST['email'] ;
                        $_SESSION['Regis_cuurentId'] = $cuurentId;
                        $_SESSION['Regis_passwprd'] = $_POST['passwprd'];
                        $_SESSION['Regis_pincode'] = $_POST['pincode']; 
                        $_SESSION['Regis_CtMsg'] = "Your resgitation has done succfully";
                        $_SESSION['Regiset'] =  1;
                        
                        
                        
                          
                        $name = $_POST['Fname'];  
                        $mob = $_POST['ph_no'];
                        $Password = $_POST['passwprd'];
                        $msg = urlencode('Dear '.$name.' congratulation, your DailyPay account is successfully created, your user Login ID '.$cuurentId.' and Password '.$Password.', Thanks for DailyPay team'); 
                        $URL  = 'http://mobicomm.dove-sms.com//submitsms.jsp?user=DailyPAY&key=d92eabc025XX&mobile=+91'.$mob.'&message='.$msg.'&senderid=DAYPAY&accusage=1&entityid=1201169824017740929&tempid=1207169865158315415';
                          
                        $crl = curl_init();
                        curl_setopt($crl, CURLOPT_URL, $URL);
                        curl_setopt($crl, CURLOPT_FRESH_CONNECT, true);
                        curl_setopt($crl, CURLOPT_RETURNTRANSFER, true); 
                        $response = curl_exec($crl);
                        
                        
                    
                        $CMB = '('.$cuurentId.'), '.$_POST['Fname'];
                        $mob = $_POST['ph_no'];
                        $msg = urlencode('Hi '.$CMB.', don’t miss out on important updates for your Daily Pay profile and enjoy DailyPay utility services facility. Thanks for DailyPay team.'); 
                        $URL  = 'http://mobicomm.dove-sms.com//submitsms.jsp?user=DailyPAY&key=d92eabc025XX&mobile=+91'.$mob.'&message='.$msg.'&senderid=DAYPAY&accusage=1&entityid=1201169824017740929&tempid=1207169883942302039';
                          
                        $crl = curl_init();
                        curl_setopt($crl, CURLOPT_URL, $URL);
                        curl_setopt($crl, CURLOPT_FRESH_CONNECT, true);
                        curl_setopt($crl, CURLOPT_RETURNTRANSFER, true); 
                        $response = curl_exec($crl);
                         
                        unset($_SESSION['MobReg']); 
                        unset($_SESSION['OtpErr']); 
                        unset($_SESSION['MobOtp']);  
                        
                        }else 
                        { 
                            
                            if($datax < 1) 
                                $_SESSION['Regis_CtMsg'] =  "Please enter valid ref Id";   
                            else if($_POST['UserPin']!=4)
                                $_SESSION['Regis_CtMsg'] =  "Please enter 4 digit of login pin"; 
                            else
                                $_SESSION['Regis_CtMsg'] =  "Sorry your registration has not been done"; 
                                
                            $_SESSION['Regiset'] =  0;
                        }    
                        
                 }else
                 {
                      $_SESSION['OtpErr'] = "You have entered wrong OTP";
                 }
                 
                 header("Location:   ".ROOTPATH."/registration ");
				 die;
            }
            
			if(isset($_POST['submit']))
			{   
			    
		    
                $expd =  explode(" ",$_POST['Fname']);
                if(count($expd)>1) 
                {
				$CMB = mt_rand(1234552,23555);
				$mob = $_POST['ph_no'];
				$msg = urlencode(''.$CMB.' is your mobile verification one time password to proceed on DAILYPAY. It is valid for 5 minitus.'); 
				$URL  = 'http://mobicomm.dove-sms.com//submitsms.jsp?user=DailyPAY&key=d92eabc025XX&mobile=+91'.$mob.'&message='.$msg.'&senderid=DAYPAY&accusage=1&entityid=1201169824017740929&tempid=1207169907610629409';
				
				$crl = curl_init();
				curl_setopt($crl, CURLOPT_URL, $URL);
				curl_setopt($crl, CURLOPT_FRESH_CONNECT, true);
				curl_setopt($crl, CURLOPT_RETURNTRANSFER, true); 
				$response = curl_exec($crl);  

				$_SESSION['MobReg'] = $_POST;   
				$_SESSION['MobOtp'] = $CMB;
                }
                
				header("Location:   ".ROOTPATH."/registration ");
				die;  
			}
    
$_Refid = $_REQUEST['refId']; 
include_once("../view/registration/default.php");
?>