<?php
/**
 * Processing Forget password form data:
 * check data then generate new password and send it through SMPT Email
 *
 * PHP version 7.3
 *
 * @package        SMS
 * @version        Release 1.0.0
 * @create_by      Mohammad aljaouni
 * @create_date    10-08-2020
 * @edited_by      Mohammad aljaouni
 * @last_edit_date 10-08-2020
 * @todo           Nothing
 */
ob_start();
session_start();
$chain = "AWdrh78y892Djk*^523*%$#ghgHGHia&%$*)AsfdsLJnLaad";
include("validateLogin.php");
include("includeInformation.php");
include_once("../readerExcel.php");
header( "Content-type: text/plain");

if(!empty($_FILES["numbers"]["tmp_name"])) {
	$imageFileType = strtolower(pathinfo($_FILES["numbers"]["name"],PATHINFO_EXTENSION));
		$allowedExtension =  array('txt','csv','xls','xlsx');
	if(!in_array($imageFileType, $allowedExtension)) {
		header("location: delete.php?res=27");
		exit();
	}

	$queryDelete=$numbers=$dataExcelE=array();
	$d1=$d2="";
	if($imageFileType == "xls")
	{
		$data = new Spreadsheet_Excel_Reader($_FILES["numbers"]['tmp_name']);
		$dataExcel = $data->dump(true,true); 
		$dataExcelE=explode("<m-RS-m>",$dataExcel);
		if($data->sheets[0]['numRows']==0){
			header("Location: delete.php?res=9");
			exit();
		}
		foreach($dataExcelE as $key=>$value){
			if(!empty($data->sheets[0]['cells'][$key])){
				$allData = $data->sheets[0]['cells'][$key];
				if(!empty($allData[1]) && is_numeric($allData[1])){
							$numbers[]="'".$allData[1]."'";			
				}	
			}
		}
	}elseif($imageFileType == "xlsx") {
		$data = new SimpleXLSX( $_FILES['numbers']['tmp_name'] );
		$dataExcel = $data->getWorksheetName();
		$dataExcel= $data->dimension(1);
		$dataExcelE=$data->rows(1);
		if(empty($dataExcelE)){
			header("Location: delete.php?res=9");
			exit();
		}
		foreach($dataExcelE as $key=>$value){
			if(!empty($value)){
				if(!empty($value[0]) && is_numeric($value[0])){
					$numbers[]="'".$value[0]."'";			
				}	
			}
		}
	}elseif($imageFileType == "csv" || $imageFileType == "txt") {
		$tempNum=$numbers="";
		$fp = fopen($_FILES['numbers']['tmp_name'], "r");
		while (!feof($fp)) {
			$tempNum = fgets($fp, 100);
			$tempNum=str_replace("\r","",$tempNum);
			$tempNum=str_replace("\n","",$tempNum);
			$key = strpos($tempNum, ',');
			if($key === false)		
				$explodeNumber = explode(";", $tempNum);
			else
				$explodeNumber = explode(",", $tempNum);
			if(!empty($explodeNumber[0])){
				$explodeNumber[0]=trim($explodeNumber[0]);
				$explodeNumber[0]=preg_replace('/[^0-9]/', '',$explodeNumber[0]);
				if(!empty($explodeNumber[0]) && is_numeric($explodeNumber[0]))
					$numbers.=$explodeNumber[0].",";
			}
		}
		fclose($fp);
		$numbers=explode(",",$numbers);
		$numbers=array_filter($numbers);
	}
	if(empty($numbers)){
		header("Location: delete.php?res=9");
		exit();
	}
	$count=count($numbers);
	$last_num=end($numbers);
	$numbers=array_chunk($numbers,500);
	foreach($numbers as $val){
		$val=implode($val,",");
		$val=rtrim($val,",");
		$queryDelete[]="DELETE FROM `user_number` WHERE `number` IN (".$val.");";
	}
	if($count>5000){
		$queryDelete=array_chunk($queryDelete,10);
		foreach($queryDelete as $key=>$array){
			$queryDelete=implode($array,"\r\n");
			$fileName="numbers_".$key."-".$count."_".date("d-m-YTh-i-s").".sql";
			$myfile = fopen($fileName, "wb");
			fwrite($myfile, $queryDelete);
			fclose($myfile);	
		}
	}else{
		$queryDelete=implode($queryDelete,"\r\n");
		$fileName="numbers_".$count."_".date("d-m-YTh-i-s").".sql";
		$myfile = fopen($fileName, "wb");
		fwrite($myfile, $queryDelete);
		fclose($myfile);
	}
	mysqli_query($conn, "UPDATE `cpanel` SET `delete`=1,`last_delete_number`=".$last_num);
	header("location: delete.php?res=111");
	exit();
}else{
	header("Location: delete.php?res=112");
	exit();	
}
ob_end_flush();  
?>
