<?php
	// namespace
	namespace sven\sys\security;

	/**
	 * cookie
	 *
	 * Wrapping cookie functions
	 *
	 * @package sven\sys\security
	 * @copyright 2018 Ruben Meyer
	 * @author Ruben Meyer <contact@rxbn.de>
	 * @version 0.1.0
	 *
	 */
	class cookie {

		public const EXPIRE_TIME = 1800; // in seconds (30mins)

		/**
		 * destructor
		 */
		public function __destruct() {
			$cks = $_COOKIE;
			foreach ($cks as $key => $value) {
				$this->write($key, $value, time() + self::EXPIRE_TIME);
			}
		}

		/**
		 * write
		 *
		 * create a cookie with a specific value
		 *
		 * @access public
		 * @param string $key Cookie name
		 * @param string $value Cookie value
		 * @param int $expire Expiring timestamp as UNIX timestamp (2018/12/15 12:14:13 => 1544876053) (0 => cookie will be deleted with session end)
		 * @param string $path URL
		 * @param string $domain Domain/Subdomain name where the cookie will be accessable
		 * @param boolean $secure HTTP Secure Flag
		 * @param boolean $httponly Unreadability for Javascript (XSS Security)
		 * @return void
		 */
		public function write($key, $value = "", $expire = 0, $path = "/", $domain = null, $secure = false, $httponly = false) {
			setcookie($key, $value, $expire, $path, $domain, $secure, $httponly);
		}

		/**
		 * read
		 *
		 * read a cookie
		 *
		 * @access public
		 * @param string $key Cookie name
		 * @return string|boolean
		 */
		public function read($key) {
			return (isset($_COOKIE[$key]) ? $_COOKIE[$key]: false);
		}

		/**
		 * remove
		 *
		 * remove a cookie
		 *
		 * @access public
		 * @param string $key Cookie name
		 * @return void
		 */
		public function remove($key) {
			unset($_COOKIE[$key]);
			$this->write($key, "", time()-3600);
		}

		/**
		 * drop
		 *
		 * drops all cookies
		 *
		 * @access public
		 * @return void
		 */
		public function drop() {
			foreach ($_COOKIE as $key => $value) {
				$this->remove($key);
			}
		}
	};

?>
