Advertisement
alpa_s

Untitled

Jan 24th, 2018
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.35 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: alpas
  5.  * Date: 23.01.18
  6.  * Time: 10:34
  7.  */
  8.  
  9. class Cache
  10. {
  11.     /** @var $server Memcached */
  12.     public $server;
  13.  
  14.     public function __construct()
  15.     {
  16.         $mem = new Memcached();
  17.         $mem->addServer('127.0.0.1', 11211);
  18.         $this->server = $mem;
  19.     }
  20.  
  21.     public static function buildKey($key)
  22.     {
  23.         return ctype_alnum($key) && Helper::byteLength($key) <= 32 ? $key : md5($key);
  24.     }
  25. }
  26.  
  27. class Helper
  28. {
  29.     /**
  30.      * Returns the number of bytes in the given string.
  31.      * This method ensures the string is treated as a byte array by using `mb_strlen()`.
  32.      * @param string $string the string being measured for length
  33.      * @return int the number of bytes in the given string.
  34.      */
  35.     public static function byteLength($string)
  36.     {
  37.         return mb_strlen($string, '8bit');
  38.     }
  39. }
  40.  
  41. class Cookie
  42. {
  43.     public function __construct()
  44.     {
  45.         $this->params = $_COOKIE;
  46.     }
  47.  
  48.     public function getLocale()
  49.     {
  50.         $locale = false;
  51.         if (isset($this->params['_locale'])) {
  52.             $string = stristr($this->params['_locale'], 'a:');
  53.             try {
  54.                 $locale = unserialize($string);
  55.                 $locale = $locale[1];
  56.             } catch (Exception $e) {
  57.             }
  58.         }
  59.         return $locale;
  60.  
  61.     }
  62.  
  63.     public function add($name, $value, $expires = null, $path = null, $domain = null, $secure = null)
  64.     {
  65.         setcookie($name, $value, $expires, $path, $domain, $secure);
  66.     }
  67.  
  68.     public function setLinksCookies()
  69.     {
  70.         $request = new Request();
  71.         if (array_key_exists('s_link', $this->params)) {
  72.             $founded = false;
  73.             if ($request->validate()) {
  74.                 foreach ($request->params as $key => $value) {
  75.                     if (stristr($key, "utm_") != false) {
  76.                         $founded = true;
  77.                     }
  78.                 }
  79.                 if ($founded || array_key_exists('s_trk', $request->params)) {
  80.                     $this->add('s_link', $request->getQueryString(), time() + 60 * 60 * 24 * 30 * 3, '/', '.bitcomo.com');
  81.                 }
  82.             }
  83.         } else {
  84.             $this->add('s_link', $request->getQueryString(), time() + 60 * 60 * 24 * 30 * 3, '/', '.bitcomo.com');
  85.         }
  86.     }
  87.  
  88.     public function setLanguage()
  89.     {
  90.         $locale = 'en';
  91.         try {
  92.             if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  93.                 $locale = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
  94.             }
  95.         } catch (Exception $e) {
  96.         }
  97.         return $locale;
  98.     }
  99. }
  100.  
  101. class Request
  102. {
  103.     public function __construct()
  104.     {
  105.         $this->params = $_REQUEST;
  106.     }
  107.  
  108.     public function validate()
  109.     {
  110.         return is_array($this->params);
  111.     }
  112.  
  113.     public function getQueryString()
  114.     {
  115.         return $_SERVER['QUERY_STRING'];
  116.     }
  117.  
  118.     public function getRequestUri()
  119.     {
  120.         return $_SERVER['REQUEST_URI'];
  121.     }
  122.  
  123.     public function checkRequest()
  124.     {
  125.         $result = 'dynamic';
  126.         $string = $this->getRequestUri();
  127.         if (stristr($string, '/client/save') === FALSE
  128.             && stristr($string, '/client/validate') === FALSE
  129.             && stristr($string, '/set-locale?locale') === FALSE) {
  130.             $result = 'static';
  131.             $array = array_filter(explode('/', $string));
  132.             if (is_array($array) && count($array) > 0) {
  133.                 $element = array_shift($array);
  134.                 if (strpos($element, '?') !== 0) {
  135.                     $result = 'error';
  136.                 }
  137.             }
  138.         }
  139.         return $result;
  140.     }
  141.  
  142.     public function getPost()
  143.     {
  144.         return $_POST;
  145.     }
  146.  
  147.     public function isPost()
  148.     {
  149.         $result = false;
  150.         if (isset($_SERVER['REQUEST_METHOD']) && stristr($_SERVER['REQUEST_METHOD'], 'post') !== false) {
  151.             $result = true;
  152.         }
  153.         return $result;
  154.     }
  155.  
  156. }
  157.  
  158. class MobileDetect
  159. {
  160.     public static function deviceCheck()
  161.     {
  162.         $result = 'desktop';
  163.         if (isset($_SERVER['HTTP_USER_AGENT'])) {
  164.             $useragent = $_SERVER['HTTP_USER_AGENT'];
  165.             if (preg_match('/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i', $useragent) || preg_match('/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i', substr($useragent, 0, 4))) {
  166.                 $result = 'mobile';
  167.             }
  168.         }
  169.         return $result;
  170.     }
  171.  
  172. }
  173.  
  174. class ABtest
  175. {
  176.     public static function switcher(Cache $cache)
  177.     {
  178.         $key = Cache::buildKey('rotator_block');
  179.         $countViews = $cache->server->get($key);
  180.         if (is_numeric($countViews)) {
  181.             if ($countViews % 2 == 0) {
  182.                 $result = 1;
  183.             } else {
  184.                 $result = 2;
  185.             }
  186.             $cache->server->increment($key);
  187.         } else {
  188.             $cache->server->add($key, 1);
  189.             $result = 1;
  190.  
  191.         }
  192.         return json_decode($result);
  193.     }
  194. }
  195.  
  196. class Response
  197. {
  198.  
  199.     public static function setJsonHeaders()
  200.     {
  201.         header('Content-Type: application/json');
  202.     }
  203.  
  204.     public static function setNotFoundHeader()
  205.     {
  206.         header("HTTP/1.0 404 Not Found");
  207.     }
  208.  
  209.  
  210. }
  211.  
  212. class App
  213. {
  214.     public function start()
  215.     {
  216.         $cache = new Cache();
  217.         $cookies = new Cookie();
  218.         $request = new Request();
  219.         if (!$request->isPost()) {
  220.             $cookies->setLinksCookies();
  221.         }
  222.         if (isset($_SERVER['HTTP_HOST'])
  223.             && $_SERVER['HTTP_HOST'] == 'ico.bitcomo.com'
  224.         ) {
  225.             $locale = $cookies->getLocale();
  226.             if (!$locale) {
  227.                 $locale = $cookies->setLanguage();
  228.             }
  229.  
  230.             $device = MobileDetect::deviceCheck();
  231.             $contentType = $request->checkRequest();
  232.  
  233.             if ($contentType == 'static') {
  234.                 $key = Cache::buildKey("bitcomo_page_{$locale}_{$device}");
  235.                 $test = ABtest::switcher($cache);
  236.                 $page = $cache->server->get($key);
  237.                 if (!empty($page)) {
  238.                     echo $page;
  239.                     exit;
  240.                 }
  241.             } elseif ($contentType == 'error') {
  242.                 Response::setNotFoundHeader();
  243.                 $key = Cache::buildKey('bitcomo_page_error');
  244.                 $errorPage = $cache->server->get($key);
  245.                 if (!empty($errorPage)) {
  246.                     echo $errorPage;
  247.                     exit;
  248.                 }
  249.             }
  250.         }
  251.     }
  252. }
  253.  
  254. (new App())->start();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement