<?php
namespace IMSGlobal\LTI;

class Cookie {
    public function get_cookie($name) {
        if (isset($_COOKIE[$name])) {
            return $_COOKIE[$name];
        }
        // Look for backup cookie if same site is not supported by the user's browser.
        if (isset($_COOKIE["LEGACY_" . $name])) {
            return $_COOKIE["LEGACY_" . $name];
        }
        return false;
    }

    public function set_cookie($name, $value, $exp = 3600, $options = []) {
        $cookie_options = [
            'expires' => time() + $exp
        ];

        // SameSite none and secure will be required for tools to work inside iframes
        $same_site_options = [
            'samesite' => 'None',
            'secure' => true
        ];

        if (PHP_VERSION_ID < 70300) {
          if (!function_exists('disallowsSameSiteNone') || !disallowsSameSiteNone()) {
        		// hack to add samesite
        		setcookie($name, $value,
              $cookie_options['expires'],
              '/; samesite=none');
          }
          // Set a second fallback cookie in the event that "SameSite" is not supported
          setcookie("LEGACY_" . $name, $value, $cookie_options['expires']);
        } else {
          if (!function_exists('disallowsSameSiteNone') || !disallowsSameSiteNone()) {
            setcookie($name, $value, array_merge($cookie_options, $same_site_options, $options));
          }
          // Set a second fallback cookie in the event that "SameSite" is not supported
          setcookie("LEGACY_" . $name, $value, array_merge($cookie_options, $options));
        }

        return $this;
    }
}
?>
