Advertisement
petschko

Cookie-Europe class

Jul 22nd, 2015
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.00 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Author: Peter Dragicevic [peter-91@hotmail.de]
  4.  * Authors-Website: http://petschko.org/
  5.  * Date: 22.07.2015
  6.  * Time: 09:07
  7.  * Update: 09.04.2016
  8.  * Version 1.0.3 (Changed Class-Name & Website)
  9.  * 1.0.2 (Reformat Code - Fixed head doc)
  10.  *
  11.  * Licence: http://creativecommons.org/licenses/by-sa/4.0/
  12.  * You are free to use this!
  13.  * Come to us we have cookies *.* Or Play Cookie-Clicker!^^
  14.  *
  15.  * How to use:
  16.  * Set cookie only if its allowed -- cookie::setcookie(@vanilla_params) -> See http://php.net/setcookie for parameter use
  17.  * Read cookie if its allowed -- cookie::getCookie((string)cookieName[, (mixed)fallback_value = false[, (bool)force_read = false]])
  18.  * Check if cookies are allowed (for displaying msg or something like that) -- cookie::getIsAllowed()
  19.  * Enable Cookies, if user clicked on accept button -- cookie::enableCookies()
  20.  * Disable Cookies (for not showing cookie msg anymore or something like that),
  21.  * but inform user that there IS one Cookie (MasterCookie) if he want never show message -- cookie::enableCookies(false)
  22.  * (Very-Optional) Set the country of the User (for allow/deny cookies without asking in countries you have set) -- cookie::setCountry((string)country)
  23.  *
  24.  * You can make more configurations below (Marked with CONFIG AREA)
  25.  */
  26.  
  27. /**
  28.  * Class Cookie
  29.  *
  30.  * Static Object
  31.  */
  32. class Cookie { // Don't touch this line, until you are the the richest man on the world!
  33.     // CONFIG AREA -------------------------------------------------------------------------------
  34.     private static $ignoreCookiePolice = Config::cookiePoliceSet;
  35.     private static $countryList = array();
  36.     private static $countryModeWhiteList = Config::cookiePoliceCountryModeWhitelist;
  37.     private static $masterCookieName = 'allow_cookies';
  38.     private static $masterCookieExpireTime = 31536000; // Default 1 Year from creation (counted in secs)
  39.     // END OF CONFIG-AREA ------------------------------------------------------------------------
  40.  
  41.     private static $country = false; // Don't change!
  42.     private static $isAllowed; // Don't change!
  43.  
  44.     /**
  45.      * Disabled Constructor
  46.      */
  47.     private function __construct() {}
  48.  
  49.     /**
  50.      * Sets a Cookie like the normal setcookie function ---- See documentation: http://php.net/setcookie
  51.      *
  52.      * @param string $name - Cookie Name
  53.      * @param string $value - Value of the cookie
  54.      * @param int $expire - Expire-Date in UNIX-Timestamp
  55.      * @param string|null $path - Cookie-Path on your Server
  56.      * @param string|null $domain - Cookie-Domain
  57.      * @param bool|null $secure - Send only if HTTPS
  58.      * @param bool|null $httpOnly - Cookie is only readable via HTTP-Protocol
  59.      * @return bool - true on success
  60.      */
  61.     public static function setcookie($name, $value, $expire = 0, $path = null, $domain = null, $secure = null, $httpOnly = null) {
  62.         if(self::cookiesAllowed() || self::isIgnoreCookiePolice())
  63.             return setcookie($name, $value, $expire, $path, $domain, $secure, $httpOnly);
  64.         return false;
  65.     }
  66.  
  67.     /**
  68.      * Get the value of a cookie (if allowed) or get a default value
  69.      *
  70.      * @param string $cookieName - Name of the Cookie
  71.      * @param mixed $defaultValue - Return value if deny read cookies
  72.      * @param bool $forceRead - Shows the cookie, no matter what, except it is not set
  73.      * @return mixed - Cookie-Value or $default_value
  74.      */
  75.     public static function getCookie($cookieName, $defaultValue = false, $forceRead = false) {
  76.         if($forceRead || self::cookiesAllowed()) {
  77.             if(! isset($_COOKIE[$cookieName]))
  78.                 $_COOKIE[$cookieName] = '';
  79.  
  80.             return $_COOKIE[$cookieName];
  81.         }
  82.  
  83.         return $defaultValue;
  84.     }
  85.  
  86.     /**
  87.      * Checks the Master-Cookie if cookies are allowed
  88.      *
  89.      * @return bool - true if cookies are allowed to set/read
  90.      */
  91.     private static function cookiesAllowed() {
  92.         if(! isset($_COOKIE[self::getMasterCookieName()]))
  93.             $_COOKIE[self::getMasterCookieName()] = false;
  94.  
  95.         if($_COOKIE[self::getMasterCookieName()] == 'allow' || self::isIgnoreCookiePolice() || self::allowCountryCookie())
  96.             return true;
  97.         return false;
  98.     }
  99.  
  100.     /**
  101.      * Checks if the user is in an allowed country
  102.      *
  103.      * @return bool - true if country is allowed to use cookies without asking
  104.      */
  105.     private static function allowCountryCookie() {
  106.         if(self::isCountryModeWhiteList()) {
  107.             foreach(self::getCountryList() as $country) {
  108.                 if($country == self::getCountry())
  109.                     return true;
  110.             }
  111.             // Default return value of whitelist
  112.             return false;
  113.         } else {
  114.             foreach(self::getCountryList() as $country) {
  115.                 if($country == self::getCountry())
  116.                     return false;
  117.             }
  118.             // Default return value of blacklist
  119.             return true;
  120.         }
  121.     }
  122.  
  123.     /**
  124.      * Enabled/Disabled Cookies
  125.      *
  126.      * @param bool $enabled - Enable cookies, set this to false to disable them
  127.      */
  128.     public static function enableCookies($enabled = true) {
  129.         if($enabled)
  130.             $value = 'allow';
  131.         else
  132.             $value = 'deny';
  133.  
  134.         // Set Master Cookie :3
  135.         setcookie(self::getMasterCookieName(), $value, time() + self::getMasterCookieExpireTime());
  136.     }
  137.  
  138.     /**
  139.      * @return bool|string - User Country
  140.      */
  141.     public static function getCountry() {
  142.         if(! self::$country)
  143.             return 'None';
  144.         return self::$country;
  145.     }
  146.  
  147.     /**
  148.      * Set the User-Country
  149.      *
  150.      * @param string $country - Sets user country | recommend 2 letter country names
  151.      */
  152.     public static function setCountry($country) {
  153.         self::$country = $country;
  154.     }
  155.  
  156.     /**
  157.      * Shows if Cookies are allowed
  158.      *
  159.      * @return boolean - shows true if cookies are allowed
  160.      */
  161.     public static function getIsAllowed() {
  162.         if(! isset(self::$isAllowed))
  163.             self::setIsAllowed(self::cookiesAllowed());
  164.         return self::$isAllowed;
  165.     }
  166.  
  167.     /**
  168.      * Set allowed to true/false
  169.      *
  170.      * @param bool $isAllowed - isAllowed
  171.      */
  172.     private static function setIsAllowed($isAllowed) {
  173.         self::$isAllowed = $isAllowed;
  174.     }
  175.  
  176.     /**
  177.      * Set ignore CookiePolice
  178.      *
  179.      * @return boolean - True if cookies are always allowed
  180.      */
  181.     private static function isIgnoreCookiePolice() {
  182.         return self::$ignoreCookiePolice;
  183.     }
  184.  
  185.     /**
  186.      * Get the CountryList
  187.      *
  188.      * @return array - List of countries
  189.      */
  190.     private static function getCountryList() {
  191.         return self::$countryList;
  192.     }
  193.  
  194.     /**
  195.      * Set the CountryList type (white/blacklist)
  196.      *
  197.      * @return boolean - true is whitelist mode and false is blacklist mode for countries
  198.      */
  199.     private static function isCountryModeWhiteList() {
  200.         return self::$countryModeWhiteList;
  201.     }
  202.  
  203.     /**
  204.      * Get the name of the MasterCookie
  205.      *
  206.      * @return string - MasterCookie Name
  207.      */
  208.     public static function getMasterCookieName() {
  209.         return self::$masterCookieName;
  210.     }
  211.  
  212.     /**
  213.      * Get the MasterCookie expire time (secs)
  214.      *
  215.      * @return int - The expire time of the masterCookie (secs)
  216.      */
  217.     public static function getMasterCookieExpireTime() {
  218.         return self::$masterCookieExpireTime;
  219.     }
  220.  
  221.     /**
  222.      * Set the MasterCookie expire time (secs)
  223.      *
  224.      * @param int $masterCookieExpireTime - New MasterCookie expire time (secs)
  225.      */
  226.     public static function setMasterCookieExpireTime($masterCookieExpireTime) {
  227.         self::$masterCookieExpireTime = $masterCookieExpireTime;
  228.     }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement