<?php
require '../conf/config.php';
require './common.php';
////////////////////////////////////////////param
(isset($_POST['params'])) ? $params = $_POST['params']:returnError("Error");
/////////////////////////////////////////////////
(isset($params['blast_qryarea']) && !empty($params['blast_qryarea'])) ? $query=$params['blast_qryarea']:_stopError("Invalid : Query is empty!");
(isset($params['blast_eval']) && !empty($params['blast_eval'])) ? $evalue=$params['blast_eval']:returnError("Error");
(isset($params['blast_word_size']) && !empty($params['blast_word_size'])) ? $wordSize = $params['blast_word_size']:returnError("Error");
(isset($params['max_target_seqs']) && !empty($params['max_target_seqs'])) ? $maxTarget = $params['max_target_seqs']:returnError("Error");
(isset($params['match_scr']) && !empty($params['match_scr'])) ? $matchScr = $params['match_scr']:returnError("Error");
(isset($params['mismatch_scr']) && !empty($params['mismatch_scr'])) ? $mismatchScr = $params['mismatch_scr']:returnError("Error");
//(isset($params['gap_open']) && $params['gap_open'] !== null && $params['gap_open'] !== "") ? $gapOpen = $params['gap_open']:returnError("Error");
//(isset($params['gap_extend']) && !empty($params['gap_extend'])) ? $gapExtend = $params['gap_extend']:returnError("Error");
$gapOpen = $params['gap_open'];
$gapExtend = $params['gap_extend'];
$program = $params['blast_program'];


///Validating Data///
$query = str_replace("&gt;",">",$query);

$query = preg_replace('/<script>.*<\/script>/','',$query);
if(preg_match("/\;|\||\&|\`|\(|\)|\*|\?|\{|\}|\[|\]|\!/",$query)) {
        $search = array(';','|','&','amp','`','(',')','*','?','{','}','[',']','!');
        $replace = array('','','','','','','','','','','','','','');
        $qtmp = str_replace($search,$replace,$query);
        $query = $qtmp;
}

_pregMatch($evalue);
if (!(is_numeric($wordSize)) || !(is_numeric($maxTarget)) || !(is_numeric($matchScr)) || !(is_numeric($mismatchScr)) || !(is_numeric($gapOpen)) || !(is_numeric($gapExtend))) {
	$errmsg = "<div align=\"center\"><font color=\"red\" style=\"font-size:18px;\">Invalid Value!!</font></div>";
        echo $errmsg;
        exit;
}
$fp = tmpfile();
fwrite($fp, $query);
rewind($fp);
$tmparray = stream_get_meta_data($fp);

if(is_file($blastPath)) $blastPath = dirname($blastPath);
//if (!file_exists($blastPath) || preg_match("/^\S*blastn$/", $blastPath) != 1) {
if (!file_exists($blastPath. "/". $program)) {
	$errmsg = "Error: blastn was not found.";
	echo $errmsg;
	exit;
}
$blast_Cmd = "";
if($program === "tblastn") {
	$blast_Cmd = $blastPath. "/tblastn -task tblastn -gapopen ". $gapOpen. " -gapextend ". $gapExtend;
} elseif($program === "tblastx") {
	if(!$useTblastx) {
		$errmsg = "Error: tblastx is disabled.";
		echo $errmsg;
		exit;
	}
	$blast_Cmd = $blastPath. "/tblastx";
} else {
	$blast_Cmd = $blastPath. "/blastn -task blastn -reward ". $matchScr. " -penalty ". $mismatchScr. " -gapopen ". $gapOpen. " -gapextend ". $gapExtend;
}
$blast_Cmd .= " -db ". $blastDBPath." -query ". $tmparray['uri']. " -evalue ". $evalue. " -word_size ". $wordSize. " -max_target_seqs ". $maxTarget;
//$blast_Cmd = $blastPath." -db ".$blastDBPath." -query ".$tmparray['uri']." -task blastn -evalue ".$evalue." -word_size ".$wordSize." -max_target_seqs ".$maxTarget." -reward ".$matchScr." -penalty ".$mismatchScr." -gapopen ".$gapOpen." -gapextend ".$gapExtend;
//$blast_Cmd .= " -outfmt 5";
//error_log("Cmd:". $blast_Cmd);
_pregMatch($blast_Cmd);
$return;
$resArray=Array();
exec($blast_Cmd,$resArray,$return);
if(empty($resArray)){
        echo "Error: Can not execute blastn.";
        exit;
}
header('Content-type:application/json');
echo json_encode($resArray);

function _stopError($str){
    echo $str;
    exit;
}

function returnError($str){
    echo $str;
}
