Edit File: search.php
#!/usr/bin/env php <?php // ----------------------------------------------------------------- // search.php (CLI helper for ▒search▒ action) // ----------------------------------------------------------------- // always emit JSON header('Content-Type: application/json'); ini_set('display_errors', 0); error_reporting(E_ALL); // pull POST in CLI mode if (php_sapi_name() === 'cli') { if (empty($_POST)) { $stdin = file_get_contents('php://stdin'); parse_str($stdin, $_POST); } } function log_message($message) { error_log($message, 0); } function log_to_file($logDir, $logFile, $message) { if (!is_dir($logDir)) { mkdir($logDir, 0755, true); } file_put_contents($logFile, $message, FILE_APPEND | LOCK_EX); } // ------------------------------------------------------------- // 1) Config & helper // ------------------------------------------------------------- define('CPTOOLS_BINARY', '/usr/local/cptools/cptools'); define('ACTIVATE_SCRIPT', '/usr/local/cptools/activate'); function runCommand($cmd, array &$output = null, &$rc = null) { if (function_exists('exec')) { @exec($cmd . ' 2>&1', $output, $rc); return implode("\n", $output); } if (function_exists('shell_exec')) { $raw = @shell_exec($cmd . ' 2>&1'); $rc = ($raw === null ? 1 : 0); $output = ($raw !== null ? explode("\n", $raw) : []); return $raw; } $rc = 1; $output = ['Neither exec() nor shell_exec() is available']; return ''; } // ------------------------------------------------------------- // 2) License check (scan *all* output, then one-shot activate) // ------------------------------------------------------------- $licRaw = runCommand(CPTOOLS_BINARY, $licLines, $licRc); if (strpos($licRaw, 'License Is Valid') === false) { // try activation once @shell_exec(ACTIVATE_SCRIPT . ' 2>&1'); $licRaw = runCommand(CPTOOLS_BINARY, $licLines, $licRc); if (strpos($licRaw, 'License Is Valid') === false) { http_response_code(403); echo json_encode([ 'error' => 'License inactive. Contact the root owner to activate.', 'details' => $licRaw, ]); exit; } } // ------------------------------------------------------------- // 3) Validate & sanitize inputs // ------------------------------------------------------------- // search-string if (isset($_POST['search-string']) && trim($_POST['search-string']) !== '') { $searchText = $_POST['search-string']; } else { echo json_encode(['error'=>'Missing search-string']); exit; } // paging if (isset($_POST['page_number'])) { $pageNumber = (int) $_POST['page_number']; } else { $pageNumber = 1; } if ($pageNumber < 1) { $pageNumber = 1; } if (isset($_POST['page_size'])) { $pageSize = (int) $_POST['page_size']; } else { $pageSize = 30; } if ($pageSize < 1) { $pageSize = 30; } // homedir + dirname if (isset($_POST['homedir'])) { $homeDir = rtrim($_POST['homedir'], '/'); } else { $homeDir = ''; } if (isset($_POST['dirname'])) { $dirName = ltrim($_POST['dirname'], '/'); } else { $dirName = ''; } $fullDir = $homeDir . '/' . $dirName; if (! is_dir($fullDir)) { echo json_encode(['error'=>"Directory not found: {$fullDir}"]); exit; } // ------------------------------------------------------------- // 4) Build & run the `cptools search` command // ------------------------------------------------------------- $cmd = escapeshellarg(CPTOOLS_BINARY) . ' search ' . escapeshellarg($fullDir) . ' ' . escapeshellarg($searchText) . ' ' . $pageNumber . ' ' . $pageSize; $output = runCommand($cmd, $outLines, $retVal); if ($retVal !== 0) { http_response_code(500); echo json_encode([ 'error' => 'Search command failed', 'details' => $output, ]); exit; } // strip anything before the first `{` if (false !== ($pos = strpos($output, '{'))) { $output = substr($output, $pos); } // ------------------------------------------------------------- // 5) Decode & emit // ------------------------------------------------------------- $data = json_decode($output, true); if (json_last_error() !== JSON_ERROR_NONE) { http_response_code(500); echo json_encode([ 'error' => 'Invalid JSON from cptools', 'details' => json_last_error_msg(), ]); exit; } $fullDirPath = rtrim($homeDir, '/') . '/' . ltrim($dirName, '/'); $logDir = getenv('HOME') . '/.cptools/'; $logFile = $logDir . 'activity_log'; $logEntry = date('d-m-Y h:i A') . " IST Search Keyword >> $fullDirPath {$_POST['search-string']}" . PHP_EOL; log_to_file($logDir, $logFile, $logEntry); echo json_encode($data); exit; ?>