/ChatX

Primary LanguagePHP

ChatX Advanced - Secure Real-time Chat Application

A modern, secure, and feature-rich real-time chat application built with PHP, WebSockets, and advanced security features.

🚀 Features

Core Features

  • Real-time messaging with WebSocket support and HTTP fallback
  • End-to-end message encryption using AES-256-CBC
  • User authentication with CSRF protection and rate limiting
  • File sharing with support for images, documents, and media
  • Message reactions with emoji support
  • Typing indicators for better user experience
  • Online/offline status tracking
  • Message search and conversation history

Security Features

  • CSRF protection on all forms
  • Rate limiting for messages and login attempts
  • Password strength validation with requirements
  • Account lockout after failed login attempts
  • File upload validation with type and size restrictions
  • XSS protection with input sanitization
  • Secure headers (HSTS, X-Frame-Options, etc.)

Advanced Features

  • Database support with MySQL/PDO and JSON fallback
  • WebSocket server for real-time communication
  • Mobile responsive design
  • Dark theme with modern UI
  • Emoji picker with extensive emoji support
  • User profiles and settings
  • Message encryption at rest and in transit

📋 Requirements

  • PHP 7.4 or higher
  • MySQL 5.7+ (optional, falls back to JSON files)
  • Composer for dependency management
  • Modern web browser with WebSocket support

🛠 Installation

1. Clone or Download

# Download the files to your web server directory
# Example: /var/www/html/chatx/

2. Install Dependencies (Optional - for WebSocket support)

composer install

3. Database Setup (Optional)

-- Create database
CREATE DATABASE chatx_advanced;

-- Create user
CREATE USER 'chatx_user'@'localhost' IDENTIFIED BY 'your_secure_password';
GRANT ALL PRIVILEGES ON chatx_advanced.* TO 'chatx_user'@'localhost';
FLUSH PRIVILEGES;

Update database credentials in database_config.php:

private $db_config = [
    'host' => 'localhost',
    'dbname' => 'chatx_advanced',
    'username' => 'chatx_user',
    'password' => 'your_secure_password',
    'charset' => 'utf8mb4'
];

4. Set Permissions

chmod 755 chatx_advanced.php
chmod -R 775 chat_data/

5. Start WebSocket Server (Optional)

php websocket_server.php

🌐 Usage

Basic Setup

  1. Open chatx_advanced.php in your web browser
  2. Register a new account with a strong password
  3. Login and start chatting!

With WebSocket (Advanced)

  1. Start the WebSocket server: php websocket_server.php
  2. The application will automatically use WebSocket for real-time communication
  3. If WebSocket is unavailable, it falls back to HTTP polling

🔧 Configuration

File Upload Settings

Edit the constants in chatx_advanced.php:

define('MAX_FILE_SIZE', 10 * 1024 * 1024); // 10MB
define('ALLOWED_FILE_TYPES', ['jpg', 'jpeg', 'png', 'gif', 'pdf', 'txt', 'docx']);
define('MAX_MESSAGE_LENGTH', 1000);

Rate Limiting

define('RATE_LIMIT_MESSAGES', 30); // messages per minute
define('RATE_LIMIT_LOGIN', 5); // login attempts per hour

Security Settings

  • Change encryption keys in production
  • Use HTTPS in production
  • Configure proper session settings
  • Set up proper file permissions

🏗 Architecture

File Structure

chatx/
├── chatx_advanced.php      # Main application file
├── database_config.php     # Database abstraction layer
├── websocket_server.php    # WebSocket server
├── websocket_client.js     # WebSocket client
├── composer.json          # Dependencies
├── README.md              # This file
└── chat_data/             # Data directory
    ├── users.json         # User data (if no database)
    ├── messages.json      # Message data (if no database)
    ├── online_users.json  # Online status
    ├── rate_limits.json   # Rate limiting data
    └── uploads/           # File uploads

Database Schema (MySQL)

  • users - User accounts and profiles
  • messages - Chat messages with encryption
  • message_reactions - Message reactions/emojis
  • online_users - Online status tracking
  • rate_limits - Rate limiting counters
  • user_settings - User preferences

🔒 Security Considerations

Production Deployment

  1. Use HTTPS - Always serve over SSL/TLS
  2. Secure database - Use strong passwords and restrict access
  3. File permissions - Set proper file and directory permissions
  4. Rate limiting - Implement reverse proxy rate limiting
  5. Firewall - Configure firewall rules
  6. Monitoring - Set up logging and monitoring
  7. Backups - Regular database and file backups

Password Security

  • Minimum 8 characters
  • Must contain uppercase, lowercase, and numbers
  • Hashed with PHP's password_hash() using bcrypt
  • Account lockout after failed attempts

File Upload Security

  • File type validation
  • Size limits enforced
  • Files stored outside web root when possible
  • Virus scanning recommended for production

🎨 Customization

Themes

The application uses CSS custom properties for easy theming:

:root {
    --bg-primary: #0a0a0a;
    --accent-primary: #6366f1;
    --text-primary: #ffffff;
    /* ... more variables */
}

Adding Features

The modular architecture makes it easy to add features:

  1. Add database schema changes
  2. Update the main ChatX class
  3. Add frontend JavaScript
  4. Update WebSocket handlers if needed

🐛 Troubleshooting

Common Issues

WebSocket Connection Failed

  • Check if WebSocket server is running
  • Verify firewall settings
  • Check browser WebSocket support

File Upload Issues

  • Check PHP upload_max_filesize setting
  • Verify directory permissions
  • Check allowed file types

Database Connection Failed

  • Verify database credentials
  • Check if database service is running
  • The app will fall back to JSON files

Rate Limiting Too Aggressive

  • Adjust rate limit constants
  • Clear rate_limits.json or database table

Performance Optimization

  • Use database instead of JSON for better performance
  • Enable PHP opcache
  • Use Redis for session storage
  • Implement CDN for file uploads
  • Use nginx for static file serving

📝 License

This project is licensed under the MIT License. See LICENSE file for details.

🤝 Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

🆘 Support

For support, please:

  1. Check this README
  2. Review the code comments
  3. Open an issue on the repository
  4. Contact the development team

🔄 Updates

Version 2.0 Features

  • End-to-end encryption
  • WebSocket real-time communication
  • Advanced security features
  • File sharing and reactions
  • Database support with fallback
  • Mobile responsive design
  • Message search and history

Roadmap

  • Voice/video calling
  • Group chats
  • Message threading
  • Advanced user roles
  • API for third-party integrations
  • Progressive Web App (PWA)
  • Multi-language support

ChatX Advanced - Secure, Modern, Real-time Communication 🚀