Advertisement
petschko

EU Link law class

Nov 20th, 2015
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.19 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Author: Peter Dragicevic [peter-91@hotmail.de]
  4.  * Authors-Website: http://petschko.org/
  5.  * Date: 19.11.2015
  6.  * Time: 23:46
  7.  * Update: 09.04.2016
  8.  * Version: 1.0.3 (Changed Class-Name & Website)
  9.  * 1.0.2 (Reformat Code)
  10.  *
  11.  * Licence: http://creativecommons.org/licenses/by-sa/4.0/
  12.  * You are free to use this!
  13.  *
  14.  * This script help you to hide Links within the EU, you can setup multiply things:
  15.  * link::setDisabledLinkText(string $text) = Setup the message, if the Link is not shown
  16.  * link::setEnableLinksAllTime(true/false) = true will ALWAYS shown ALL Links, false will check User Country & Link visibility
  17.  * var $countries = Setup the country List, have can save them in whatever format you like... eg: array("de", "en"); or array("Germany", "England"); this depend on a script how you detect the country
  18.  * var $whiteList = Setup the List-Mode, white-list (true) will ONLY show ALWAYS links to countries that are in the country-list, false will show ALWAYS links to countries they are NOT in the country list
  19.  * link::setUserCountry(string $country) = Set the current user country, set this to false or none will ignore countries
  20.  *
  21.  * How to setup a Link
  22.  * 1. You can create a new Link object and display the link
  23.  * $var = new link($href[, string $linkText = false[, bool $alwaysVisible = true[, string $htmlAttributes = false]]]);
  24.  * echo $var->show();
  25.  *
  26.  * 2. Or create a new object and show it instant
  27.  * echo link::showNew($href[, string $linkText = false[, bool $alwaysVisible = true[, string $htmlAttributes = false]]]);
  28.  */
  29.  
  30. /**
  31.  * Class Link
  32.  */
  33. class Link {
  34.     private static $disabledLinkText = 'Link disabled....'; // Message if link is hidden....
  35.     private static $enableLinksAllTime = true; // True = always show ALL links | False = Show only links outside the affected countries and the links that are always visible
  36.     private static $countries = array(); // Setup country-List
  37.     private static $whiteList = false; // Set this on true, the links will ONLY shown always in countries that are in the country array | false will invert this
  38.     private static $userCountry = false; // Do not touch this
  39.     private $href;
  40.     private $text;
  41.     private $alwaysVisible;
  42.     private $htmlAttr;
  43.  
  44.     /**
  45.      * Create a new Instance of a Link class
  46.      *
  47.      * @param string $href - The url you want to link - eg http://www.google.com/
  48.      * @param bool|string $text - LinkText (between <a></a>) - Set this on false will use the link as text
  49.      * @param bool $alwaysVisible - Set if link is always shown, set it to true even if your global settings disallow links (May if you have right to setup this link) else set this on false!!!
  50.      * @param bool|string $htmlAttr - Additional HTML attributes like -> class="link" or -> id="myLink" or some other html attributes
  51.      */
  52.     public function __construct($href, $text = false, $alwaysVisible = true, $htmlAttr = false) {
  53.         $this->setHref($href);
  54.         $this->setText($text);
  55.         $this->setAlwaysVisible($alwaysVisible);
  56.         $this->setHtmlAttr($htmlAttr);
  57.     }
  58.  
  59.     /**
  60.      * Clears Memory
  61.      */
  62.     public function __destruct() {
  63.         unset($this->href);
  64.         unset($this->text);
  65.         unset($this->alwaysVisible);
  66.         unset($this->htmlAttr);
  67.     }
  68.  
  69.     /**
  70.      * Get the disabled Link message
  71.      *
  72.      * @return string - The Link-Hidden message
  73.      */
  74.     public static function getDisabledLinkText() {
  75.         return self::$disabledLinkText;
  76.     }
  77.  
  78.     /**
  79.      * Set the disabled Link message
  80.      *
  81.      * @param string $disabledLinkText - The Text that will be shown if the link is hidden
  82.      */
  83.     public static function setDisabledLinkText($disabledLinkText) {
  84.         self::$disabledLinkText = $disabledLinkText;
  85.     }
  86.  
  87.     /**
  88.      * Shows if the link is always visible
  89.      *
  90.      * @return boolean - true = link is always visible | false = only if specified or not in the country list
  91.      */
  92.     private function isAlwaysVisible() {
  93.         return $this->alwaysVisible;
  94.     }
  95.  
  96.     /**
  97.      * Set this link is always visible (or not)
  98.      *
  99.      * @param boolean $alwaysVisible - true = link is always visible | false = only if specified or not in the country list
  100.      */
  101.     private function setAlwaysVisible($alwaysVisible) {
  102.         $this->alwaysVisible = $alwaysVisible;
  103.     }
  104.  
  105.     /**
  106.      * Get the current country of the User
  107.      *
  108.      * @return string|bool - Country of the current user
  109.      */
  110.     private static function getUserCountry() {
  111.         return self::$userCountry;
  112.     }
  113.  
  114.     /**
  115.      * Set the current country of the user
  116.      *
  117.      * @param string|bool $userCountry - Set the country of the current user - false means ignore country
  118.      */
  119.     public static function setUserCountry($userCountry) {
  120.         self::$userCountry = $userCountry;
  121.     }
  122.  
  123.     /**
  124.      * Get country List
  125.      *
  126.      * @return array - Country List
  127.      */
  128.     private static function getCountries() {
  129.         return self::$countries;
  130.     }
  131.  
  132.     /**
  133.      * Check if the current List-Mode is whitelist or blacklist
  134.      *
  135.      * @return boolean - True = White-List-Mode | False = Black-List-Mode
  136.      */
  137.     private static function isWhiteList() {
  138.         return self::$whiteList;
  139.     }
  140.  
  141.     /**
  142.      * Returns the Link (or not^^)
  143.      *
  144.      * @return string - Link or Notice about hidden Link
  145.      */
  146.     public function show() {
  147.         if(! $this->getHref())
  148.             return 'INVALID LINK -> Set href!!';
  149.  
  150.         // Set Links as Text if no text is set
  151.         if(! $this->getText())
  152.             $this->setText($this->getHref());
  153.  
  154.         if($this->checkVisibility())
  155.             return '<a href="' . $this->getHref() . '"' . (($this->getHtmlAttr()) ? ' ' . $this->getHtmlAttr() : '') . '>' . $this->getText() . '</a>';
  156.         else
  157.             return '<span class="linkHidden">' . self::getDisabledLinkText() . '</span>';
  158.     }
  159.  
  160.     /**
  161.      * Create a new Instance of a Link class - and shows direct the link
  162.      *
  163.      * @param string $href - The url you want to link - eg http://www.google.com/
  164.      * @param bool|string $text - LinkText (between <a></a>) - Set this on false will use the link as text
  165.      * @param bool $alwaysVisible - Set if link is always shown, set it to true even if your global settings disallow links (May if you have right to setup this link) else set this on false!!!
  166.      * @param bool|string $htmlAttr - Additional HTML attributes like -> class="link" or -> id="myLink" or some other html attributes
  167.      * @return string - The Link (Or not^^)
  168.      */
  169.     public static function showNew($href, $text = false, $alwaysVisible = true, $htmlAttr = false) {
  170.         $link = new self($href, $text, $alwaysVisible, $htmlAttr);
  171.         return $link->show();
  172.     }
  173.  
  174.     /**
  175.      * Check if the Link can seen by the User
  176.      *
  177.      * @return bool - true = yes | false = no
  178.      */
  179.     private function checkVisibility() {
  180.         // Check if it is always enabled for ALL
  181.         if(self::isEnableLinksAllTime())
  182.             return true;
  183.  
  184.         // Check country
  185.         if(self::checkCountryVisibility())
  186.             return true;
  187.  
  188.         // Check if link itself is visible if al other not allows to see it...
  189.         if($this->isAlwaysVisible())
  190.             return true;
  191.  
  192.         // Link is not visible to the user
  193.         return false;
  194.     }
  195.  
  196.     /**
  197.      * Check if users Country can always see links
  198.      *
  199.      * @return bool - true = yes | false = no
  200.      */
  201.     private static function checkCountryVisibility() {
  202.         // If none UserCountry is set ignore it
  203.         if(! self::getUserCountry())
  204.             return false;
  205.  
  206.         foreach(self::getCountries() as $country) {
  207.             // Check if userCountry is in List
  208.             if($country == self::getUserCountry()) {
  209.  
  210.                 // Return correct value by ListType
  211.                 if(self::isWhiteList())
  212.                     return true;
  213.                 else
  214.                     return false;
  215.             }
  216.         }
  217.  
  218.         // If user Country is not in list return correct value by List-Type
  219.         if(self::isWhiteList())
  220.             return false;
  221.         else
  222.             return true;
  223.     }
  224.  
  225.     /**
  226.      * Check if ALL links are visible
  227.      *
  228.      * @return boolean - True = always show ALL links | False = Show only links outside the affected countries and the links that are always visible
  229.      */
  230.     private static function isEnableLinksAllTime() {
  231.         return self::$enableLinksAllTime;
  232.     }
  233.  
  234.     /**
  235.      * Set if ALL links are visible
  236.      *
  237.      * @param boolean $enableLinksAllTime - True = always show ALL links | False = Show only links outside the affected countries and the links that are always visible
  238.      */
  239.     public static function setEnableLinksAllTime($enableLinksAllTime) {
  240.         self::$enableLinksAllTime = $enableLinksAllTime;
  241.     }
  242.  
  243.     /**
  244.      * Get the URL of this link
  245.      *
  246.      * @return string - Link-URL
  247.      */
  248.     private function getHref() {
  249.         if(! isset($this->href))
  250.             return false;
  251.  
  252.         return $this->href;
  253.     }
  254.  
  255.     /**
  256.      * Set the URL of this link
  257.      *
  258.      * @param string $href - Link-URL
  259.      */
  260.     private function setHref($href) {
  261.         $this->href = $href;
  262.     }
  263.  
  264.     /**
  265.      * Get the Link-Text (Text between <a> and </a>)
  266.      *
  267.      * @return string - LinkText
  268.      */
  269.     private function getText() {
  270.         if(! $this->text)
  271.             $this->setText($this->getHref());
  272.  
  273.         return $this->text;
  274.     }
  275.  
  276.     /**
  277.      * Set the Link-Text (Text between <a> and </a>)
  278.      *
  279.      * @param bool|string $text - LinkText - false for URL as Text
  280.      */
  281.     private function setText($text) {
  282.         $this->text = $text;
  283.     }
  284.  
  285.     /**
  286.      * Get additional HTML-Attributes
  287.      *
  288.      * @return bool|string - HTML attributes - false means none
  289.      */
  290.     private function getHtmlAttr() {
  291.         return $this->htmlAttr;
  292.     }
  293.  
  294.     /**
  295.      * Set additional HTML-Attributes
  296.      *
  297.      * @param bool|string $htmlAttr - HTML attributes - false means none
  298.      */
  299.     private function setHtmlAttr($htmlAttr) {
  300.         $this->htmlAttr = $htmlAttr;
  301.     }
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement