PHPIndex

This page lists files in the current directory. You can view content, get download/execute commands for Wget, Curl, or PowerShell, or filter the list using wildcards (e.g., `*.sh`).

BaconQrCodeProvider.php
wget 'https://lists2.roe3.org/hesk/vendor/robthree/twofactorauth/lib/Providers/Qr/BaconQrCodeProvider.php'
View Content
<?php

namespace RobThree\Auth\Providers\Qr;

use BaconQrCode\Writer;
use BaconQrCode\Renderer\ImageRenderer;
use BaconQrCode\Renderer\RendererStyle\RendererStyle;
use BaconQrCode\Renderer\RendererStyle\Fill;
use BaconQrCode\Renderer\Color\Rgb;
use BaconQrCode\Renderer\RendererStyle\EyeFill;

use BaconQrCode\Renderer\Image\EpsImageBackEnd;
use BaconQrCode\Renderer\Image\ImageBackEndInterface;
use BaconQrCode\Renderer\Image\ImagickImageBackEnd;
use BaconQrCode\Renderer\Image\SvgImageBackEnd;

class BaconQrCodeProvider implements IQRCodeProvider
{
    private $borderWidth = 4; // default from Bacon QR Code
    private $backgroundColour;
    private $foregroundColour;
    private $format;

    /**
     * Ensure we using the latest Bacon QR Code and specify default options
     *
     * @param int $borderWidth space around the QR code, 4 is the default from Bacon QR Code
     * @param string $backgroundColour hex reference for the background colour
     * @param string $foregroundColour hex reference for the foreground colour
     * @param string $format the desired output, png or svg
     */
    public function __construct($borderWidth = 4, $backgroundColour = '#ffffff', $foregroundColour = '#000000', $format = 'png')
    {
        if (! class_exists(ImagickImageBackEnd::class)) {
            throw new \RuntimeException('Make sure you are using version 2 of Bacon QR Code');
        }

        $this->borderWidth = $borderWidth;
        $this->backgroundColour = $this->handleColour($backgroundColour);
        $this->foregroundColour = $this->handleColour($foregroundColour);
        $this->format = strtolower($format);
    }

    /**
     * Standard functions from IQRCodeProvider
     */

    public function getMimeType()
    {
        switch ($this->format) {
            case 'png':
                return 'image/png';
            case 'gif':
                return 'image/gif';
            case 'jpg':
            case 'jpeg':
                return 'image/jpeg';
            case 'svg':
                return 'image/svg+xml';
            case 'eps':
                return 'application/postscript';
        }

        throw new \RuntimeException(sprintf('Unknown MIME-type: %s', $this->format));
    }

    public function getQRCodeImage($qrText, $size)
    {
        switch ($this->format) {
            case 'svg':
                $backend = new SvgImageBackEnd;
                break;
            case 'eps':
                $backend = new EpsImageBackEnd;
                break;
            default:
                $backend = new ImagickImageBackEnd($this->format);
        }

        $output = $this->getQRCodeByBackend($qrText, $size, $backend);

        if ($this->format == 'svg') {
            $svg = explode("\n", $output);
            return $svg[1];
        }

        return $output;
    }

    /**
     * Abstract QR code generation function
     * providing colour changing support
     */
    private function getQRCodeByBackend($qrText, $size, ImageBackEndInterface $backend)
    {
        $rendererStyleArgs = array($size, $this->borderWidth);

        if (is_array($this->foregroundColour) && is_array($this->backgroundColour)) {
            $rendererStyleArgs = array_merge($rendererStyleArgs, array(
                null,
                null,
                Fill::withForegroundColor(
                    new Rgb(...$this->backgroundColour),
                    new Rgb(...$this->foregroundColour),
                    new EyeFill(null, null),
                    new EyeFill(null, null),
                    new EyeFill(null, null)
                )
            ));
        }

        $writer = new Writer(new ImageRenderer(
            new RendererStyle(...$rendererStyleArgs),
            $backend
        ));

        return $writer->writeString($qrText);
    }

    /**
     * Ensure colour is an array of three values but also
     * accept a string and assume its a 3 or 6 character hex
     */
    private function handleColour($colour)
    {
        if (is_string($colour) && $colour[0] == '#') {
            $hexToRGB = function ($input) {
                // ensure input no longer has a # for more predictable division
                // PHP 8.1 does not like implicitly casting a float to an int
                $input = trim($input, '#');

                if (strlen($input) != 3 && strlen($input) != 6) {
                    throw new \RuntimeException('Colour should be a 3 or 6 character value after the #');
                }

                // split the array into three chunks
                $split = str_split($input, strlen($input) / 3);

                // cope with three character hex reference
                if (strlen($input) == 3) {
                    array_walk($split, function (&$character) {
                        $character = str_repeat($character, 2);
                    });
                }

                // convert hex to rgb
                return array_map('hexdec', $split);
            };

            return $hexToRGB($colour);
        }

        if (is_array($colour) && count($colour) == 3) {
            return $colour;
        }

        throw new \RuntimeException('Invalid colour value');
    }
}
BaseHTTPQRCodeProvider.php
wget 'https://lists2.roe3.org/hesk/vendor/robthree/twofactorauth/lib/Providers/Qr/BaseHTTPQRCodeProvider.php'
View Content
<?php

namespace RobThree\Auth\Providers\Qr;

abstract class BaseHTTPQRCodeProvider implements IQRCodeProvider
{
    /** @var bool */
    protected $verifyssl;

    /**
     * @param string $url
     *
     * @return string|bool
     */
    protected function getContent($url)
    {
        $curlhandle = curl_init();

        curl_setopt_array($curlhandle, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_CONNECTTIMEOUT => 10,
            CURLOPT_DNS_CACHE_TIMEOUT => 10,
            CURLOPT_TIMEOUT => 10,
            CURLOPT_SSL_VERIFYPEER => $this->verifyssl,
            CURLOPT_USERAGENT => 'TwoFactorAuth'
        ));
        $data = curl_exec($curlhandle);

        if (PHP_VERSION_ID < 80500) {
            curl_close($curlhandle);
        }
        return $data;
    }
}
EndroidQrCodeProvider.php
wget 'https://lists2.roe3.org/hesk/vendor/robthree/twofactorauth/lib/Providers/Qr/EndroidQrCodeProvider.php'
View Content
<?php
namespace RobThree\Auth\Providers\Qr;

use Endroid\QrCode\Color\Color;
use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelMedium;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelQuartile;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Writer\PngWriter;

class EndroidQrCodeProvider implements IQRCodeProvider
{
    public $bgcolor;
    public $color;
    public $margin;
    public $errorcorrectionlevel;

    protected $endroid4 = false;

    public function __construct($bgcolor = 'ffffff', $color = '000000', $margin = 0, $errorcorrectionlevel = 'H')
    {
        $this->endroid4 = method_exists(QrCode::class, 'create');

        $this->bgcolor = $this->handleColor($bgcolor);
        $this->color = $this->handleColor($color);
        $this->margin = $margin;
        $this->errorcorrectionlevel = $this->handleErrorCorrectionLevel($errorcorrectionlevel);
    }

    public function getMimeType()
    {
        return 'image/png';
    }

    public function getQRCodeImage($qrtext, $size)
    {
        if (!$this->endroid4) {
            return $this->qrCodeInstance($qrtext, $size)->writeString();
        }

        $writer = new PngWriter();
        return $writer->write($this->qrCodeInstance($qrtext, $size))->getString();
    }

    protected function qrCodeInstance($qrtext, $size)
    {
        $qrCode = new QrCode($qrtext);
        $qrCode->setSize($size);

        $qrCode->setErrorCorrectionLevel($this->errorcorrectionlevel);
        $qrCode->setMargin($this->margin);
        $qrCode->setBackgroundColor($this->bgcolor);
        $qrCode->setForegroundColor($this->color);

        return $qrCode;
    }

    private function handleColor($color)
    {
        $split = str_split($color, 2);
        $r = hexdec($split[0]);
        $g = hexdec($split[1]);
        $b = hexdec($split[2]);

        return $this->endroid4 ? new Color($r, $g, $b, 0) : ['r' => $r, 'g' => $g, 'b' => $b, 'a' => 0];
    }

