Edit File: find_recent.php
#!/usr/bin/env php <?php // ----------------------------------------------------------------- // find_recent.php (CLI helper for â–’find_recentâ–’ action) // ----------------------------------------------------------------- header('Content-Type: application/json'); ini_set('display_errors', 0); error_reporting(E_ALL); // If run via CLI with no POST data, grab stdin if (php_sapi_name() === 'cli' && empty($_POST)) { parse_str(file_get_contents('php://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) Configuration define('CPTOOLS_BINARY', '/usr/local/cptools/cptools'); define('ACTIVATE_SCRIPT', '/usr/local/cptools/activate'); // 2) Helper to run shell commands (captures both stdout & stderr) 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 ''; } // 3) License check (with one-shot activation) $licRaw = runCommand(CPTOOLS_BINARY, $licLines, $licRc); if (strpos($licRaw, 'License Is Valid') === false) { @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; } } // 4) Gather & sanitize inputs with if/else if (isset($_POST['homedir'])) { $homedir = trim($_POST['homedir']); } else { $homedir = '/'; } if (isset($_POST['dirname'])) { $dirname = trim($_POST['dirname']); } else { $dirname = '/'; } if (isset($_POST['page_number'])) { $page_number = (int) $_POST['page_number']; } else { $page_number = 1; } if (isset($_POST['page_size'])) { $page_size = (int) $_POST['page_size']; } elseif (isset($_POST['numRec'])) { $page_size = (int) $_POST['numRec']; } else { $page_size = 20; } // build full path $fullDir = rtrim($homedir, '/') . '/' . ltrim($dirname, '/'); if (! is_dir($fullDir)) { http_response_code(400); echo json_encode([ 'error' => "Directory does not exist: {$fullDir}" ]); exit; } // 5) Invoke the binary $cmd = escapeshellarg(CPTOOLS_BINARY) . ' find_recent ' . escapeshellarg($fullDir) . ' ' . $page_number . ' ' . $page_size; $output = runCommand($cmd, $outLines, $retVal); if ($retVal !== 0) { http_response_code(500); echo json_encode([ 'error' => 'find_recent command failed', 'details' => $output, ]); exit; } // strip noise before JSON if (false !== ($pos = strpos($output, '{'))) { $output = substr($output, $pos); } // 6) 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; } $home = getenv('HOME') ?: '/root'; $logDir = $home . '/.cptools/'; $logFile = $logDir . 'activity_log'; if (!is_dir($logDir)) { mkdir($logDir, 0755, true); } if (is_writable($logDir)) { $logEntry = date('d-m-Y h:i A') . " IST Recent Modification >> $fullDir (Page $page_number, Page Size $page_size)" . PHP_EOL; file_put_contents($logFile, $logEntry, FILE_APPEND | LOCK_EX); } else { error_log("Cannot write to log directory: $logDir"); } echo json_encode($data); exit; ?>