# PHP Extension Usage Audit Report

**Last Updated:** 2026-01-15  
**Audit Date:** 2026-01-15

## Overview

This audit identifies all PHP files that use extension-specific functions and determines whether they have proper extension checks and fallback logic.

## Audit Results Summary

**Files Scanned:** 1,753 PHP files  
**Files Using Extensions:** 168 files  
**Files With Checks:** 13 files  
**Files Without Checks:** 155 files ⚠️

## Extension Usage Statistics

### mbstring

- **Total Usage:** 437 function calls
- **Files Using:** 91 files
- **With Checks:** 10 files ✅
- **Without Checks:** 81 files ⚠️

**Status:** Many files use mbstring functions without checks. Priority: HIGH

### curl

- **Total Usage:** 859 function calls
- **Files Using:** 61 files
- **With Checks:** 1 file ✅
- **Without Checks:** 60 files ⚠️

**Status:** Most curl usage lacks checks. Priority: HIGH

### gd

- **Total Usage:** 117 function calls
- **Files Using:** 16 files
- **With Checks:** 1 file ✅
- **Without Checks:** 15 files ⚠️

**Status:** Most gd usage lacks checks. Priority: MEDIUM (optional extension)

### iconv

- **Total Usage:** 16 function calls
- **Files Using:** 9 files
- **With Checks:** 7 files ✅
- **Without Checks:** 2 files ⚠️

**Status:** Most iconv usage has checks. Priority: LOW

### xml

- **Total Usage:** 11 function calls
- **Files Using:** 8 files
- **With Checks:** 0 files ⚠️
- **Without Checks:** 8 files ⚠️

**Status:** All xml usage lacks checks. Priority: MEDIUM (optional extension)

## Files With Proper Checks

These files already have extension checks and can serve as examples:

1. `v2/config/blog-schema-generator.php` - mbstring checks with fallback
2. `v2/config/blog-template-helpers.php` - mbstring helper functions
3. `v2/api/generate_excel.php` - Extension validation
4. `v2/api/produkt-updates-diagnostics.php` - Extension checks

## High Priority Files (No Checks)

### mbstring Usage Without Checks

**Critical Files:**

- `v2/config/blog-template-helpers.php` - Has helper functions but some direct calls
- `v2/scripts/blog/*.php` - Multiple scripts use mbstring without checks
- `v2/api/*.php` - Some API files use mbstring

**Action Required:**

- Add `function_exists()` checks before mbstring calls
- Use helper functions from `v2/helpers/php-extensions.php`
- Implement fallback logic

### curl Usage Without Checks

**Critical Files:**

- `v2/config/hubspot-api-helpers.php` - API integration
- `v2/api/*.php` - Multiple API endpoints
- `v2/admin/produkt-updates/*.php` - Admin functionality

**Action Required:**

- Add `function_exists('curl_init')` checks
- Implement fallback using `file_get_contents()` if available
- Log fallback usage

### gd Usage Without Checks

**Files:**

- `v2/api/generate_excel.php` - Image processing
- `v2/scripts/optimize-images.php` - Image optimization

**Action Required:**

- Add `extension_loaded('gd')` checks
- Implement fallback (skip image processing)
- Log when fallback is used

## Recommended Actions

### Phase 1: Critical Files (High Priority)

1. **Update API files** using curl:

   - Add curl checks
   - Implement fallback logic
   - Test with curl disabled

2. **Update blog-related files** using mbstring:
   - Use helper functions from `php-extensions.php`
   - Add checks where helper functions not used
   - Test fallback logic

### Phase 2: Important Files (Medium Priority)

1. **Update image processing files** using gd:

   - Add gd checks
   - Implement fallback (skip processing)
   - Log fallback usage

2. **Update XML processing files**:
   - Add xml checks
   - Implement fallback or skip processing

### Phase 3: Remaining Files (Lower Priority)

1. **Update remaining files** incrementally
2. **Use helper functions** from `php-extensions.php`
3. **Test fallback logic** for each file

## Standardization Plan

### Use Helper Functions

**Preferred approach:**

```php
// Include helper functions
require_once __DIR__ . '/../helpers/php-extensions.php';

// Use safe functions
$length = safe_mb_strlen($text, 'UTF-8');
$substring = safe_mb_substr($text, 0, 100, 'UTF-8');
$lowercase = safe_mb_strtolower($text, 'UTF-8');
```

### Use Extension Checks

**For direct function calls:**

```php
if (function_exists('mb_strlen')) {
    $length = mb_strlen($text, 'UTF-8');
} else {
    $length = strlen($text);
}
```

### Use Extension Helpers

**For extension-level checks:**

```php
if (has_extension('mbstring')) {
    // Use mbstring functions
} else {
    // Use fallback
}
```

## Testing Strategy

### For Each Updated File

1. **Test with extension enabled** (normal operation)
2. **Test with extension disabled** (if possible)
3. **Verify fallback logic** activates correctly
4. **Check error logs** for fallback usage
5. **Verify functionality** still works

### Automated Testing

Run test script:

```bash
php v2/scripts/dev-helpers/test-extension-fallbacks.php
```

## Audit Script

**Run audit:**

```bash
php v2/scripts/dev-helpers/audit-extension-usage.php
```

**JSON output:**

```bash
php v2/scripts/dev-helpers/audit-extension-usage.php --json > audit_results.json
```

## Related Documentation

- `docs/development/PHP_EXTENSION_DEPENDENCIES.md` - Extension dependencies guide
- `docs/development/PHP_EXTENSION_FALLBACK_PATTERNS.md` - Fallback patterns
- `v2/helpers/php-extensions.php` - Helper functions
- `.cursor/rules/php-extensions.mdc` - Cursor rules

## Next Steps

1. ✅ Audit completed - 155 files need updates
2. ⏭️ Prioritize critical files (API endpoints, blog system)
3. ⏭️ Update files incrementally
4. ⏭️ Test fallback logic for each update
5. ⏭️ Re-run audit after updates
6. ⏭️ Document updated files

## Notes

- **Vendor files excluded:** Audit focuses on `v2/` directory only
- **Helper functions:** Many files can use helper functions from `php-extensions.php`
- **Incremental updates:** Update files in priority order
- **Testing required:** Test each update before moving to next file

**Remember**: The goal is to prevent production failures, not to update everything at once. Focus on critical paths first.