    private function handleErrorCorrectionLevel($level)
    {
        switch ($level) {
            case 'L':
                return $this->endroid4 ? new ErrorCorrectionLevelLow() : ErrorCorrectionLevel::LOW();
            case 'M':
                return $this->endroid4 ? new ErrorCorrectionLevelMedium() : ErrorCorrectionLevel::MEDIUM();
            case 'Q':
                return $this->endroid4 ? new ErrorCorrectionLevelQuartile() : ErrorCorrectionLevel::QUARTILE();
            case 'H':
            default:
                return $this->endroid4 ? new ErrorCorrectionLevelHigh() : ErrorCorrectionLevel::HIGH();
        }
    }
}
EndroidQrCodeWithLogoProvider.php
wget 'https://lists2.roe3.org/hesk/vendor/robthree/twofactorauth/lib/Providers/Qr/EndroidQrCodeWithLogoProvider.php'
View Content
<?php
namespace RobThree\Auth\Providers\Qr;

use Endroid\QrCode\Logo\Logo;
use Endroid\QrCode\Writer\PngWriter;

class EndroidQrCodeWithLogoProvider extends EndroidQrCodeProvider
{
    protected $logoPath;
    protected $logoSize;

    /**
     * Adds an image to the middle of the QR Code.
     * @param string $path Path to an image file
     * @param array|int $size Just the width, or [width, height]
     */
    public function setLogo($path, $size = null)
    {
        $this->logoPath = $path;
        $this->logoSize = (array)$size;
    }

    public function getQRCodeImage($qrtext, $size)
    {
        if (!$this->endroid4) {
            return $this->qrCodeInstance($qrtext, $size)->writeString();
        }

        $logo = null;
        if ($this->logoPath) {
            $logo = Logo::create($this->logoPath);
            if ($this->logoSize) {
                $logo->setResizeToWidth($this->logoSize[0]);
                if (isset($this->logoSize[1])) {
                    $logo->setResizeToHeight($this->logoSize[1]);
                }
            }
        }
        $writer = new PngWriter();
        return $writer->write($this->qrCodeInstance($qrtext, $size), $logo)->getString();
    }

    protected function qrCodeInstance($qrtext, $size) {
        $qrCode = parent::qrCodeInstance($qrtext, $size);

        if (!$this->endroid4 && $this->logoPath) {
            $qrCode->setLogoPath($this->logoPath);
            if ($this->logoSize) {
                $qrCode->setLogoSize($this->logoSize[0], isset($this->logoSize[1]) ? $this->logoSize[1] : null);
            }
        }

        return $qrCode;
    }
}
GoogleChartsQrCodeProvider.php
wget 'https://lists2.roe3.org/hesk/vendor/robthree/twofactorauth/lib/Providers/Qr/GoogleChartsQrCodeProvider.php'
View Content
<?php

namespace RobThree\Auth\Providers\Qr;

// https://developers.google.com/chart/infographics/docs/qr_codes
class GoogleChartsQrCodeProvider extends BaseHTTPQRCodeProvider
{
    /** @var string */
    public $errorcorrectionlevel;

    /** @var int */
    public $margin;

    /** @var string */
    public $encoding;

    /**
     * @param bool $verifyssl
     * @param string $errorcorrectionlevel
     * @param int $margin
     * @param string $encoding
     */
    public function __construct($verifyssl = false, $errorcorrectionlevel = 'L', $margin = 4, $encoding = 'UTF-8')
    {
        if (!is_bool($verifyssl)) { 
            throw new QRException('VerifySSL must be bool'); 
        }

        $this->verifyssl = $verifyssl;

        $this->errorcorrectionlevel = $errorcorrectionlevel;
        $this->margin = $margin;
        $this->encoding = $encoding;
    }

    /**
     * {@inheritdoc}
     */
    public function getMimeType()
    {
        return 'image/png';
    }
    
    /**
     * {@inheritdoc}
     */
    public function getQRCodeImage($qrtext, $size)
    {
        return $this->getContent($this->getUrl($qrtext, $size));   
    }

    /**
     * @param string $qrtext the value to encode in the QR code
     * @param int|string $size the desired size of the QR code
     *
     * @return string file contents of the QR code
     */
    public function getUrl($qrtext, $size)
    {
        return 'https://chart.googleapis.com/chart'
            . '?chs=' . $size . 'x' . $size
            . '&chld=' . urlencode(strtoupper($this->errorcorrectionlevel) . '|' . $this->margin)
            . '&cht=' . 'qr'
            . '&choe=' . $this->encoding
            . '&chl=' . rawurlencode($qrtext);
    }
}
HandlesDataUri.php
wget 'https://lists2.roe3.org/hesk/vendor/robthree/twofactorauth/lib/Providers/Qr/HandlesDataUri.php'
View Content
<?php

namespace RobThree\Auth\Providers\Qr;

trait HandlesDataUri
{
	/**
     * @param string $datauri
     *
     * @return null|array
     */
    private function DecodeDataUri($datauri)
    {
        if (preg_match('/data:(?P<mimetype>[\w\.\-\+\/]+);(?P<encoding>\w+),(?P<data>.*)/', $datauri, $m) === 1) {
            return array(
                'mimetype' => $m['mimetype'],
                'encoding' => $m['encoding'],
                'data' => base64_decode($m['data'])
            );
        }

        return null;
    }
}
IQRCodeProvider.php
wget 'https://lists2.roe3.org/hesk/vendor/robthree/twofactorauth/lib/Providers/Qr/IQRCodeProvider.php'
View Content
<?php

namespace RobThree\Auth\Providers\Qr;

interface IQRCodeProvider
{
    /**
     * Generate and return the QR code to embed in a web page
     *
     * @param string $qrtext the value to encode in the QR code
     * @param int $size the desired size of the QR code
     *
     * @return string file contents of the QR code
     */
    public function getQRCodeImage($qrtext, $size);

    /**
     * Returns the appropriate mime type for the QR code
     * that will be generated
     *
     * @return string
     */
    public function getMimeType();
}
ImageChartsQRCodeProvider.php
wget 'https://lists2.roe3.org/hesk/vendor/robthree/twofactorauth/lib/Providers/Qr/ImageChartsQRCodeProvider.php'
View Content
<?php

namespace RobThree\Auth\Providers\Qr;

// https://image-charts.com
class ImageChartsQRCodeProvider extends BaseHTTPQRCodeProvider
{
    /** @var string */
    public $errorcorrectionlevel;

    /** @var int */
    public $margin;

    /**
     * @param bool $verifyssl
     * @param string $errorcorrectionlevel
     * @param int $margin
     */
    public function __construct($verifyssl = false, $errorcorrectionlevel = 'L', $margin = 1)
    {
        if (!is_bool($verifyssl)) {
            throw new QRException('VerifySSL must be bool');
        }

        $this->verifyssl = $verifyssl;

        $this->errorcorrectionlevel = $errorcorrectionlevel;
        $this->margin = $margin;
    }

    /**
     * {@inheritdoc}
     */
    public function getMimeType()
    {
        return 'image/png';
    }

    /**
     * {@inheritdoc}
     */
    public function getQRCodeImage($qrtext, $size)
    {
        return $this->getContent($this->getUrl($qrtext, $size));
    }

    /**
     * @param string $qrtext the value to encode in the QR code
     * @param int $size the desired size of the QR code
     *
     * @return string file contents of the QR code
     */
    public function getUrl($qrtext, $size)
    {
        return 'https://image-charts.com/chart?cht=qr'
            . '&chs=' . ceil($size / 2) . 'x' . ceil($size / 2)
            . '&chld=' . $this->errorcorrectionlevel . '|' . $this->margin
            . '&chl=' . rawurlencode($qrtext);
    }
}
QRException.php
wget 'https://lists2.roe3.org/hesk/vendor/robthree/twofactorauth/lib/Providers/Qr/QRException.php'
View Content
<?php

namespace RobThree\Auth\Providers\Qr;

use RobThree\Auth\TwoFactorAuthException;

class QRException extends TwoFactorAuthException {}
QRServerProvider.php
wget 'https://lists2.roe3.org/hesk/vendor/robthree/twofactorauth/lib/Providers/Qr/QRServerProvider.php'
View Content
<?php

namespace RobThree\Auth\Providers\Qr;

// http://goqr.me/api/doc/create-qr-code/
class QRServerProvider extends BaseHTTPQRCodeProvider
{
    /** @var string */
    public $errorcorrectionlevel;

    /** @var int */
    public $margin;

    /** @var int */
    public $qzone;

    /** @var string */
    public $bgcolor;

    /** @var string */
    public $color;

    /** @var string */
    public $format;

    /**
     * @param bool $verifyssl
     * @param string $errorcorrectionlevel
     * @param int $margin
     * @param int $qzone
     * @param string $bgcolor
     * @param string $color
     * @param string $format
     */
    public function __construct($verifyssl = false, $errorcorrectionlevel = 'L', $margin = 4, $qzone = 1, $bgcolor = 'ffffff', $color = '000000', $format = 'png')
    {
        if (!is_bool($verifyssl)) {
            throw new QRException('VerifySSL must be bool');
        }

        $this->verifyssl = $verifyssl;

        $this->errorcorrectionlevel = $errorcorrectionlevel;
        $this->margin = $margin;
        $this->qzone = $qzone;
        $this->bgcolor = $bgcolor;
        $this->color = $color;
        $this->format = $format;
    }

    /**
     * {@inheritdoc}
     */
    public function getMimeType()
    {
        switch (strtolower($this->format)) {
            case 'png':
                return 'image/png';
            case 'gif':
                return 'image/gif';
            case 'jpg':
            case 'jpeg':
                return 'image/jpeg';
            case 'svg':
                return 'image/svg+xml';
            case 'eps':
                return 'application/postscript';
        }
        throw new QRException(sprintf('Unknown MIME-type: %s', $this->format));
    }

    /**
     * {@inheritdoc}
     */
    public function getQRCodeImage($qrtext, $size)
    {
        return $this->getContent($this->getUrl($qrtext, $size));
    }

    /**
     * @param string $value
     *
     * @return string
     */
    private function decodeColor($value)
    {
        return vsprintf('%d-%d-%d', sscanf($value, "%02x%02x%02x"));
    }

    /**
     * @param string $qrtext the value to encode in the QR code
     * @param int|string $size the desired size of the QR code
     *
     * @return string file contents of the QR code
     */
    public function getUrl($qrtext, $size)
    {
        return 'https://api.qrserver.com/v1/create-qr-code/'
            . '?size=' . $size . 'x' . $size
            . '&ecc=' . strtoupper($this->errorcorrectionlevel)
            . '&margin=' . $this->margin
            . '&qzone=' . $this->qzone
            . '&bgcolor=' . $this->decodeColor($this->bgcolor)
            . '&color=' . $this->decodeColor($this->color)
            . '&format=' . strtolower($this->format)
            . '&data=' . rawurlencode($qrtext);
    }
}
QRicketProvider.php
wget 'https://lists2.roe3.org/hesk/vendor/robthree/twofactorauth/lib/Providers/Qr/QRicketProvider.php'
View Content
<?php

namespace RobThree\Auth\Providers\Qr;

// http://qrickit.com/qrickit_apps/qrickit_api.php
class QRicketProvider extends BaseHTTPQRCodeProvider
{
    /** @var string */
    public $errorcorrectionlevel;

    /** @var string */
    public $bgcolor;

    /** @var string */
    public $color;

    /** @var string */
    public $format;

    /**
     * @param string $errorcorrectionlevel
     * @param string $bgcolor
     * @param string $color
     * @param string $format
     */
    public function __construct($errorcorrectionlevel = 'L', $bgcolor = 'ffffff', $color = '000000', $format = 'p')
    {
        $this->verifyssl = false;

        $this->errorcorrectionlevel = $errorcorrectionlevel;
        $this->bgcolor = $bgcolor;
        $this->color = $color;
        $this->format = $format;
    }

    /**
     * {@inheritdoc}
     */
    public function getMimeType()
    {
        switch (strtolower($this->format)) {
            case 'p':
                return 'image/png';
            case 'g':
                return 'image/gif';
            case 'j':
                return 'image/jpeg';
        }
        throw new QRException(sprintf('Unknown MIME-type: %s', $this->format));
    }

    /**
     * {@inheritdoc}
     */
    public function getQRCodeImage($qrtext, $size)
    {
        return $this->getContent($this->getUrl($qrtext, $size));
    }

    /**
     * @param string $qrtext the value to encode in the QR code
     * @param int|string $size the desired size of the QR code
     *
     * @return string file contents of the QR code
     */
    public function getUrl($qrtext, $size)
    {
        return 'http://qrickit.com/api/qr'
            . '?qrsize=' . $size
            . '&e=' . strtolower($this->errorcorrectionlevel)
            . '&bgdcolor=' . $this->bgcolor
            . '&fgdcolor=' . $this->color
            . '&t=' . strtolower($this->format)
            . '&d=' . rawurlencode($qrtext);
    }
}