PHP Classes

File: src/DataTypes/WifiValidator.php

Recommend this page to a friend!
  Packages of Amirreza Ebrahimi   HeroQR Powerful PHP QR Code Library to generate PNG, SVG, PDF, Logos Ready to Use with Laravel   src/DataTypes/WifiValidator.php   Download  
File: src/DataTypes/WifiValidator.php
Role: Class source
Content type: text/plain
Description: Class source
Class: HeroQR Powerful PHP QR Code Library to generate PNG, SVG, PDF, Logos Ready to Use with Laravel
Generate QR code images in several formats
Author: By
Last change:
Date: 23 days ago
Size: 1,603 bytes
 

Contents

Class file image Download
<?php

namespace HeroQR\DataTypes;

use
HeroQR\Contracts\DataTypes\AbstractDataType;

/**
 * Validates Wi-Fi credentials for QR Code.
 * Format: WIFI:T:<encryption>;S:<SSID>;P:<password>;
 * Encryption types: WPA, WPA2, WEP, nopass, WPA/WPA2-Personal (treated as WPA2)
 * Checks SSID length/characters and password rules based on encryption type.
 * Example: WIFI:T:WPA2;S:MyNetwork;P:password123;
 */
class WifiValidator extends AbstractDataType
{
    public static function
validate(string $data): bool
   
{
       
$wifiString = trim($data);

       
$pattern = '/^WIFI:T:(WPA|WPA2|WEP|nopass|WPA\/WPA2-Personal);S:([^;]+);(?:P:([^;]*))?;$/';

        if (empty(
$wifiString) || !preg_match($pattern, $wifiString, $matches)) {
            return
false;
        }

        list(,
$encryptionType, $ssid, $password) = $matches;

        if (
$encryptionType === 'WPA/WPA2-Personal') {
           
$encryptionType = 'WPA2';
        }

        if (
strlen($ssid) > 32 || preg_match('/[;:]/', $ssid)) {
            return
false;
        }

        if (
in_array($encryptionType, ['WPA', 'WPA2'])) {
            if (empty(
$password) || strlen($password) < 8 || strlen($password) > 63) {
                return
false;
            }
        }

        if (
$encryptionType === 'WEP') {
            if (empty(
$password) || !in_array(strlen($password), [10, 26]) || !ctype_xdigit($password)) {
                return
false;
            }
        }

        if (
$encryptionType === 'nopass') {
            if (!empty(
$password)) {
                return
false;
            }
        }

        return
true;
    }
}