| Server IP : 192.169.170.185 / Your IP : 216.73.216.97 Web Server : Apache System : Linux p3plmcpnl495852.prod.phx3.secureserver.net 4.18.0-553.52.1.lve.el8.x86_64 #1 SMP Wed May 21 15:31:29 UTC 2025 x86_64 User : akhilnew ( 1712764) PHP Version : 5.6.40 Disable Function : NONE MySQL : ON | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /home/akhilnew/www/ |
Upload File : |
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('memory_limit', '512M');
set_time_limit(30);
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit();
}
// Optimized similarity function
function fuzzyMatch($str1, $str2) {
$str1 = strtolower($str1);
$str2 = strtolower($str2);
if ($str1 === $str2) return 100;
// Similar text gives good results for typos
similar_text($str1, $str2, $percent);
return $percent;
}
$host = 'localhost';
$dbname = 'akinikpublication';
$username = 'akinikpublicatio';
$password = '2-xu&*3qAh%C';
$query = isset($_GET['q']) ? trim($_GET['q']) : '';
if (empty($query) || strlen($query) < 3) {
echo json_encode(array(
'success' => false,
'message' => 'Minimum 3 characters required',
'suggestions' => array()
));
exit;
}
$mysqli = new mysqli($host, $username, $password, $dbname);
if ($mysqli->connect_error) {
http_response_code(500);
echo json_encode(array(
'success' => false,
'message' => 'Database connection failed',
'suggestions' => array()
));
exit;
}
$mysqli->set_charset('utf8mb4');
$results = array();
$queryLower = strtolower($query);
$queryLen = strlen($queryLower);
// Step 1: Direct exact search
$searchPattern = '%' . $mysqli->real_escape_string($query) . '%';
$sql = "SELECT bookname, volume, newlink
FROM bookchapters
WHERE bookname LIKE ? OR scope LIKE ?
ORDER BY bookname
LIMIT 10";
if ($stmt = $mysqli->prepare($sql)) {
$stmt->bind_param('ss', $searchPattern, $searchPattern);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($bookname, $volume, $newlink);
while ($stmt->fetch()) {
$results[] = array(
'title' => $bookname . " (Volume - " . $volume . ")",
'newlink' => $newlink,
'source' => 'exact'
);
}
$stmt->close();
}
// Step 2: Fuzzy search if no results
if (count($results) === 0) {
// Get ALL distinct words from database (with length filtering in MySQL)
$minLen = $queryLen - 3;
$maxLen = $queryLen + 3;
// More aggressive: get more records
$wordSql = "SELECT DISTINCT bookname, scope
FROM bookchapters
LIMIT 2000";
$candidateWords = array();
if ($wordResult = $mysqli->query($wordSql)) {
while ($row = $wordResult->fetch_assoc()) {
// Process bookname
if (!empty($row['bookname'])) {
$words = preg_split('/[\s,\-_\.;:()\[\]]+/', $row['bookname'], -1, PREG_SPLIT_NO_EMPTY);
foreach ($words as $word) {
$word = strtolower(trim($word));
$wlen = strlen($word);
// Length-based filtering
if ($wlen >= $minLen && $wlen <= $maxLen && $wlen >= 4) {
if (!isset($candidateWords[$word])) {
$candidateWords[$word] = 0;
}
$candidateWords[$word]++;
}
}
}
// Process scope
if (!empty($row['scope'])) {
$words = preg_split('/[\s,\-_\.;:()\[\]]+/', $row['scope'], -1, PREG_SPLIT_NO_EMPTY);
foreach ($words as $word) {
$word = strtolower(trim($word));
$wlen = strlen($word);
if ($wlen >= $minLen && $wlen <= $maxLen && $wlen >= 4) {
if (!isset($candidateWords[$word])) {
$candidateWords[$word] = 0;
}
$candidateWords[$word]++;
}
}
}
}
$wordResult->free();
}
// Find best matches
$matches = array();
foreach ($candidateWords as $word => $frequency) {
$similarity = fuzzyMatch($queryLower, $word);
// Accept anything above 50% similarity
if ($similarity >= 50) {
$matches[] = array(
'word' => $word,
'score' => $similarity,
'frequency' => $frequency
);
}
}
// Sort by similarity first, then frequency
usort($matches, function($a, $b) {
if ($b['score'] != $a['score']) {
return $b['score'] - $a['score'];
}
return $b['frequency'] - $a['frequency'];
});
// Try top 3 matches
$topMatches = array_slice($matches, 0, 3);
foreach ($topMatches as $match) {
$correctedWord = $match['word'];
$correctedPattern = '%' . $mysqli->real_escape_string($correctedWord) . '%';
$sql2 = "SELECT bookname, volume, newlink
FROM bookchapters
WHERE bookname LIKE ? OR scope LIKE ?
ORDER BY bookname
LIMIT 20";
if ($stmt2 = $mysqli->prepare($sql2)) {
$stmt2->bind_param('ss', $correctedPattern, $correctedPattern);
$stmt2->execute();
$stmt2->store_result();
$stmt2->bind_result($bookname, $volume, $newlink);
while ($stmt2->fetch()) {
// Avoid duplicates
$isDuplicate = false;
foreach ($results as $existing) {
if ($existing['newlink'] === $newlink) {
$isDuplicate = true;
break;
}
}
if (!$isDuplicate) {
$results[] = array(
'title' => $bookname . " (Volume - " . $volume . ")",
'newlink' => $newlink,
'source' => 'fuzzy',
'corrected_from' => $query,
'corrected_to' => $correctedWord,
'similarity' => round($match['score'], 1)
);
}
}
$stmt2->close();
}
// Stop if we found results
if (count($results) > 0) break;
}
}
$mysqli->close();
// Limit final results
$results = array_slice($results, 0, 10);
echo json_encode(array(
'success' => true,
'message' => count($results) > 0 ? 'Books found' : 'No books found',
'suggestions' => $results,
'count' => count($results)
), JSON_UNESCAPED_UNICODE);