<?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("../reader.php");
$userNumber = $userNumber;

$saved=1;
if(!empty($_FILES['file']['tmp_name']) && !empty($_FILES['file']['size'])) {
    $allowedExtension =  array('txt','csv' ,'xls' ,'xlsx');
    $numberFileProperties = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
    if(!in_array($numberFileProperties, $allowedExtension)) {
        header("location: number.php?res=11");
        exit;
    }
    if(empty($_POST["categoryId"]) || empty($_POST["subCateIm"])) {
        header("location: number.php?res=2");
        exit;
    }
    if($numberFileProperties == "txt" ||  $numberFileProperties == "csv") {
        $tempNum="";
        $fp = fopen($_FILES['file']['tmp_name'], "r");
        if(!empty($fp)) {
            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);

                $queryInsert = "insert into `$userNumber` (name,number,userId,categoryId,sub_cat) values ";

                if(@$explodeNumber[1]) {
                    $name=$explodeNumber[0];
                    $number=$explodeNumber[1];
                } elseif(@!$explodeNumber[1]) {
                    $name="";
                    $number=$explodeNumber[0];
                }
                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;

                }
                if(!empty($number)) {
                    $queryInsert .= "('".mysqli_real_escape_string($conn, ($name))."','".mysqli_real_escape_string($conn, ($number))."','".$_SESSION["userId"]."','".$_POST["categoryId"]."','".$_POST["subCateIm"]."')";
                    $resultInsert = mysqli_query($conn, $queryInsert);
                }
            }
            fclose($fp);
            header("location: number.php?res=1");
            exit;
        } else {
            header("location: number.php?res=2");
            exit;
        }
    } elseif ($numberFileProperties == "xlsx") {
        $check=false;
        $getWorksheetName = array();
        $xlsx = new SimpleXLSX( $_FILES['file']['tmp_name'] );
        $getWorksheetName = $xlsx->getWorksheetName();
        $j=1;
        list($cols,) = $xlsx->dimension($j);
        $arrayRows=$xlsx->rows($j);
        if(!empty($arrayRows)) {
            for($z=0;$z<count($arrayRows);$z++)  {
                if($cols==2) {
					$name=iconv("UTF-8", "windows-1256", $arrayRows[$z][0]);
                    $number=$arrayRows[$z][1];
                }  elseif($cols==1)  {
                    $name="";
                    $number=$arrayRows[$z][0];
                }
                $queryInsert = "insert into `$userNumber` (name,number,userId,categoryId,sub_cat) values ";
                if(in_array(substr($number,0,6), array('+96650','+96651','+96653','+96654','+96655','+96656','+96658','+96659','+96657'))) {
                    $number =ltrim($number, "");
                } elseif(in_array(substr($value,0,3), array('050','051','053','054','055','056','058','059','057'))) {
                    $number =preg_replace('/0/', '966', $number, 1);
                } elseif(in_array(substr($number,0,2), array('50','51','53','54','55','56','58','59','57'))) {
                    $number ='966'.$number;
                }
                if(!empty($number)) {
                    $queryInsert .= "('".mysqli_real_escape_string($conn, ($name))."','".mysqli_real_escape_string($conn, ($number))."','".$_SESSION["userId"]."','".$_POST["categoryId"]."','".$_POST["subCateIm"]."');";
                    $resultInsert = mysqli_query($conn, $queryInsert);
                }
            }
            header("location: number.php?res=1");
            exit;
        }else {
            header("location: number.php?res=2");
            exit;
        }
    } elseif($numberFileProperties == "xls") {
        $d1="";
        $d2="";
        $data = new Spreadsheet_Excel_Reader($_FILES["file"]['tmp_name']);
        $dataExcel = $data->dump(true,true);
        $dataExcelE=explode("<m-RS-m>",$dataExcel);
        if(!empty($dataExcelE)) {
            for ($i = 1; $i <= count($dataExcelE); $i++) {
                $allData=array();
                if(!empty($data->sheets[0]['cells'][$i])) {
                    $allData = $data->sheets[0]['cells'][$i];
                    if(!empty($allData[1]) && !empty($allData[2])) {
                        $name=$allData[1];
						$name=iconv("windows-1256", "UTF-8", $allData[1]);
                        $number=$allData[2];
                    } else {
                        $name="";
                        $number=$allData[1];
                    }
                } else  {
                    $name="";
                    $number="";
                }
                $queryInsert = "insert into `$userNumber` (name,number,userId,categoryId,sub_cat) values ";
                if(in_array(substr($number,0,6), array('+96650','+96651','+96653','+96654','+96655','+96656','+96658','+96659','+96657','+96657'))) {
                    $number =ltrim($number, "");
                } elseif(in_array(substr($value,0,3), array('050','051','053','054','055','056','058','059','057','057'))) {
                    $number =preg_replace('/0/', '966', $number, 1);
                } elseif(in_array(substr($number,0,2), array('50','51','53','54','55','56','58','59','57','57'))) {
                    $number ='966'.$number;
                }
                if(!empty($number)) {
                    $queryInsert .= "('".mysqli_real_escape_string($conn, ($name))."','".mysqli_real_escape_string($conn, ($number))."','".$_SESSION["userId"]."','".$_POST["categoryId"]."','".$_POST["subCateIm"]."');";
                    $resultInsert = mysqli_query($conn, $queryInsert);
                }
            }
            header("location: number.php?res=1");
            exit;
        } else {
            header("location: number.php?res=2");
            exit;
        }
    }
} else {
	header("location:number.php?res=2");
	exit;
}
ob_end_flush();  
?>
