# CrimeSafe Map - PHP Full Stack Project

## 🇧🇩 Bangladesh Safety Navigation Platform

Complete PHP web application for crime reporting, live mapping, and community safety management.

---

## 📋 Features

### ✅ Complete Functionality
- **User Authentication** - Dual system for Citizens & Law Enforcement Agencies
- **Live Crime Map** - Interactive map with real-time crime markers using Leaflet.js
- **Report Crime** - Location-based crime reporting with map picker
- **Community Page** - Agency posts with likes, comments & real-time engagement
- **Analytics Dashboard** - Crime statistics with Chart.js visualizations
- **Agency Dashboard** - Report verification, post management & incident tracking
- **Responsive Design** - Works on all devices

### 👥 User Types
1. **Citizens** - Can report crimes, view map, engage with community
2. **Law Enforcement** - Police, RAB, Government agencies with special dashboard

---

## 🚀 Installation Guide

### Prerequisites
- PHP 7.4 or higher
- MySQL 5.7 or higher
- Apache/Nginx web server
- XAMPP/WAMP (recommended for local development)

### Step 1: Database Setup

1. Open phpMyAdmin or MySQL command line
2. Run the SQL file:
```sql
SOURCE /path/to/php-project/database.sql
```

Or manually:
- Create database: `crimesafe_map`
- Import the `database.sql` file

### Step 2: Configure Database Connection

Edit `/php-project/config/database.php`:

```php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');          // Your MySQL username
define('DB_PASS', '');              // Your MySQL password
define('DB_NAME', 'crimesafe_map');
```

### Step 3: File Structure

Ensure this structure:
```
php-project/
├── config/
│   └── database.php
├── includes/
│   ├── header.php
│   └── footer.php
├── pages/
│   ├── home.php
│   ├── login.php
│   ├── map.php
│   ├── report.php
│   ├── community.php
│   ├── analytics.php
│   └── agency-dashboard.php
├── api/
│   ├── add-comment.php
│   ├── get-comments.php
│   └── toggle-like.php
├── index.php
├── logout.php
└── database.sql
```

### Step 4: Run the Application

1. **Using XAMPP:**
   - Copy `php-project` folder to `C:/xampp/htdocs/`
   - Start Apache & MySQL in XAMPP Control Panel
   - Visit: `http://localhost/php-project/`

2. **Using PHP Built-in Server:**
```bash
cd php-project
php -S localhost:8000
```
Visit: `http://localhost:8000`

---

## 🔐 Default Login Credentials

### Citizen Account
- **Email:** user@example.com
- **Password:** password

### Agency Accounts
- **Police:** police@dhaka.gov.bd / password
- **RAB:** rab@dhaka.gov.bd / password

---

## 🎯 Main Pages & Routes

| Page | URL | Access |
|------|-----|--------|
| Home | `?page=home` | Public |
| Live Map | `?page=map` | Public |
| Report Crime | `?page=report` | **Login Required** |
| Community | `?page=community` | **Login Required** |
| Analytics | `?page=analytics` | Public |
| Login/Register | `?page=login` | Public |
| Agency Dashboard | `?page=agency-dashboard` | **Agency Only** |

---

## 💻 Technology Stack

### Backend
- **PHP 7.4+** - Server-side logic
- **MySQL** - Database
- **MySQLi** - Database connection with prepared statements

### Frontend
- **HTML5 & CSS3** - Structure & styling
- **JavaScript (Vanilla)** - Interactivity
- **Leaflet.js** - Interactive maps
- **Chart.js** - Data visualization
- **Font Awesome** - Icons

### Security Features
- ✅ Password hashing (bcrypt)
- ✅ Prepared statements (SQL injection protection)
- ✅ Session management
- ✅ XSS protection with htmlspecialchars()
- ✅ Input validation

---

## 📊 Database Schema

### Tables
1. **users** - User accounts (citizens & agencies)
2. **crime_reports** - Crime incident reports
3. **community_posts** - Agency announcements
4. **post_likes** - Post engagement
5. **post_comments** - Comments on posts
6. **active_incidents** - Live incident tracking

---

## 🔧 API Endpoints

### POST /api/add-comment.php
Add comment to community post
```javascript
{
  post_id: number,
  comment: string
}
```

### GET /api/get-comments.php?post_id={id}
Get all comments for a post

### POST /api/toggle-like.php
Like/unlike a post
```javascript
{
  post_id: number
}
```

---

## 🎨 Features Breakdown

### 1. Live Map (`pages/map.php`)
- Interactive Leaflet map centered on Dhaka
- Crime markers color-coded by type
- Filter by crime type
- User location detection
- Popup with crime details

### 2. Report Crime (`pages/report.php`)
- Visual crime type selection
- Interactive map location picker
- Current location detection
- Reverse geocoding for addresses
- Anonymous reporting option

### 3. Community (`pages/community.php`)
- Agency-only posting (with priority levels)
- Like & comment system
- Real-time engagement tracking
- Official agency badges

### 4. Analytics (`pages/analytics.php`)
- Crime statistics cards
- Doughnut chart (crime by type)
- Line chart (6-month trends)
- Top crime locations

### 5. Agency Dashboard (`pages/agency-dashboard.php`)
- Pending reports verification
- Quick actions panel
- Statistics overview
- Recent posts management

---

## 🐛 Troubleshooting

### Database Connection Error
```
Check:
1. MySQL service is running
2. Database credentials in config/database.php
3. Database 'crimesafe_map' exists
```

### Map Not Loading
```
Check:
1. Internet connection (Leaflet CDN)
2. Browser console for errors
3. Latitude/longitude values in database
```

### Comments Not Showing
```
Check:
1. User is logged in
2. Session is active
3. Browser console for AJAX errors
```

---

## 📱 Mobile Responsive

The application is fully responsive with:
- Mobile navigation toggle
- Responsive grid layouts
- Touch-friendly buttons
- Optimized map controls

---

## 🔒 Security Best Practices

1. ✅ Never commit database passwords
2. ✅ Use HTTPS in production
3. ✅ Enable error logging (not display)
4. ✅ Regular database backups
5. ✅ Update dependencies

---

## 🚀 Production Deployment

### Recommended Changes:

1. **config/database.php:**
```php
define('DB_HOST', 'your-production-host');
define('DB_USER', 'your-production-user');
define('DB_PASS', 'strong-password');
```

2. **PHP Settings:**
```ini
display_errors = Off
log_errors = On
error_log = /path/to/php-error.log
```

3. **Security Headers:**
Add to `.htaccess`:
```apache
Header set X-Content-Type-Options "nosniff"
Header set X-Frame-Options "SAMEORIGIN"
Header set X-XSS-Protection "1; mode=block"
```

---

## 📞 Support

For issues or questions:
- Check troubleshooting section
- Review browser console errors
- Verify database connection
- Check PHP error logs

---

## 📄 License

© 2025 CrimeSafe Map - Bangladesh Safety Navigation Platform

---

## 🎉 Credits

- **Maps:** Leaflet.js + OpenStreetMap
- **Charts:** Chart.js
- **Icons:** Font Awesome
- **Design:** Custom CSS with gradient themes

---

## ✨ Next Steps

After setup:
1. ✅ Create test accounts
2. ✅ Add sample crime reports
3. ✅ Test agency dashboard
4. ✅ Verify all features
5. ✅ Customize for your needs

---

**Happy Coding! 🇧🇩**
