[], 'keywords' => []]; if (file_exists($blacklistFile)) { $content = file_get_contents($blacklistFile); if ($content !== false && !empty($content)) { $blacklist = json_decode($content, true) ?? ['domains' => [], 'keywords' => []]; if (json_last_error() !== JSON_ERROR_NONE) { error_log("JSON decode error in $blacklistFile: " . json_last_error_msg()); $blacklist = ['domains' => [], 'keywords' => []]; } } } else { error_log("Blacklist file does not exist: $blacklistFile"); if (is_writable(__DIR__)) { file_put_contents($blacklistFile, json_encode(['domains' => [], 'keywords' => []])); } } // Check if user is logged in $isLoggedIn = isset($_SESSION['user_id']) && isset($licenses[$_SESSION['user_id']]); if (!$isLoggedIn && isset($_GET['ajax'])) { http_response_code(401); echo json_encode(['error' => 'Please log in to search']); exit; } if ($isLoggedIn) { $userId = $_SESSION['user_id']; $user = $licenses[$userId] ?? []; if (empty($user)) { error_log("User data not found for user_id: $userId"); session_destroy(); http_response_code(401); echo json_encode(['error' => 'User data not found']); exit; } if ($user['banned']) { error_log("User $userId is banned"); http_response_code(403); echo json_encode(['error' => 'Your account is banned']); exit; } if ($user['api_blocked'] && isset($_GET['ajax'])) { error_log("API access blocked for user $userId"); http_response_code(403); echo json_encode(['error' => 'API access is blocked']); exit; } // Check rate limit, daily search limit, and credits $today = date('Y-m-d'); if (!isset($_SESSION['last_search'])) { $_SESSION['last_search'] = 0; $_SESSION['daily_searches'] = ['date' => $today, 'count' => 0]; } if ($_SESSION['daily_searches']['date'] !== $today) { $_SESSION['daily_searches'] = ['date' => $today, 'count' => 0]; } if (isset($_GET['ajax'])) { if (time() - $_SESSION['last_search'] < (60 / $user['rate_limit'])) { error_log("Rate limit exceeded for user $userId"); http_response_code(429); echo json_encode(['error' => 'Rate limit exceeded']); exit; } if ($_SESSION['daily_searches']['count'] >= $user['daily_search_limit']) { error_log("Daily search limit exceeded for user $userId"); http_response_code(429); echo json_encode(['error' => 'Daily search limit exceeded']); exit; } if ($user['credits'] < 1) { error_log("Insufficient credits for user $userId"); http_response_code(403); echo json_encode(['error' => 'Insufficient search credits. Buy more credits']); exit; } } } // Composer autoload & MongoDB client require_once __DIR__ . '/vendor/autoload.php'; use MongoDB\Client, MongoDB\BSON\Regex; // Connect to MongoDB and fetch total count $totalEntries = 0; try { $client = new Client("mongodb://127.0.0.1:27017"); $collection = $client->logscraper->logs; $totalEntries = $collection->countDocuments(); } catch (Exception $e) { error_log("MongoDB connection error: " . $e->getMessage()); http_response_code(500); echo json_encode(['error' => 'Database connection failed']); exit; } // Load search history $historyFile = __DIR__ . '/search_history.json'; $searchHistory = []; if (file_exists($historyFile)) { $content = file_get_contents($historyFile); if ($content !== false && !empty($content)) { $searchHistory = json_decode($content, true) ?? []; if (json_last_error() !== JSON_ERROR_NONE) { error_log("JSON decode error in $historyFile: " . json_last_error_msg()); $searchHistory = []; } } } else { error_log("History file does not exist: $historyFile"); if (is_writable(__DIR__)) { file_put_contents($historyFile, json_encode([])); } } // Safe file write with logging function safeWriteJson($file, $data) { if (!file_exists($file)) { if (!touch($file)) { error_log("Error: Failed to create file $file"); return false; } } if (!is_writable($file)) { error_log("Error: File $file is not writable, permissions: " . (file_exists($file) ? substr(sprintf('%o', fileperms($file)), -4) : 'does not exist')); return false; } $fp = @fopen($file, 'c'); if ($fp === false) { error_log("Error: Failed to open file $file, error: " . error_get_last()['message']); return false; } if (flock($fp, LOCK_EX, $wouldBlock)) { if (ftruncate($fp, 0) === false) { error_log("Error: Failed to truncate file $file"); flock($fp, LOCK_UN); fclose($fp); return false; } $json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); if ($json === false) { error_log("Error: JSON encode failed for file $file, error: " . json_last_error_msg()); flock($fp, LOCK_UN); fclose($fp); return false; } if (fwrite($fp, $json) === false) { error_log("Error: Failed to write to file $file, error: " . error_get_last()['message']); flock($fp, LOCK_UN); fclose($fp); return false; } fflush($fp); // Ensure data is written to disk flock($fp, LOCK_UN); fclose($fp); error_log("Successfully wrote to file $file"); return true; } error_log("Error: Failed to acquire lock on file $file, would block: " . ($wouldBlock ? 'yes' : 'no')); fclose($fp); return false; } // Handle AJAX search requests if (isset($_GET['ajax']) && $isLoggedIn) { header('Content-Type: application/json; charset=UTF-8'); $q = trim($_GET['query'] ?? ''); $searchType = $_GET['type'] ?? 'url'; $useRegex = isset($_GET['regex']); $caseSensitive = isset($_GET['caseSensitive']); $blacklistDomain = trim($_GET['blacklist'] ?? ($_SESSION['blacklist_domain'] ?? '')); $resultsLimit = $user['results_limit'] ?? 10; // Update session blacklist domain $_SESSION['blacklist_domain'] = $blacklistDomain; // Check if query is blacklisted if ($searchType === 'url' || $searchType === 'email_domain') { $cleanQuery = preg_replace(['/^https?:\/\//i', '/^www\./i', '/\/.*$/', '/\.$/'], '', strtolower($q)); if (!empty($cleanQuery)) { // Check blacklisted domains if (is_array($blacklist['domains']) && in_array($cleanQuery, $blacklist['domains'])) { error_log("Blacklisted domain searched by user $userId: $cleanQuery"); if (!isset($searchHistory[$userId])) { $searchHistory[$userId] = []; } $searchHistory[$userId][] = [ 'query' => $q, 'type' => $searchType, 'blacklist' => $blacklistDomain, 'results_count' => 0, 'timestamp' => date('Y-m-d H:i:s'), 'regex' => $useRegex, 'caseSensitive' => $caseSensitive, 'error' => 'Blacklisted domain, unauthorized search' ]; if (!safeWriteJson($historyFile, $searchHistory)) { error_log("Failed to save search history for user_id: $userId"); } http_response_code(403); echo json_encode(['error' => 'Blacklisted domain, unauthorized search']); exit; } // Check blacklisted keywords foreach ($blacklist['keywords'] as $keyword) { if (stripos($cleanQuery, $keyword) !== false) { error_log("Blacklisted keyword '$keyword' found in query by user $userId: $cleanQuery"); if (!isset($searchHistory[$userId])) { $searchHistory[$userId] = []; } $searchHistory[$userId][] = [ 'query' => $q, 'type' => $searchType, 'blacklist' => $blacklistDomain, 'results_count' => 0, 'timestamp' => date('Y-m-d H:i:s'), 'regex' => $useRegex, 'caseSensitive' => $caseSensitive, 'error' => "Blacklisted keyword '$keyword', unauthorized search" ]; if (!safeWriteJson($historyFile, $searchHistory)) { error_log("Failed to save search history for user_id: $userId"); } http_response_code(403); echo json_encode(['error' => "Blacklisted keyword '$keyword', unauthorized search"]); exit; } } } } // Validation if (!$useRegex) { if ($searchType === 'url') { $cleanUrl = preg_replace(['/^https?:\/\//i', '/^www\./i', '/\/.*$/', '/\.$/'], '', $q); if (strlen($cleanUrl) < 1) { http_response_code(400); echo json_encode(['error' => 'Please enter a valid domain (e.g. google.com)']); exit; } $q = $cleanUrl; } elseif (strlen($q) < 3) { http_response_code(400); echo json_encode(['error' => 'Please enter at least 3 characters']); exit; } } if ($useRegex && @preg_match("/{$q}/", "") === false) { error_log("Invalid regular expression: $q"); http_response_code(400); echo json_encode(['error' => 'Invalid regular expression']); exit; } // Build query based on search type $query = []; switch ($searchType) { case 'email': $query['$or'] = [ ['email' => $useRegex ? new Regex($q, $caseSensitive ? '' : 'i') : $q], ['url' => $useRegex ? new Regex($q, $caseSensitive ? '' : 'i') : $q] ]; break; case 'email_domain': $pattern = $useRegex ? $q : preg_quote($q, '/'); $query['email'] = new Regex($pattern, $caseSensitive ? '' : 'i'); break; case 'password': $query['password'] = $useRegex ? new Regex($q, $caseSensitive ? '' : 'i') : $q; break; case 'username': $query['email'] = $useRegex ? new Regex($q, $caseSensitive ? '' : 'i') : $q; break; case 'url': default: if (!preg_match('/^https?:\/\//i', $q)) { $pattern = $useRegex ? $q : preg_quote($q, '/'); $query['url'] = new Regex('^https?://([a-zA-Z0-9-]+\.)*'.$pattern, $caseSensitive ? '' : 'i'); } else { $query['url'] = $useRegex ? new Regex($q, $caseSensitive ? '' : 'i') : $q; } } // Add blacklist domain filter if set if ($blacklistDomain) { $blacklistPattern = preg_quote($blacklistDomain, '/'); $query['url'] = [ '$not' => new Regex('^https?://(www\.)?'.$blacklistPattern.'(/.*)?$', 'i'), '$regex' => $query['url'] instanceof Regex ? $query['url']->getPattern() : $query['url'] ]; } // Query MongoDB try { error_log("Executing MongoDB query for user $userId, query: " . json_encode($query)); $cursor = $collection->find( $query, [ 'projection' => ['_id' => 0, 'url' => 1, 'email' => 1, 'password' => 1, 'source_db' => 1], 'limit' => $resultsLimit ] ); $results = iterator_to_array($cursor); // Deduct 1 credit and update license file $licenses[$userId]['credits'] = max(0, $licenses[$userId]['credits'] - 1); if (!safeWriteJson($licenseFile, $licenses)) { error_log("Failed to update credits for user_id: $userId"); echo json_encode(['error' => 'Failed to update credits']); exit; } // Log search to history if (!isset($searchHistory[$userId])) { $searchHistory[$userId] = []; } $searchHistory[$userId][] = [ 'query' => $q, 'type' => $searchType, 'blacklist' => $blacklistDomain, 'results_count' => count($results), 'timestamp' => date('Y-m-d H:i:s'), 'regex' => $useRegex, 'caseSensitive' => $caseSensitive ]; if (!safeWriteJson($historyFile, $searchHistory)) { error_log("Failed to save search history for user_id: $userId"); echo json_encode(['error' => 'Failed to save search history']); exit; } $_SESSION['search_count'] = ($_SESSION['search_count'] ?? 0) + 1; $_SESSION['last_search'] = time(); $_SESSION['daily_searches']['count']++; echo json_encode(['count' => count($results), 'results' => $results, 'blacklist' => $blacklistDomain]); } catch (Exception $e) { error_log("MongoDB search error: " . $e->getMessage()); http_response_code(500); echo json_encode(['error' => 'Search failed: ' . $e->getMessage()]); } exit; } // HTML-escape helper function h($v) { return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8'); } // Generate CSRF token if (!isset($_SESSION['csrf_token'])) { error_log("Generating new CSRF token in index.php"); $_SESSION['csrf_token'] = bin2hex(random_bytes(32)); } ?> Google



Login



Total Entries

2,15

Your Searches

Last Update

You need credits to search

Search the database

Enter a search query above to begin.