0xV3NOMx
Linux ip-172-26-7-228 5.4.0-1103-aws #111~18.04.1-Ubuntu SMP Tue May 23 20:04:10 UTC 2023 x86_64



Your IP : 18.119.235.107


Current Path : /proc/thread-self/root/proc/thread-self/root/var/www/oasis/src/
Upload File :
Current File : //proc/thread-self/root/proc/thread-self/root/var/www/oasis/src/admuploadexsheet.php

<?php

function replaceChars($name)
{
	$spl_char = array("*","/","~","!","@","#","$","%","^","&",":",";","?","/","\\","_","-","'"," ","");
	$link_name = strtolower(str_replace($spl_char,"_",$name));
	return str_ireplace("__","_",$link_name);
}


function admuploadexeclsheet($aobj_context)
{
	$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC); 
	session_start();
	require_once dirname(__FILE__) . '/../PHPExcel/Classes/PHPExcel/IOFactory.php';

	require_once('sendsmsapi.php');
	$db = $aobj_context->mobj_data["db"];
	$date = date("dmYhis");
	$collcode = $_SESSION['collcode'];
	$file_name=replaceChars(basename($_FILES['admuploadsheet']['name']));
	$path_arr=pathinfo($_FILES['admuploadsheet']['name']);
	$file_ext=$path_arr['extension'];
	//var_dump($file_ext);
	$folderpath = $aobj_context->main_src."/$db/admupload/"; 
	$temp_path = $aobj_context->main_src."/$db/admupload/";
	if(!file_exists($temp_path))
	{
		mkdir($temp_path);
	}

	$file_path="/{$collcode}_{$date}_{$file_name}";
	$temp_path.=$file_path;
	
	if($collcode == '')
	{
		$data = "Please login and try again";
		echo $aobj_context->mobj_output->ToJSONEnvelope($data,-1,"failure"); 
		return;
	}	
	
	if(isset($_FILES['admuploadsheet']['name']) && $_FILES['admuploadsheet']['name'] != '')  
	{ 
		$size=($_FILES['admuploadsheet']['size']/1024);
		
		if($size>1024)
		{
			$data="Please Upload file of size less than 1 MB";
			echo $aobj_context->mobj_output->ToJSONEnvelope($data,-2,"failure"); 
			return;
		}
		
		$ext = pathinfo($_FILES['admuploadsheet']['name'], PATHINFO_EXTENSION);

		if($ext != 'xlsx')
		{
			$data="Please Upload file xlsx format only";
			echo $aobj_context->mobj_output->ToJSONEnvelope($data,-2,"failure"); 
			return;
		}

		if(move_uploaded_file($_FILES['admuploadsheet']['tmp_name'],$temp_path))
		{
			$data="admupload{$file_path}";

			$query = "insert into admfile(fcollcode,ffilename,fuploadtime) 
			value ('{$collcode}','{$data}',now())";
			$results = $aobj_context->mobj_db->Execute($query);
			
			if(!$results)
			{
				$data = "Upload failure. Error inserting uploaded file";
				echo $aobj_context->mobj_output->ToJSONEnvelope($data,-1,"failure"); 
				return;
			}

			if(!file_exists($temp_path))
			{
				$data = "Upload failure. Upload file does not exist";
				echo $aobj_context->mobj_output->ToJSONEnvelope($data,-1,"failure"); 
				return;
			}
			$inputFileName = $temp_path;
			$inputFileType = 'Excel2007';
			///////////////////// Code To convert xlsx to csv \\\\\\\\\\\\\
			$objReader = PHPExcel_IOFactory::createReader($inputFileType);
			$objPHPExcelReader = $objReader->load($inputFileName);

			$loadedSheetNames = $objPHPExcelReader->getSheetNames();

			$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcelReader, 'CSV');
			$i = 0;
			if($db == 'rcub')
			{
				foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) 
				{
					if($i != 0)
						break;
					
					$objWriter->setSheetIndex($sheetIndex);
					
					$csvFile = $folderpath.$date.$loadedSheetName.'.csv';
					$objWriter->setUseBOM(true);
					$objWriter->save($csvFile);
					$query = "LOAD DATA LOCAL INFILE '{$csvFile}'
					INTO TABLE tmpcollstud
					FIELDS TERMINATED BY \",\"
					OPTIONALLY ENCLOSED BY '\"' 
					LINES TERMINATED BY \"\n\" 
					IGNORE 1 LINES
					(@col1,@col2,@col3,@col4,@col5,@col6,@col7,@col8) set
					fslno=@col1, fcollcode='{$collcode}', fdegree=@col3,fcombcode =@col4,
					fname=@col5, fmobileno=@col6, femail=@col7, fcaste=@col8";
					//var_dump($query);    
					$results = $aobj_context->mobj_db->Execute($query);
					//var_dump($results);
					$rows_affected = $aobj_context->mobj_db->affected_rows();
					// var_dump($rows_affected);
					//return;
					if(!$results)
					{
						$data = "Upload failure. Error while loading excel data to database";
						echo $aobj_context->mobj_output->ToJSONEnvelope($data,-1,"failure"); 
						return;
					}
					// Delete All the blank values from table
					$query = "delete from tmpcollstud where fcollcode = '' or fdegree = '' or fname = '' 
					or fdegree = 'Degree'";
					$results = $aobj_context->mobj_db->Execute($query);
					// For second sheet 
					if($i > 0)
					{
						// Check for the data in second sheet
						$query = "select count(*) as fcnt from tmpcollstud 
						where fcollcode = '{$collcode}' and ifnull(fimported,'') <> 'T'";
						
						$results = $aobj_context->mobj_db->GetRow($query);
						
						if($results['fcnt'] == 0)
						{
							break;
						}
					}
	
	
					$query = "select distinct fdegree from tmpcollstud 
					where fcollcode = '{$collcode}' and ifnull(fimported,'') <> 'T' 
					and fdegree not in (select distinct fdegree 
					from mascomb where ifnull(fdeleted,'') <> 'T')";
							
					$results = $aobj_context->mobj_db->GetAll($query);
							
					if(count($results) > 0)
					{
						$query = "delete from tmpcollstud where ifnull(fimported,'') <> 'T' 
						and fcollcode = '{$collcode}'";
						// var_dump($query);
						$results = $aobj_context->mobj_db->GetAll($query);
	
						$data = "Upload failure. Invalid degree codes / combination code";
						echo $aobj_context->mobj_output->ToJSONEnvelope($data,-1,"failure"); 
						return;
					}
	
					$query = "update tmpcollstud set fimported = 'F' where ifnull(fimported,'') = ''";
					$results = $aobj_context->mobj_db->Execute($query);
					
					// insert records with no mobile number to collstudnb
					$query = "insert into collstudnb(fslno, fcollcode, fdegree,fcombcode, fmobileno, femail, fname, fcaste) 
					select fslno, fcollcode, fdegree, fcombcode, fmobileno, femail, fname, fcaste 
					from tmpcollstud where replace(ifnull(tmpcollstud.fmobileno,''),' ','') = '' 
					and fcollcode = '{$collcode}' and ifnull(fimported,'') <> 'T'";
					$results = $aobj_context->mobj_db->Execute($query);
					// delete records in temp table with no mobilenumber
					$query = "delete from tmpcollstud where replace(ifnull(tmpcollstud.fmobileno,''),' ','') = '' 
					and fcollcode = '{$collcode}' and ifnull(fimported,'') <> 'T'";
					$results = $aobj_context->mobj_db->Execute($query);
	
					// check for duplicates in the file uploading
					$query = "insert into tmpcollstuddup 
					select fmobileno, count(fmobileno) as fcnt from tmpcollstud 
					where fcollcode = '{$collcode}' and ifnull(fimported,'') <> 'T' 
					group by fmobileno having count(fmobileno) > 1";
					$results = $aobj_context->mobj_db->Execute($query);
	
					// identify duplicates in file uploading
					$query = "update tmpcollstud a, tmpcollstuddup b set a.fdeleted = 'X' 
					where a.fmobileno = b.fmobileno and a.fcollcode = '{$collcode}' 
					and ifnull(a.fimported,'') <> 'T'";
					$results = $aobj_context->mobj_db->Execute($query);
	
					// insert duplicate records of that college to collstuddup
					$query = "insert into collstuddup 
					select * from tmpcollstud where fdeleted = 'X' and fcollcode = '{$collcode}' 
					and ifnull(fimported,'') <> 'T'";
					$results = $aobj_context->mobj_db->Execute($query);
					// delete duplicates in temp table
					$query = "delete from tmpcollstud where fdeleted = 'X' and fcollcode = '{$collcode}' 
					and ifnull(fimported,'') <> 'T'";
					$results = $aobj_context->mobj_db->Execute($query);
	
					// identify dulicates with previous inserted records (other than currently uploading college)
					$query = "update tmpcollstud, collstud set tmpcollstud.fdeleted = 'X' 
					where collstud.fmobileno = tmpcollstud.fmobileno and collstud.fcollcode <> tmpcollstud.fcollcode 
					and replace(ifnull(tmpcollstud.fmobileno,''),' ','') <> '' 
					and tmpcollstud.fcollcode = '{$collcode}' and ifnull(tmpcollstud.fimported,'') <> 'T'";
					$results = $aobj_context->mobj_db->Execute($query);
					// insert duplicates to collstuddup
					$query = "insert into collstuddup select * from tmpcollstud where fdeleted = 'X' and fcollcode = '{$collcode}' and ifnull(fimported,'') <> 'T'";
					$results = $aobj_context->mobj_db->Execute($query);
	
					// identify duplicate records (overall)
					$query = "update tmpcollstud, collstud set tmpcollstud.fdeleted = 'X' 
					where collstud.fmobileno = tmpcollstud.fmobileno 
					and replace(ifnull(tmpcollstud.fmobileno,''),' ','') <> '' and tmpcollstud.fcollcode = '{$collcode}' 
					and ifnull(tmpcollstud.fimported,'') <> 'T'";
					$results = $aobj_context->mobj_db->Execute($query);
	
					// delete duplicate records in temp table
					$query = "delete from tmpcollstud where fdeleted = 'X' and fcollcode = '{$collcode}' 
					and ifnull(fimported,'') <> 'T'";
					$results = $aobj_context->mobj_db->Execute($query);
	
					$query = "select a.fcollcode, a.fdegree, a.fcombcode, count(fmobileno) as fcount, 
					ifnull(b.fcombcode,'') as fcombcode, ifnull(b.fintake,0) as fintake from tmpcollstud a 
					left join collcomb b on a.fcollcode = b.fcollcode and a.fdegree = b.fdegree 
					and a.fcombcode = b.fcombcode where a.fcollcode = '{$collcode}' 
					group by a.fcollcode,a.fdegree,a.fcombcode
					having fcount >= ifnull(fintake,0)";
					$results = $aobj_context->mobj_db->GetAll($query);
					//var_dump($query);
					if(count($results) > 0)
					{
						$query = "delete from tmpcollstud where ifnull(fimported,'') <> 'T' 
						and fcollcode = '{$collcode}'";
						// var_dump($query);
						$results1 = $aobj_context->mobj_db->GetAll($query);
	
						$data = "Upload failure. Intake Exceeded for degree ".$results[0]['fdegree']." Combination ".$results[0]['fcombcode'];
						echo $aobj_context->mobj_output->ToJSONEnvelope($data,-1,"failure"); 
						return;
					}
	
					// insert remaining records to collstud
					$query = "insert into collstud(fslno, fcollcode, fdegree, fcombcode, fmobileno, femail, fname,fcaste, fimpdate) 
					select fslno, fcollcode, fdegree, fcombcode, fmobileno, femail, fname,fcaste, fimpdate from tmpcollstud 
					where fcollcode = '{$collcode}' and ifnull(fimported,'') <> 'T'";
					$results = $aobj_context->mobj_db->Execute($query);
					// Update records as imported 
					$query = "update tmpcollstud set fimported = 'T' where 
					fcollcode = '{$collcode}' and ifnull(fimported,'') <> 'T'";
					$results = $aobj_context->mobj_db->Execute($query);
					$i++;
				}
			}else
			{
				foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) 
				{
					if($i != 0)
						break;
					
					$objWriter->setSheetIndex($sheetIndex);
					
					$csvFile = $folderpath.$date.$loadedSheetName.'.csv';
					$objWriter->setUseBOM(true);
					$objWriter->save($csvFile);
					$query = "LOAD DATA LOCAL INFILE '{$csvFile}'
					INTO TABLE tmpcollstud
					FIELDS TERMINATED BY \",\"
					OPTIONALLY ENCLOSED BY '\"' 
					LINES TERMINATED BY \"\n\" 
					IGNORE 1 LINES
					(@col1,@col2,@col3,@col4,@col5,@col6) set
		        fslno=@col1, fcollcode='{$collcode}', fdegree=@col3,
				fname=@col4, fmobileno=@col5, femail=@col6";

				// 	$query = "LOAD DATA LOCAL INFILE '{$csvFile}'
		        // INTO TABLE tmpcollstud
		        // FIELDS TERMINATED BY ','
		        // OPTIONALLY ENCLOSED BY '\"' 
		        // LINES TERMINATED BY '\r\n' 
		        // IGNORE 1 LINES
		        // (@col1,@col2,@col3,@col4,@col5,@col6) set
		        // fslno=@col1, fcollcode='{$collcode}', fdegree=@col3,
				// fname=@col4, fmobileno=@col5, femail=@col6";
				
					//var_dump($query);    
					$results = $aobj_context->mobj_db->Execute($query);
					//var_dump($results);
					$rows_affected = $aobj_context->mobj_db->affected_rows();
					// var_dump($rows_affected);
					//return;
					if(!$results)
					{
						$data = "Upload failure. Error while loading excel data to database";
						echo $aobj_context->mobj_output->ToJSONEnvelope($data,-1,"failure"); 
						return;
					}
					// Delete All the blank values from table
					$query = "delete from tmpcollstud where fcollcode = '' or fdegree = '' or fname = '' 
					or fdegree = 'Degree'";
					$results = $aobj_context->mobj_db->Execute($query);
					// For second sheet 
					if($i > 0)
					{
						// Check for the data in second sheet
						$query = "select count(*) as fcnt from tmpcollstud 
						where fcollcode = '{$collcode}' and ifnull(fimported,'') <> 'T'";
						
						$results = $aobj_context->mobj_db->GetRow($query);
						
						if($results['fcnt'] == 0)
						{
							break;
						}
					}


					$query = "select distinct fdegree from tmpcollstud 
					where fcollcode = '{$collcode}' and ifnull(fimported,'') <> 'T' 
					and fdegree not in (select distinct fdegree 
					from mascomb where ifnull(fdeleted,'') <> 'T')";
					// var_dump($query);
					//die();
					$results = $aobj_context->mobj_db->GetAll($query);
							
					if(count($results) > 0)
					{
						$query = "delete from tmpcollstud where ifnull(fimported,'') <> 'T' 
						and fcollcode = '{$collcode}'";
						// var_dump($query);
						$results = $aobj_context->mobj_db->GetAll($query);

						$data = "Upload failure. Invalid degree codes / combination code";
						echo $aobj_context->mobj_output->ToJSONEnvelope($data,-1,"failure"); 
						return;
					}

					$query = "update tmpcollstud set fimported = 'F' where ifnull(fimported,'') = ''";
					$results = $aobj_context->mobj_db->Execute($query);
					
					// insert records with no mobile number to collstudnb
					$query = "insert into collstudnb(fslno, fcollcode, fdegree,fcombcode, fmobileno, femail, fname, fcaste) 
					select fslno, fcollcode, fdegree, fcombcode, fmobileno, femail, fname, fcaste 
					from tmpcollstud where replace(ifnull(tmpcollstud.fmobileno,''),' ','') = '' 
					and fcollcode = '{$collcode}' and ifnull(fimported,'') <> 'T'";
					$results = $aobj_context->mobj_db->Execute($query);
					// delete records in temp table with no mobilenumber
					$query = "delete from tmpcollstud where replace(ifnull(tmpcollstud.fmobileno,''),' ','') = '' 
					and fcollcode = '{$collcode}' and ifnull(fimported,'') <> 'T'";
					$results = $aobj_context->mobj_db->Execute($query);

					// check for duplicates in the file uploading
					$query = "insert into tmpcollstuddup 
					select fmobileno, count(fmobileno) as fcnt from tmpcollstud 
					where fcollcode = '{$collcode}' and ifnull(fimported,'') <> 'T' 
					group by fmobileno having count(fmobileno) > 1";
					$results = $aobj_context->mobj_db->Execute($query);

					// identify duplicates in file uploading
					$query = "update tmpcollstud a, tmpcollstuddup b set a.fdeleted = 'X' 
					where a.fmobileno = b.fmobileno and a.fcollcode = '{$collcode}' 
					and ifnull(a.fimported,'') <> 'T'";
					$results = $aobj_context->mobj_db->Execute($query);

					// insert duplicate records of that college to collstuddup
					$query = "insert into collstuddup 
					select * from tmpcollstud where fdeleted = 'X' and fcollcode = '{$collcode}' 
					and ifnull(fimported,'') <> 'T'";
					$results = $aobj_context->mobj_db->Execute($query);
					// delete duplicates in temp table
					$query = "delete from tmpcollstud where fdeleted = 'X' and fcollcode = '{$collcode}' 
					and ifnull(fimported,'') <> 'T'";
					$results = $aobj_context->mobj_db->Execute($query);

					// identify dulicates with previous inserted records (other than currently uploading college)
					$query = "update tmpcollstud, collstud set tmpcollstud.fdeleted = 'X' 
					where collstud.fmobileno = tmpcollstud.fmobileno and collstud.fcollcode <> tmpcollstud.fcollcode 
					and replace(ifnull(tmpcollstud.fmobileno,''),' ','') <> '' 
					and tmpcollstud.fcollcode = '{$collcode}' and ifnull(tmpcollstud.fimported,'') <> 'T'";
					$results = $aobj_context->mobj_db->Execute($query);
					// insert duplicates to collstuddup
					$query = "insert into collstuddup select * from tmpcollstud where fdeleted = 'X' and fcollcode = '{$collcode}' and ifnull(fimported,'') <> 'T'";
					$results = $aobj_context->mobj_db->Execute($query);

					// identify duplicate records (overall)
					$query = "update tmpcollstud, collstud set tmpcollstud.fdeleted = 'X' 
					where collstud.fmobileno = tmpcollstud.fmobileno 
					and replace(ifnull(tmpcollstud.fmobileno,''),' ','') <> '' and tmpcollstud.fcollcode = '{$collcode}' 
					and ifnull(tmpcollstud.fimported,'') <> 'T'";
					$results = $aobj_context->mobj_db->Execute($query);

					// delete duplicate records in temp table
					$query = "delete from tmpcollstud where fdeleted = 'X' and fcollcode = '{$collcode}' 
					and ifnull(fimported,'') <> 'T'";
					$results = $aobj_context->mobj_db->Execute($query);

					// insert remaining records to collstud
					$query = "insert into collstud(fslno, fcollcode, fdegree, fcombcode, fmobileno, femail, fname,fcaste, fimpdate) 
					select fslno, fcollcode, fdegree, fcombcode, fmobileno, femail, fname,fcaste, fimpdate from tmpcollstud 
					where fcollcode = '{$collcode}' and ifnull(fimported,'') <> 'T'";
					$results = $aobj_context->mobj_db->Execute($query);
					
					// Update records as imported 
					$query = "update tmpcollstud set fimported = 'T' where 
					fcollcode = '{$collcode}' and ifnull(fimported,'') <> 'T'";
					$results = $aobj_context->mobj_db->Execute($query);
					$i++;
				}
			}
			

			$query = "select distinct FUNIVCODE, FUNIVNAME,FFOLDER,FSMSUSER,FSMSPASSWD,FSMSSENDID 
			from logisys3_comexam.dbname where funivcode = '{$_SESSION['FUNIVCODE']}'";
			$res = $aobj_context->mobj_db->GetRow($query);
			$FUNIVCODE = $res['FUNIVCODE'];
			$FUNIVNAME = $res['FUNIVNAME'];
			$FFOLDER = $res['FFOLDER'];
			$FSMSUSER = $res['FSMSUSER'];
			$FSMSPASSWD = $res['FSMSPASSWD'];
			$FSMSSENDID = $res['FSMSSENDID']; 

			$enttype = "AI{$collcode}";

			$sql1 = "insert ignore into logisys3_comexam.sendsms(funivcode, fregno, fmobileno, 
			fenttype, fsubcode, fmessage, fstatus, fdeleted, fyear, fexamtype,fupdate)
			select distinct '{$FUNIVCODE}', x.fmobileno, x.fmobileno, '{$enttype}', '{$enttype}', 
			concat('From {$FFOLDER}: Dear ', x.fname,', You are required to submit the admission details in online.',' Visit https://universitysolutions.in/','{$FFOLDER}','/adm', ' - Uniclare') as fmessage, 
			'F', 'F', '2020', '2', now() 
			from collstud x  where ifnull(x.fsmspush,'') <> 'T' 
			and x.fcollcode = '{$collcode}'
			and ifnull(x.factive,'') <> 'T'";

			$results = $aobj_context->mobj_db->Execute($sql1);

			if($results) {

				$sql1 = "update collstud x set x.fsmspush = 'T'
				where ifnull(x.fsmspush,'') <> 'T' and x.fcollcode = '{$collcode}'";
				
				$res = $aobj_context->mobj_db->Execute($sql1);
				
				$query = "select funivcode, fregno, fenttype, fmobileno as mobileno, 
				concat(fmessage,' Sent Time: ',time(now())) as fmessage
				from logisys3_comexam.sendsms 
				where ifnull(fstatus, 'F') = 'F' and fenttype = '{$enttype}' 
				and funivcode = '{$FUNIVCODE}' and fmobileno is not null";

				$results = $aobj_context->mobj_db->GetAll($query);

				$username = $FSMSUSER;
				$password = $FSMSPASSWD;
				$from = $FSMSSENDID;
				$category = '';

				$commondb = 'logisys3_comexam';

				if(count($results) >0)
				{
					$i = 0;
					foreach( $results as $key => $row)
					{
						$to = $row['mobileno'];
						$text = $row['fmessage'];

						$smsresp = sendsmaapi($username,$password,$from,$to,$text,$category);	

						$rgInsert[] = "('".$row['funivcode']."','".$row['fregno']."','{$to}','0',
						'{$smsresp}','".$row['fenttype']."','T', DATE_FORMAT(NOW(),'%Y-%m-%d'), 
						DATE_FORMAT(NOW(),'%T'))";
						$rgTrimedMobs[$i] = $to;
						
						if($i == 50)
						{
							$strInsertQuery = "insert into {$commondb}.smslog(funivcode, fregno,fmobileno, 
							freasoncode, fguid, fenttype, fstatus,fackdate, facktime) 
							values".implode(',',$rgInsert);
							//var_dump($strInsertQuery);
							$result1 = $aobj_context->mobj_db->Execute($strInsertQuery);

							$srtImplodedMobs = implode("','",$rgTrimedMobs); 
							$strUpdateQuery = "update {$commondb}.sendsms set fstatus = 'T' 
							where fenttype = '{$row['fenttype']}' and fmobileno in ('{$srtImplodedMobs}')";
							//var_dump($strUpdateQuery);	
							
							$result2 = $aobj_context->mobj_db->Execute($strUpdateQuery);
							
							unset($rgInsert);
							$rgInsert = array();

							unset($rgTrimedMobs);
							$rgTrimedMobs = array();
							$i = 0;
						}

						$i++;
					}

					$strInsertQuery = "insert into {$commondb}.smslog(funivcode,fregno,fmobileno, freasoncode, 
					fguid, fenttype, fstatus,fackdate, facktime) values".implode(',',$rgInsert);
					//var_dump($strInsertQuery);
					$result3 = $aobj_context->mobj_db->Execute($strInsertQuery);

					$srtImplodedMobs = implode("','",$rgTrimedMobs); 
					$strUpdateQuery = "update {$commondb}.sendsms set fstatus = 'T' 
					where  fenttype = '{$row['fenttype']}' and fmobileno in ('{$srtImplodedMobs}')";
					//var_dump($strUpdateQuery);	
					$result3 = $aobj_context->mobj_db->Execute($strUpdateQuery);
					

			}

			$data = "Uploaded Successfully.";
			echo $aobj_context->mobj_output->ToJSONEnvelope($data,0,"success"); 
			return;	
		}
	}
	
}
}

?>