Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by PhpStorm.
- * User: alpas
- * Date: 23.01.18
- * Time: 10:34
- */
- class Cache
- {
- /** @var $server Memcached */
- public $server;
- public function __construct()
- {
- $mem = new Memcached();
- $mem->addServer('127.0.0.1', 11211);
- $this->server = $mem;
- }
- public static function buildKey($key)
- {
- return ctype_alnum($key) && Helper::byteLength($key) <= 32 ? $key : md5($key);
- }
- }
- class Helper
- {
- /**
- * Returns the number of bytes in the given string.
- * This method ensures the string is treated as a byte array by using `mb_strlen()`.
- * @param string $string the string being measured for length
- * @return int the number of bytes in the given string.
- */
- public static function byteLength($string)
- {
- return mb_strlen($string, '8bit');
- }
- }
- class Cookie
- {
- public function __construct()
- {
- $this->params = $_COOKIE;
- }
- public function getLocale()
- {
- $locale = false;
- if (isset($this->params['_locale'])) {
- $string = stristr($this->params['_locale'], 'a:');
- try {
- $locale = unserialize($string);
- $locale = $locale[1];
- } catch (Exception $e) {
- }
- }
- return $locale;
- }
- public function add($name, $value, $expires = null, $path = null, $domain = null, $secure = null)
- {
- setcookie($name, $value, $expires, $path, $domain, $secure);
- }
- public function setLinksCookies()
- {
- $request = new Request();
- if (array_key_exists('s_link', $this->params)) {
- $founded = false;
- if ($request->validate()) {
- foreach ($request->params as $key => $value) {
- if (stristr($key, "utm_") != false) {
- $founded = true;
- }
- }
- if ($founded || array_key_exists('s_trk', $request->params)) {
- $this->add('s_link', $request->getQueryString(), time() + 60 * 60 * 24 * 30 * 3, '/', '.bitcomo.com');
- }
- }
- } else {
- $this->add('s_link', $request->getQueryString(), time() + 60 * 60 * 24 * 30 * 3, '/', '.bitcomo.com');
- }
- }
- public function setLanguage()
- {
- $locale = 'en';
- try {
- if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
- $locale = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
- }
- } catch (Exception $e) {
- }
- return $locale;
- }
- }
- class Request
- {
- public function __construct()
- {
- $this->params = $_REQUEST;
- }
- public function validate()
- {
- return is_array($this->params);
- }
- public function getQueryString()
- {
- return $_SERVER['QUERY_STRING'];
- }
- public function getRequestUri()
- {
- return $_SERVER['REQUEST_URI'];
- }
- public function checkRequest()
- {
- $result = 'dynamic';
- $string = $this->getRequestUri();
- if (stristr($string, '/client/save') === FALSE
- && stristr($string, '/client/validate') === FALSE
- && stristr($string, '/set-locale?locale') === FALSE) {
- $result = 'static';
- $array = array_filter(explode('/', $string));
- if (is_array($array) && count($array) > 0) {
- $element = array_shift($array);
- if (strpos($element, '?') !== 0) {
- $result = 'error';
- }
- }
- }
- return $result;
- }
- public function getPost()
- {
- return $_POST;
- }
- public function isPost()
- {
- $result = false;
- if (isset($_SERVER['REQUEST_METHOD']) && stristr($_SERVER['REQUEST_METHOD'], 'post') !== false) {
- $result = true;
- }
- return $result;
- }
- }
- class MobileDetect
- {
- public static function deviceCheck()
- {
- $result = 'desktop';
- if (isset($_SERVER['HTTP_USER_AGENT'])) {
- $useragent = $_SERVER['HTTP_USER_AGENT'];
- 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))) {
- $result = 'mobile';
- }
- }
- return $result;
- }
- }
- class ABtest
- {
- public static function switcher(Cache $cache)
- {
- $key = Cache::buildKey('rotator_block');
- $countViews = $cache->server->get($key);
- if (is_numeric($countViews)) {
- if ($countViews % 2 == 0) {
- $result = 1;
- } else {
- $result = 2;
- }
- $cache->server->increment($key);
- } else {
- $cache->server->add($key, 1);
- $result = 1;
- }
- return json_decode($result);
- }
- }
- class Response
- {
- public static function setJsonHeaders()
- {
- header('Content-Type: application/json');
- }
- public static function setNotFoundHeader()
- {
- header("HTTP/1.0 404 Not Found");
- }
- }
- class App
- {
- public function start()
- {
- $cache = new Cache();
- $cookies = new Cookie();
- $request = new Request();
- if (!$request->isPost()) {
- $cookies->setLinksCookies();
- }
- if (isset($_SERVER['HTTP_HOST'])
- && $_SERVER['HTTP_HOST'] == 'ico.bitcomo.com'
- ) {
- $locale = $cookies->getLocale();
- if (!$locale) {
- $locale = $cookies->setLanguage();
- }
- $device = MobileDetect::deviceCheck();
- $contentType = $request->checkRequest();
- if ($contentType == 'static') {
- $key = Cache::buildKey("bitcomo_page_{$locale}_{$device}");
- $test = ABtest::switcher($cache);
- $page = $cache->server->get($key);
- if (!empty($page)) {
- echo $page;
- exit;
- }
- } elseif ($contentType == 'error') {
- Response::setNotFoundHeader();
- $key = Cache::buildKey('bitcomo_page_error');
- $errorPage = $cache->server->get($key);
- if (!empty($errorPage)) {
- echo $errorPage;
- exit;
- }
- }
- }
- }
- }
- (new App())->start();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement