<commit_before><?php

namespace fkooman\VPN\UserPortal;

use fkooman\Http\Exception\BadRequestException;
use ZipArchive;

class Utils
{
    public static function validateConfigName($configName)
    {
        if (null === $configName) {
            throw new BadRequestException('missing parameter');
        }
        if (!is_string($configName)) {
            throw new BadRequestException('malformed parameter');
        }
        if (32 < strlen($configName)) {
            throw new BadRequestException('name too long, maximum 32 characters');
        }
        // XXX: be less restrictive in supported characters...
        if (0 === preg_match('/^[a-zA-Z0-9-_.@]+$/', $configName)) {
            throw new BadRequestException('invalid characters in name');
        }
    }

    public static function configToZip($configName, $configData)
    {
        $inlineTypeFileName = array(
            'ca' => sprintf('%s_ca.crt', $configName),
            'cert' => sprintf('%s_client.crt', $configName),
            'key' => sprintf('%s_client.key', $configName),
            'tls-auth' => sprintf('%s_ta.key', $configName),
        );
        $zipName = tempnam(sys_get_temp_dir(), 'vup_');
        $z = new ZipArchive();
        $z->open($zipName, ZipArchive::CREATE);
        foreach (array('cert', 'ca', 'key', 'tls-auth') as $inlineType) {
            $pattern = sprintf('/\<%s\>(.*)\<\/%s\>/msU', $inlineType, $inlineType);
            if (1 !== preg_match($pattern, $configData, $matches)) {
                throw new DomainException('inline type not found');
            }
            $configData = preg_replace(
                $pattern,
                sprintf(
                    '%s %s',
                    $inlineType,
                    $inlineTypeFileName[$inlineType]
                ),
                $configData
            );
            $z->addFromString($inlineTypeFileName[$inlineType], trim($matches[1]));
        }
        // remove "key-direction X" and add it to tls-auth line as last
        // parameter (hack to make NetworkManager import work)
        $configData = str_replace(
            array(
                'key-direction 1',
                sprintf('tls-auth %s_ta.key', $configName),
            ),
            array(
                '',
                sprintf('tls-auth %s_ta.key 1', $configName),
            ),
            $configData
        );
        $z->addFromString(sprintf('%s.ovpn', $configName), $configData);
        $z->close();
        $zipData = file_get_contents($zipName);
        unlink($zipName);

        return $zipData;
    }
}<commit_msg>update ZIP file generation with new config template<commit_after><?php

namespace fkooman\VPN\UserPortal;

use fkooman\Http\Exception\BadRequestException;
use ZipArchive;

class Utils
{
    public static function validateConfigName($configName)
    {
        if (null === $configName) {
            throw new BadRequestException('missing parameter');
        }
        if (!is_string($configName)) {
            throw new BadRequestException('malformed parameter');
        }
        if (32 < strlen($configName)) {
            throw new BadRequestException('name too long, maximum 32 characters');
        }
        // XXX: be less restrictive in supported characters...
        if (0 === preg_match('/^[a-zA-Z0-9-_.@]+$/', $configName)) {
            throw new BadRequestException('invalid characters in name');
        }
    }

    public static function configToZip($configName, $configData)
    {
        $inlineTypeFileName = array(
            'ca' => sprintf('%s_ca.crt', $configName),
            'cert' => sprintf('%s_client.crt', $configName),
            'key' => sprintf('%s_client.key', $configName),
            'tls-auth' => sprintf('%s_ta.key', $configName),
        );
        $zipName = tempnam(sys_get_temp_dir(), 'vup_');
        $z = new ZipArchive();
        $z->open($zipName, ZipArchive::CREATE);

        foreach (array('cert', 'ca', 'key', 'tls-auth') as $inlineType) {

            // replace the inline rules with actual file names
            $configData = str_replace(
                sprintf('%s [inline]', $inlineType),
                sprintf('%s %s', $inlineType, $inlineTypeFileName[$inlineType]),
                $configData
            );

            // remove the inline blocks
            $pattern = sprintf('/\<%s\>(.*)\<\/%s\>/msU', $inlineType, $inlineType);
            if (1 !== preg_match($pattern, $configData, $matches)) {
                throw new DomainException('inline type not found');
            }
            $configData = preg_replace(
                $pattern,
                '',
                $configData
            );

            // add the file to the zip
            $z->addFromString($inlineTypeFileName[$inlineType], trim($matches[1]));
        }

        // add the ovpn config to the ZIP
        $z->addFromString(sprintf('%s.ovpn', $configName), $configData);
        $z->close();
        $zipData = file_get_contents($zipName);
        unlink($zipName);

        return $zipData;
    }
}
{'fraction_non_alphanumeric': 0.143646408839779, 'fraction_numerical': 0.003946329913180742, 'mean_word_length': 1.97651203758074, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '507946', 'n_tokens_mistral': 1455, 'n_tokens_neox': 1407, 'n_words': 315}