* @link https://TheTempusProject.com/Core * @license https://opensource.org/licenses/MIT [MIT LICENSE] */ namespace TheTempusProject\Bedrock\Functions; use TheTempusProject\Canary\Canary as Debug; class Cookie { /** * Checks whether $data is a valid saved cookie or not. * * @param {string} [$data] - Name of the cookie to check for. * @return {bool} */ public static function exists( $data ) { if ( !Check::dataTitle( $data ) ) { return false; } $cookieName = DEFAULT_COOKIE_PREFIX . $data; if ( isset( $_COOKIE[$cookieName] ) ) { Debug::log( "Cookie found: $data" ); return true; } Debug::info( "Cookie not found: $data" ); return false; } /** * Returns a specific cookie if it exists. * * @param {string} [$data] - Cookie to retrieve data from. * @return {bool|string} - String from the requested cookie, or false if the cookie does not exist. */ public static function get( $data ) { if ( !Check::dataTitle( $data ) ) { return false; } if ( self::exists( $data ) ) { $cookieName = DEFAULT_COOKIE_PREFIX . $data; return $_COOKIE[$cookieName]; } return false; } /** * Create cookie function. * * @param {string} [$name] - Cookie name. * @param {string} [$value] - Cookie value. * @param {int} [$expiry] - How long (in seconds) until the cookie should expire. * @return {bool} */ public static function put( $name, $value, $expire = null ) { if ( ! Check::dataTitle( $name ) ) { return false; } if ( ! $expire ) { $expire = time() + DEFAULT_COOKIE_EXPIRATION; } if ( ! Check::ID( $expire ) ) { return false; } $cookieName = DEFAULT_COOKIE_PREFIX . $name; $test = setcookie( $cookieName, $value, $expire, '/' ); if ( ! $test ) { Debug::error( "Cookie not created: '$name', until: $expire" ); return false; } Debug::debug( "Cookie Created: $name till $expire" ); return true; } /** * Delete cookie function. * * @param {string} [$name] - Name of cookie to be deleted. */ public static function delete( $name ) { if ( !Check::dataTitle( $name ) ) { return false; } $cookieName = DEFAULT_COOKIE_PREFIX . $name; setcookie( $cookieName, '', ( time() - 1 ), '/' ); Debug::log( "Cookie deleted: $name" ); return true; } }