<?php
    if (isset($_GET['qry'])) {
	    $add_title = str_replace("<", " ", $_GET['qry']);
	}
    include("template/Head.php");
?>
<script language="JavaScript" src="js/init.js"></script>
<script language="JavaScript" src="js/image.js"></script>
<?php
// list the photo album
if (!isset($_GET['sec']) && !isset($_GET['qry'])) {
    $sec = 'STAT';
    $qry = 'active';
}
else {
    if (isset($_GET['sec'])) {
	$sec = $_GET['sec'];
	if (!ctype_alpha($sec)) $sec = 'BASIC';  // depress number, spaces, etc
    }
    else $sec = 'BASIC';
    if (isset($_GET['qry'])) {
		$qry = $_GET['qry'];
    }
}
if (isset($_GET['p'])) $p = (int)$_GET['p']; else $p = 0;
if (isset($_GET['ps'])) $ps = (int)$_GET['ps']; else $ps = 16;

// now go to gbsearch if a query exists
if ( isset($qry) ) {
	// remove non alnum chars from query
	$q=trim($qry);
	$len=strlen($q);
	for ($i=0; $i<$len;$i++) {
		if (
				($q[$i] >= 'A' && $q[$i] <= 'Z') ||
				($q[$i] >= 'a' && $q[$i] <= 'z') ||
				($q[$i] >= '0' && $q[$i] <= '9') )
			continue;
		else {
			switch($q[$i]) {
				case '`':
				case '\\':
				case '\n':
//				case '&':
//				case '|':
//				case '\'':
				case '"':
				case ',':
				case '.':
				case '/':
				case '?':
				case '>':
				case '<':
//				case '-':
					$q[$i] = ' ';
					break;
				default:
					break;
			}
		}
	}

    // shell의 특수문자 처리(역슬래쉬)를 역처리...
    $q = stripslashes($q);
	$pureQry = str_replace('-and-', ' and ', $q); // 20231231. change the query including '-and-' to solve operator problem

	// 관련어 목록을 DB에서 빼온다. 검색 첫 페이지에만 인쇄...
	if ($_kr && $p == 0) {
		passthru('bin/apa.terms_like -f0 -n25 -m1 -l1 -qTERM:"'.$pureQry.'"');
		// 한글의 경우에는 LIKE 검색을 추가 한다...
		$pureQry = str_replace("  ", " ", $pureQry);
		$pureQry = str_replace(" ", "% %", $pureQry);
		$pureQry = '%'.$pureQry.'%';
//		echo $pureQry;
		passthru('bin/apa.terms_like -f0 -n25 -m0 -l1 -qTERM:"'.$pureQry.'"');
	}
	else if ($p == 0)
		passthru('bin/apa.terms_like -f0 -n25 -m1 -l0 -qTERM:"'.$pureQry.'"');

//    if (!strcmp($sec, 'STAT') && !strcmp($qry, 'active'))
//    $q = "-q\"(".$sec.":".$q.")\"";
//    $q = "-q\"(".$sec.":\\\"".$q."\\\")\"";
    // $q = "-q\"".$sec.":(".$q.")\"";
    $pureQry = str_replace('-and-', ' and ', $q); // 20231231. change the query including '-and-' to solve operator problem
    $q = "-q\"".$sec.":(".$pureQry.")\""; // 2023.12.31. change dash from query including '-and-' to solve operator problem
    if ($_kr) $q = "-l1 ".$q;
    if (!strcmp($sec, 'STAT') ||
	    !strcmp($sec, 'MAIL') ||
	    !strcmp($sec, 'NAME')
	    )
		$cmd = 'bin/apa.search -f'.$p.' -n'.$ps.' -m0 '.$q;
    else
		$cmd = 'bin/apa.search -f'.$p.' -n'.$ps.' -m1 '.$q;

	if (stristr($cmd, 'calphotos') || stristr($cmd, 'berkeley')) {
		echo '<center><p>No Result</a>';
	}
	else passthru($cmd);


echo '<!-- Search Google -->';
echo "<center><p><font size=-1>";
include('google_custom_search.php');
if ($_kr) echo '구글에서 더 많은 정보를 얻을 수 있습니다.';
else echo 'Try google to get more information about animals.';
echo "</font></p></center>";
echo '<!-- Search Google -->';

}
else {	// if query is not entered
	echo '<p><table border=0 cellpadding=5><tr><td class=title>Enter Your Query.</td></tr></table><p>';
}

?>

<?php include("template/tail.php"); ?>
</body>
</html>


</body>

<HEAD>
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
</HEAD>
</html>
