Edit File: large_files.php
<?php header('Content-Type: application/json'); error_reporting(E_ALL); ini_set('display_errors', 1); // --- Populate $_POST from STDIN when running in CLI --- if (php_sapi_name() === 'cli' && empty($_POST)) { parse_str(file_get_contents('php://stdin'), $_POST); } // ------------------------- // Configuration & Constants // ------------------------- define('LICENSE_FILE', '/usr/local/cptools/cptools.lisc'); define('EXPIRED_FILE', '/usr/local/cptools/expired'); define('CPTOOLS_BINARY', '/usr/local/cptools/cptools'); 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); } // ------------------------- // Retrieve and Validate POST Data // ------------------------- // homedir if (isset($_POST['homedir'])) { $homeDir = $_POST['homedir']; } else { $homeDir = null; } // dirname if (isset($_POST['dirname'])) { $dirName = $_POST['dirname']; } else { $dirName = null; } // page_size $pageSize = $_POST['page_size']; // pageNumber if (isset($_POST['page_number'])) { $pageNumber = (int) $_POST['page_number']; } else { $pageNumber = 1; } if (!$homeDir || !$dirName) { echo json_encode(['error'=>'Missing required parameters (homedir or dirname).']); exit; } $fullDirPath = rtrim($homeDir, '/') . '/' . ltrim($dirName, '/'); if (!is_dir($fullDirPath)) { echo json_encode(['error'=>'Directory does not exist: '.$fullDirPath]); exit; } // ------------------------- // Call the cptools binary // ------------------------- $cmd = CPTOOLS_BINARY . " large_file " . escapeshellarg($fullDirPath) . " {$pageNumber} {$pageSize}" . " 2>/dev/null"; // drop any stderr noise $output = shell_exec($cmd); if (trim($output) === '') { echo json_encode(['error'=>'No output from helper.']); exit; } // ------------------------- // Strip any noise before the JSON // ------------------------- $pos = strpos($output, '{'); if ($pos === false) { echo json_encode([ 'error' => 'No JSON object found in helper output', 'details' => $output ]); exit; } $json = substr($output, $pos); $data = json_decode($json, true); if (json_last_error() !== JSON_ERROR_NONE) { echo json_encode([ 'error' => 'JSON decode failed', 'details' => json_last_error_msg() ]); exit; } // ------------------------- // Format File Sizes // ------------------------- function formatSizeUnits($bytes) { if ($bytes >= 1073741824) return number_format($bytes/1073741824,2).' GB'; if ($bytes >= 1048576) return number_format($bytes/1048576,2).' MB'; if ($bytes >= 1024) return number_format($bytes/1024,2).' KB'; return $bytes.' bytes'; } if (!empty($data['results']) && is_array($data['results'])) { foreach ($data['results'] as &$r) { if (isset($r['size'])) { $r['size'] = formatSizeUnits($r['size']); } } } // ------------------------- // Log the Search Activity // ------------------------- $logDir = getenv('HOME').'/.cptools/'; $logFile = $logDir.'activity_log'; if (!is_dir($logDir)) mkdir($logDir, 0700, true); $entry = date('d-m-Y h:i A').' IST Large file Finder >> ' . $fullDirPath . " (Last {$numFiles} files)\n"; file_put_contents($logFile, $entry, FILE_APPEND | LOCK_EX); // ------------------------- // Return the JSON Response // ------------------------- $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; ?>