diff --git a/INSTALL.md b/INSTALL.md index d5c6151..d720a1d 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -135,24 +135,7 @@ location /uploads/ { } ``` -### 8. Set Up Cron for Image Cleanup - -Images auto-expire after 24 hours. Set up cron to clean them up: - -```bash -# Edit crontab -crontab -e - -# Add this line (runs every hour): -0 * * * * cd /path/to/nano-pro && /usr/bin/php cleanup.php >> cleanup.log 2>&1 -``` - -Or run manually: -```bash -php cleanup.php -``` - -### 9. Test Installation +### 8. Test Installation Visit these URLs to verify: @@ -401,15 +384,12 @@ nano config.php mkdir -p uploads/sessions chmod 755 uploads/sessions -# 5. Set up cron -crontab -e -# Add: 0 * * * * cd /path/to/nano-pro && php cleanup.php >> cleanup.log 2>&1 - -# 6. Test +# 5. Test # Visit: https://your-server.com/nano-pro/ # Visit: https://your-server.com/nano-pro/auth-test.php -# 7. Done! +# 6. Done! +# Note: Images auto-cleanup on app launch (no cron needed) ``` --- @@ -457,6 +437,23 @@ chmod 755 uploads/sessions/ --- +## Automatic Image Cleanup + +**No cron job required!** The application automatically cleans up expired images: + +- Cleanup runs automatically when users launch the app (~10% of sessions) +- Finds images older than 24 hours across all user sessions +- Deletes expired images and metadata files +- Removes empty session directories +- Logs cleanup activity to `error_log` + +**Manual cleanup** (if needed): +```bash +php cleanup.php +``` + +--- + ## Monitoring ### Check Application Status diff --git a/session_manager.php b/session_manager.php index 0652bb4..8ad65e3 100644 --- a/session_manager.php +++ b/session_manager.php @@ -43,6 +43,12 @@ class SessionManager { // Clean up invalid history entries $this->cleanupImageHistory(); + + // Auto-cleanup expired images (runs randomly ~10% of the time) + // This replaces the need for a cron job + if (rand(1, 10) === 1) { + $this->autoCleanupExpiredImages(); + } } private function initializeSessionData() { @@ -319,6 +325,25 @@ class SessionManager { return $extMap[$extension] ?? 'image/png'; } + /** + * Auto-cleanup expired images (called on session init) + * Runs the cleanup in the background without blocking + */ + private function autoCleanupExpiredImages() { + try { + // Run cleanup silently + $result = self::cleanupExpiredImages($this->uploadDir); + + // Log cleanup results if any images were cleaned + if ($result['cleaned'] > 0) { + error_log("Auto-cleanup: Removed {$result['cleaned']} expired images"); + } + } catch (Exception $e) { + // Silently fail - don't break the app if cleanup fails + error_log("Auto-cleanup failed: " . $e->getMessage()); + } + } + /** * Static method to clean up expired images across all sessions */ diff --git a/uploads/.htaccess b/uploads/.htaccess index 312aaf3..f67cb07 100644 --- a/uploads/.htaccess +++ b/uploads/.htaccess @@ -1,11 +1,17 @@ # Secure uploads directory -# Prevent direct access to uploaded images via browser +# Allow image access but prevent directory listing -# Deny access to all files -Order Deny,Allow -Deny from all +# Disable directory browsing +Options -Indexes -# Allow PHP scripts to access files - +# Allow access to image files + + Order Allow,Deny Allow from all + +# Deny access to metadata files + + Order Deny,Allow + Deny from all +