<?php
namespace Core;

class XSS
{
    public static function avoid($args = [])
    {
        if ($args) {
            if (array_key_exists('exclude', $args)) {
                $excludes = $args['exclude'];

                foreach ($excludes as $exclude) {
                    $includes[$exclude] = $args[$exclude];
                    unset($args[$exclude]);
                }

                unset($args['exclude']);
            }

            foreach ($args as $key => $value) {
                $args[$key] = htmlspecialchars($value);
            }

            // Re-include excluded data
            if (isset($includes)) {
                foreach ($includes as $key => $value) {
                    $args[$key] = $value;
                }
            }

            return $args;
        }
    }

    public static function decode($args = []) {
        if ($args) {
            if (array_key_exists('exclude', $args)) {
                $excludes = $args['exclude'];

                foreach ($excludes as $exclude) {
                    $includes[$exclude] = $args[$exclude];
                    unset($args[$exclude]);
                }

                unset($args['exclude']);
            }

            foreach ($args as $key => $value) {
                $args[$key] = htmlspecialchars_decode($value);
            }

            // Re-include excluded data
            if (isset($includes)) {
                foreach ($includes as $key => $value) {
                    $args[$key] = $value;
                }
            }

            return $args;
        }
    }
}
