PHP Classes

File: src/DataTypes/Url.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/Url.php   Download  
File: src/DataTypes/Url.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: Update of src/DataTypes/Url.php
Date: 6 months ago
Size: 1,710 bytes
 

Contents

Class file image Download
<?php

namespace HeroQR\DataTypes;

use
HeroQR\Contracts\DataTypes\AbstractDataType;

/**
 * Class Url
 *
 * This class validates a URL to ensure it follows a proper structure and does not contain unsafe content.
 * It checks the URL's validity by verifying the following:
 * - The URL format (http/https).
 * - The URL contains a valid domain (host).
 * - The URL is not malicious, ensuring no SQL injections, script tags, or relative path traversals.
 *
 * The URL is first checked using PHP's filter_var function, then it ensures that:
 * - The URL follows a valid format using a regular expression (regex).
 * - The host part of the URL is a valid IP address or a domain.
 * - It does not contain potentially harmful content, such as SQL injection patterns or script tags.
 * - It does not attempt relative path traversal (e.g., '../').
 *
 * Example valid URL:
 * - https://www.example.com
 *
 * Example invalid URL:
 * - http://localhost (does not contain a valid domain or external host).
 *
 * @package HeroQR\DataTypes
 */

class Url extends AbstractDataType
{
    public static function
validate(string $url): bool
   
{
       
$parsedUrl = parse_url($url);

        if (!
filter_var($url, FILTER_VALIDATE_URL) || empty($parsedUrl['host'])) {
            return
false;
        }

        if (
            !
preg_match('/^(https?:\/\/(?:[a-zA-Z0-9-]+\.)+[a-zA-Z0-9-]+(?:\/[^\s]*)?(\?[^\s]*)?(#\S*)?)$/i', $url)
            && !
filter_var($url, FILTER_VALIDATE_IP)
        ) {
            return
false;
        }

        if (
self::hasSqlInjection($url) || self::hasScriptTag($url) || preg_match('/(\.\.\/)/', $url)) {
            return
false;
        }

        return
true;
    }
}