<?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");
$name = "";
$number = "";
$categoryId = "";
if($_POST['categoryId']=="X")
{
	if($_POST["category1"]==1)
		$categoryId =1;
	if($_POST["category2"]==1)
		$categoryId =2;
	if($_POST["category3"]==1)
		$categoryId =3;	
		
	if($_POST["category1"]==2)
		$name =1;
	if($_POST["category2"]==2)
		$name =2;
	if($_POST["category3"]==2)
		$name =3;	
	
	if($_POST["category1"]==3)
		$number =1;
	if($_POST["category2"]==3)
		$number =2;
	if($_POST["category3"]==3)
		$number =3;
}
else
{
	$categoryId = $_POST['categoryId'];
	if($_POST["category2"]==2)
		$name =1;
	if($_POST["category3"]==2)
		$name =2;	
	
	if($_POST["category2"]==3)
		$number =1;
	if($_POST["category3"]==3)
		$number =2;
	if(empty($name))
		$number = 1;		
}
if(!empty($_FILES['file']['size']))
{
	if(strrchr(@$_FILES["file"]['name'],".")==".zip")
	{
		$pathFileZip = "fileExtract/extractFile.zip";
		move_uploaded_file($_FILES['file']['tmp_name'],$pathFileZip);
		$zip = new ZipArchive; 
		if($zip->open("fileExtract/extractFile.zip"))
		{ 
			$path = "fileExtract/";
			if(!empty($zip->numFiles))
			{
				$fileNameFile = $zip->getNameIndex(0);
				$fileExtanchin = strrchr($zip->getNameIndex(0),".");
				$pathNameFile2 = $path."fileExtractFile".$fileExtanchin;
				$zip->extractTo($path);
				$zip->close();
				if(strrchr($fileNameFile,".")==".xls")
				{
					$d1="";
					$d2="";
					$data = new Spreadsheet_Excel_Reader("fileExtract/".$fileNameFile);
					$checkFile = $data->read("fileExtract/".$fileNameFile);
					if($checkFile==1)
					{
						header("location:number.php?res=20");
						exit();
					}
					$dataExcel = $data->dump(true,true); 
					$dataExcelE=explode("<m-RS-m>",$dataExcel);
					$categoryIdUser = $categoryId;
					$nameUser = $name;
					$numberUser = $number;
					
					for ($i = 1; $i < count($dataExcelE); $i++)
					{
						if($data->sheets[0]['numRows']==0) {
							unlink($pathFileZip);
							unlink("fileExtract/".$fileNameFile);
							header("location:number.php?res=20");
							exit();
						}
						$allData = $data->sheets[0]['cells'][$i];
						if(!empty($numberUser))
							$number=$allData[$numberUser];
						else
						  continue;
						if(!empty($nameUser))
							$name=$allData[$nameUser];
						if($_POST['categoryId']=="X") {
							if(!empty($categoryIdUser))
								$category=$allData[$categoryIdUser];

							$querySelect = "SELECT `id` FROM `user_category` WHERE `name`='$category' AND `userId`=$_SESSION[userId]";
							$resultSelect = mysqli_query($conn, $querySelect);
							$numSelect = mysqli_num_rows($resultSelect);		

							if(empty($numSelect) && !empty($category)) {
								$queryInsert = "INSERT INTO `user_category` (name,userId) values ";
								$queryInsert .= "('".$category."',$_SESSION[userId])";
								$resultInsert = mysqli_query($conn, $queryInsert);
								$categoryId = mysqli_insert_id($conn);
							} else {
								$rowSelect = mysqli_fetch_assoc($resultSelect);
								$categoryId = $rowSelect['id'];
							}
						}
						if(strlen($number)!=12)
						{
							if(substr($number,0,2) == "05")
							{
								$number  =substr($number, 1, strlen($number));
								$number = "966".$number;
							}
							elseif(substr($number,0,1) == "5")
								$number = "966".$number;
						}
						$queryInsert = "INSERT INTO user_number (`name`,`number`,`userId`,`categoryId`,`city`) values ";
						$queryInsert .= "('".@$name."','".@$number."','".$_SESSION["userId"]."','".$categoryId ."','0')";
						$resultInsert = mysqli_query($conn, $queryInsert);
						
					}		
				}
				elseif(strrchr($fileNameFile,".")==".xlsx")
				{
					$getWorksheetName = array();
					$xlsx = new SimpleXLSX("fileExtract/".$fileNameFile);
					$getWorksheetName = $xlsx->getWorksheetName();
					$j=1;
					list($cols,) = $xlsx->dimension($j);
					 $arrayRows=$xlsx->rows($j);
					 $categoryIdUser = $categoryId;
					$nameUser = $name;
					$numberUser = $number;

					if(!empty($arrayRows))
					 {
						for($z=0;$z<count($arrayRows);$z++)


						{
							if(!empty($nameUser))
								$name=iconv("UTF-8", "windows-1256",$arrayRows[$z][$nameUser-1]);
							if(!empty($numberUser))
								$number=$arrayRows[$z][$numberUser-1];
							else
							  continue;
						
                            if($_POST['categoryId']=="X")
                            {
                                if(!empty($categoryIdUser))
                                    $category=$arrayRows[$z][$categoryIdUser-1];
                                $category=iconv("UTF-8", "windows-1256", $category);
                                $querySelect = "SELECT `id` FROM `user_category` WHERE `name`='$category' AND `userId`='".$_SESSION["userId"]."'";
                                $resultSelect = mysqli_query($conn, $querySelect);
                                $numSelect = mysqli_num_rows($resultSelect);
                                if(empty($numSelect) && !empty($category))
                                {
                                        $queryInsert = "INSERT INTO `user_category` (name,userId) values ";
                                        $queryInsert .= "('".$category."','".$_SESSION["userId"]."')";
                                        $resultInsert = mysqli_query($conn, $queryInsert);
                                        $categoryId = mysqli_insert_id($conn);
                                }
                                else
                                {
                                    $rowSelect = mysqli_fetch_assoc($resultSelect);
                                    $categoryId = $rowSelect['id'];
                                }
								
                            }

                            if(!empty($number))
                            {
								if(strlen($number)!=12)
								{
									if(substr($number,0,2) == "05")
									{
										$number  =substr($number, 1, strlen($number));
										$number = "966".$number;
									}
									elseif(substr($number,0,1) == "5")
										$number = "966".$number;
								}
                                $queryInsert = "INSERT INTO user_number (`name`,`number`,`userId`,`categoryId`,`city`) values ";
                                $queryInsert .= "('".@$name."','".@$number."','".$_SESSION["userId"]."','".$categoryId."','')";
                                $resultInsert = mysqli_query($conn, $queryInsert);
                            }
							
					    }
					 }
					 else
					 {
						header("location:number.php?res=20");
						exit(); 
					 }
				}
				unlink($pathFileZip);
				unlink("fileExtract/".$fileNameFile);	
				header("location:number.php?res=1");
				exit();	
			}
		}
		unlink($pathFileZip);
		unlink("fileExtract/".$fileNameFile);
		header("location:number.php?res=47");
		exit();
	}
	elseif(strrchr(@$_FILES["file"]['name'],".")==".xls" )
	{
		$d1="";
		$d2="";
		$data = new Spreadsheet_Excel_Reader($_FILES["file"]['tmp_name']);
		$checkFile = $data->read($_FILES["file"]['tmp_name']);
		if($checkFile==1)
		{
			header("location:number.php?res=20");
			exit();
		}
		$dataExcel = $data->dump(true,true); 
		$dataExcelE=explode("<m-RS-m>",$dataExcel);
		$categoryIdUser = $categoryId;
		$nameUser = $name;
		$numberUser = $number;
		for ($i = 1; $i < count($dataExcelE); $i++)
		{
			if($data->sheets[0]['numRows']==0)
			{
				header("location:number.php?res=20");
				exit();
			}
			$allData = $data->sheets[0]['cells'][$i];
				if(!empty($nameUser))
					$name=$allData[$nameUser];
				if(!empty($numberUser))
					$number=$allData[$numberUser];
				else
					continue;

			if($_POST['categoryId']=="X")
			{
				if(!empty($categoryIdUser))
					$category=$allData[$categoryIdUser];
				else
					continue;
				$querySelect = "SELECT `id` FROM `user_category` WHERE `name`='$category' AND `userId`='".$_SESSION["userId"]."'";
				$resultSelect = mysqli_query($conn, $querySelect);
				$numSelect = mysqli_num_rows($resultSelect);
				if(empty($numSelect) && !empty($category))
				{
						$queryInsert = "INSERT INTO `user_category` (name,userId) values ";
						$queryInsert .= "('".$category."','".$_SESSION["userId"]."')";
						$resultInsert = mysqli_query($conn, $queryInsert);
						$categoryId = mysqli_insert_id($conn);
				}
				else
				{
					$rowSelect = mysqli_fetch_assoc($resultSelect);
					$categoryId = $rowSelect['id'];
				}
			}
			if(!empty($number))
			{
				if(strlen($number)!=12)
				{
					if(substr($number,0,2) == "05")
					{
						$number  =substr($number, 1, strlen($number));
						$number = "966".$number;
					}
					elseif(substr($number,0,1) == "5")
						$number = "966".$number;
				}
				$queryInsert = "INSERT INTO user_number (`name`,`number`,`userId`,`categoryId`,`city`) values ";
				$queryInsert .= "('".@$name."','".@$number."','".$_SESSION["userId"]."','".$categoryId ."','')";
				$resultInsert = mysqli_query($conn, $queryInsert);
			}
		}
		header("location:number.php?res=1");
		exit();
	}
	elseif(strrchr(@$_FILES["file"]['name'],".")==".xlsx" )
	{
		$getWorksheetName = array();
		$xlsx = new SimpleXLSX( $_FILES['file']['tmp_name'] );
		$getWorksheetName = $xlsx->getWorksheetName();
			$j=1;
			list($cols,) = $xlsx->dimension($j);
			 $arrayRows=$xlsx->rows($j);
			 $categoryIdUser = $categoryId;
			$nameUser = $name;
			$numberUser = $number;

			 if(!empty($arrayRows))
			for($z=0;$z<count($arrayRows);$z++)
			{
				
				if(!empty($nameUser) && !empty($arrayRows[$z][$nameUser-1]))
					$name=iconv("UTF-8", "windows-1256",$arrayRows[$z][$nameUser-1]);
				else
					$name="";
				if(!empty($numberUser))
					$number=$arrayRows[$z][$numberUser-1];
				else
					continue;
					
			

				if($_POST['categoryId']=="X")
				{
					if(!empty($categoryIdUser))
						if(!empty($arrayRows[$z][$categoryIdUser-1]))
							$category=$arrayRows[$z][$categoryIdUser-1];
						else
							continue;
					else
						continue;
					$category=iconv("UTF-8", "windows-1256", $category);
					$querySelect = "SELECT `id` FROM `user_category` WHERE `name`='$category' AND `userId`='".$_SESSION["userId"]."'";
					$resultSelect = mysqli_query($conn, $querySelect);
					$numSelect = mysqli_num_rows($resultSelect);
					if(empty($numSelect) && !empty($category))
					{
							$queryInsert = "INSERT INTO `user_category` (name,userId) values ";
							$queryInsert .= "('".$category."','".$_SESSION["userId"]."')";
							$resultInsert = mysqli_query($conn, $queryInsert);
							$categoryId = mysqli_insert_id($conn);
					}
					else
					{
						$rowSelect = mysqli_fetch_assoc($resultSelect);
						$categoryId = $rowSelect['id'];
					}
				}
				if(!empty($number))
				{
					if(strlen($number)!=12)
					{
						if(substr($number,0,2) == "05")
						{
							$number  =substr($number, 1, strlen($number));
							$number = "966".$number;
						}
						elseif(substr($number,0,1) == "5")
							$number = "966".$number;
					}
					$queryInsert = "INSERT INTO user_number (`name`,`number`,`userId`,`categoryId`,`city`) values ";
					$queryInsert .= "('".@$name."','".@$number."','".$_SESSION["userId"]."','".$categoryId."','')";
					$resultInsert = mysqli_query($conn, $queryInsert);
				}
			}

		header("location:number.php?res=1");
		exit();
	}
	else
	{
		header("location:number.php?res=11");
		exit();
	}
}
else
{
	header("location:number.php?res=20");
	exit();	
}

ob_end_flush();  
?>
