Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- *
- * @ This file is created by http://DeZender.Net
- * @ deZender (PHP5 Decoder for ionCube Encoder)
- *
- * @ Version : 3.0.8.0
- * @ Author : DeZender
- * @ Release on : 25.09.2017
- * @ Official site : http://DeZender.Net
- *
- */
- namespace Stalker\Lib\Core;
- class Stb
- {
- public $id = 0;
- public $mac = '';
- public $ip = null;
- public $hd = 0;
- private $user_agent = '';
- private $access_token = null;
- private $is_moderator = null;
- private $params = array( );
- private $db = null;
- public $lang = null;
- private $locale = null;
- private $country_id = null;
- private $openweathermap_country_id = null;
- public $city_id = null;
- public $openweathermap_city_id = null;
- public $timezone = null;
- public static $server_timezone = null;
- public $timezone_diff = 0;
- private $stb_lang = null;
- public $additional_services_on = 0;
- private static $just_created = false;
- private $valid_hash_version = false;
- private static $allowed_languages = null;
- private static $clock_formats = array(
- '12h',
- '24h'
- );
- private static $instance = null;
- /**
- * @static
- * @return Stb
- */
- public static function getInstance()
- {
- if (self::$instance == NULL) {
- self::$instance = new Stb( );
- }
- return self::$instance;
- }
- private function __construct()
- {
- $debug_key = $this->getDebugKey( );
- $this->user_agent = ((empty( $_SERVER['HTTP_USER_AGENT'] ) ? '' : $_SERVER['HTTP_USER_AGENT']));
- if (!(empty( $_SERVER['HTTP_X_USER_AGENT'] ))) {
- $this->user_agent .= '; ' . $_SERVER['HTTP_X_USER_AGENT'];
- }
- $this->parseAuthorizationHeader( );
- if (!(empty( $debug_key )) && $this->checkDebugKey( $debug_key )) {
- if (!(empty( $_REQUEST['mac'] ))) {
- $this->mac = @htmlspecialchars( trim( urldecode( $_REQUEST['mac'] ) ) );
- }
- else if (!(empty( $_COOKIE['mac'] ))) {
- $this->mac = @htmlspecialchars( trim( urldecode( $_COOKIE['mac'] ) ) );
- }
- else {
- echo 'Identification failed';
- exit( );
- }
- if (!(empty( $_COOKIE['debug'] )) || !(empty( $_REQUEST['debug'] ))) {
- Mysql::$debug = true;
- }
- }
- else if (!(empty( $_COOKIE['mac'] )) && empty( $_COOKIE['mac_emu'] )) {
- $this->mac = @htmlspecialchars( trim( urldecode( $_COOKIE['mac'] ) ) );
- if (!(empty( $_REQUEST['action'] )) && ($_REQUEST['action'] != 'handshake') && ($_REQUEST['action'] != 'get_profile') && ($_REQUEST['action'] != 'get_localization') && ($_REQUEST['action'] != 'do_auth') && !($this->isValidAccessToken( $this->access_token ))) {
- error_log( 'STB authorization failed. MAC: ' . $this->mac . ', token: ' . $this->access_token );
- echo 'Authorization failed.';
- exit( );
- }
- }
- else {
- if ((!(empty( $_SERVER['TARGET'] )) && (($_SERVER['TARGET'] == 'API') || ($_SERVER['TARGET'] == 'ADM'))) || (!(empty( $_REQUEST['type'] )) && ($_REQUEST['type'] == 'stb'))) {
- }
- else {
- $this->mac = '';
- echo 'Unauthorized request.';
- exit( );
- }
- }
- $this->mac = strtoupper( $this->mac );
- if (!(empty( $_COOKIE['stb_lang'] ))) {
- $this->stb_lang = @trim( urldecode( $_COOKIE['stb_lang'] ) );
- }
- if (!(empty( $_COOKIE['timezone'] )) && ($_COOKIE['timezone'] != 'undefined')) {
- $this->timezone = @trim( $_COOKIE['timezone'] );
- }
- if (@$_SERVER['HTTP_X_REAL_IP']) {
- $this->ip = @$_SERVER['HTTP_X_REAL_IP'];
- }
- else {
- $this->ip = @$_SERVER['REMOTE_ADDR'];
- }
- $this->db = Mysql::getInstance( );
- try {
- $this->getStbParams( );
- }
- catch (MysqlException $e) {
- echo $e->getMessage( ) . PHP_EOL;
- return;
- }
- if (!(empty( $_COOKIE['mac'] )) && (empty( $this->id ) || ($this->params['status'] == 1) || ($this->params['blocked'] == 1)) && !(empty( $_REQUEST['action'] )) && ($_REQUEST['action'] != 'handshake') && ($_REQUEST['action'] != 'get_profile') && ($_REQUEST['action'] != 'get_localization') && ($_REQUEST['action'] != 'do_auth') && ($_REQUEST['action'] != 'get_events') && ($_REQUEST['action'] != 'get_modules') && ($_REQUEST['action'] != 'get_main_info') && ($_REQUEST['action'] != 'get_payment_info') && ($_REQUEST['action'] != 'get_agreement_info') && ($_REQUEST['action'] != 'get_terms_info')) {
- error_log( 'Access denied to ' . $_REQUEST['type'] . ':' . $_REQUEST['action'] . ' for MAC: ' . $this->mac );
- echo 'Access denied.';
- exit( );
- }
- if (empty( $this->id )) {
- $this->initLocale( $this->stb_lang );
- if (!(empty( $_COOKIE['mac'] )) && !(empty( $_REQUEST['action'] )) && ($_REQUEST['action'] != 'handshake') && ($_REQUEST['action'] != 'get_profile') && ($_REQUEST['action'] != 'get_localization') && ($_REQUEST['action'] != 'do_auth') && ($_REQUEST['action'] != 'get_events') && ($_REQUEST['action'] != 'get_main_info') && ($_REQUEST['action'] != 'get_payment_info') && ($_REQUEST['action'] != 'get_agreement_info') && ($_REQUEST['action'] != 'get_terms_info')) {
- error_log( 'STB not found in the database, authorization failed. MAC: ' . $this->mac . ', token: ' . $this->access_token );
- echo 'Authorization failed.';
- exit( );
- }
- }
- }
- private function checkDebugKey($key)
- {
- return (bool) Mysql::getInstance( )->from( 'administrators' )->where( array(
- 'debug_key' => $key,
- 'login' => 'admin'
- ) )->get( )->first( );
- }
- private function getDebugKey()
- {
- if (!(empty( $_REQUEST['debug_key'] ))) {
- return $_REQUEST['debug_key'];
- }
- if (!(empty( $_COOKIE['debug_key'] ))) {
- return $_COOKIE['debug_key'];
- }
- return null;
- }
- public function setId($id)
- {
- $this->id = (int) $id;
- $this->params['id'] = (int) $id;
- }
- public function getTimezone()
- {
- return $this->timezone;
- }
- public function getParam($name)
- {
- return $this->params[$name];
- }
- public function getUserAgent()
- {
- return $this->user_agent;
- }
- public function getStbLanguage()
- {
- return $this->stb_lang;
- }
- public function setParam($key, $value)
- {
- if (!(array_key_exists( $key, $this->params ))) {
- return false;
- }
- if ($this->params[$key] == $value) {
- return true;
- }
- $this->params[$key] = $value;
- if (property_exists( $this, $key )) {
- $this->$key = $value;
- }
- return Mysql::getInstance( )->update( 'users', array( $key => $value ), array( 'id' => $this->id ) );
- }
- public function getStbParams()
- {
- if (!(empty( $this->mac ))) {
- $user = $this->db->from( 'users' )->where( array( 'mac' => $this->mac ) )->get( )->first( );
- }
- else {
- if (User::isInitialized( ) && User::getInstance( )->getId( )) {
- $user = $this->db->from( 'users' )->where( array( 'id' => (int) User::getInstance( )->getId( ) ) )->get( )->first( );
- }
- }
- if (!(empty( $user ))) {
- $this->params = $user;
- $this->id = $user['id'];
- $this->hd = $user['hd'];
- if (($this->params['hw_version_2'] && (($this->params['hw_version_2'] % 2) == 0)) || ($this->params['hw_version_2'] == '')) {
- $this->valid_hash_version = true;
- }
- $this->locale = ((empty( $user['locale'] ) && Config::exist( 'default_locale' ) ? Config::get( 'default_locale' ) : $user['locale']));
- if ((Config::getSafe( 'default_city_id', 0 ) == 0) && ($user['city_id'] == 0)) {
- $this->city_id = 0;
- }
- else {
- $this->city_id = ((empty( $user['city_id'] ) && Config::exist( 'default_city_id' ) ? Config::get( 'default_city_id' ) : intval( $user['city_id'] )));
- }
- if ((Config::getSafe( 'default_openweathermap_city_id', 0 ) == 0) && ($user['openweathermap_city_id'] == 0)) {
- $this->openweathermap_city_id = 0;
- }
- else {
- $this->openweathermap_city_id = ((empty( $user['openweathermap_city_id'] ) && Config::exist( 'default_openweathermap_city_id' ) ? Config::get( 'default_openweathermap_city_id' ) : intval( $user['openweathermap_city_id'] )));
- }
- $this->timezone = ((empty( $this->timezone ) && Config::exist( 'default_timezone' ) ? Config::get( 'default_timezone' ) : $this->timezone));
- self::$server_timezone = date_default_timezone_get( );
- date_default_timezone_set( $this->timezone );
- $date_server = new DateTime( );
- $date_server->setTimezone( new DateTimeZone( Stb::$server_timezone ) );
- $date_stb = new DateTime( );
- $date_stb->setTimezone( new DateTimeZone( $this->timezone ) );
- $this->timezone_diff = $date_server->format( 'Z' ) - $date_stb->format( 'Z' );
- $date = new DateTime( );
- $offset = $date->format( 'e' );
- if (!(Mysql::getInstance( )->set_timezone( $offset ))) {
- $offset = $date->format( 'P' );
- Mysql::getInstance( )->set_timezone( $offset );
- }
- $this->additional_services_on = $user['additional_services_on'];
- if (!(empty( $user['country'] ))) {
- $this->user_agent .= '; Country: ' . $user['country'];
- }
- if (!(empty( $user['client_type'] ))) {
- $this->user_agent .= '; ClientType: ' . $user['client_type'];
- }
- $this->initLocale( $this->stb_lang );
- }
- }
- public function initLocale($lang)
- {
- $stb_lang = $lang;
- if (!(empty( $lang )) && (2 <= strlen( $lang ))) {
- $preferred_locales = array_filter( Config::get( 'allowed_locales' ), function($e) use($stb_lang) {
- return strpos( $e, $stb_lang ) === 0;
- } );
- if (!(empty( $preferred_locales ))) {
- $preferred_locales = array_values( $preferred_locales );
- $this->locale = $preferred_locales[0];
- }
- }
- $this->stb_lang = substr( $this->locale, 0, 2 );
- setlocale( LC_MESSAGES, $this->locale );
- putenv( 'LC_MESSAGES=' . $this->locale );
- if (!(function_exists( 'bindtextdomain' ))) {
- throw new ErrorException( 'php-gettext extension not installed.' );
- }
- if (!(function_exists( 'locale_accept_from_http' ))) {
- throw new ErrorException( 'php-intl extension not installed.' );
- }
- bindtextdomain( 'stb', PROJECT_PATH . '/locale' );
- textdomain( 'stb' );
- bind_textdomain_codeset( 'stb', 'UTF-8' );
- }
- public function getStorages()
- {
- $master = new VideoMaster( );
- return $master->getStoragesForStb( );
- }
- private function isValidAccessToken($access_token, $mac = NULL)
- {
- if (empty( $access_token )) {
- return false;
- }
- if (!($mac)) {
- $mac = $this->mac;
- }
- $user = Mysql::getInstance( )->from( 'users' )->where( array( 'mac' => $mac ) )->get( )->first( );
- if (empty( $user )) {
- return false;
- }
- return $user['access_token'] == $access_token;
- }
- private function parseAuthorizationHeader()
- {
- if (function_exists( 'getallheaders' )) {
- $headers = getallheaders( );
- }
- else {
- $headers = $this->getHttpHeaders( );
- }
- if (!($headers)) {
- return;
- }
- $auth_header = ((!(empty( $headers['Authorization'] )) ? $headers['Authorization'] : null));
- if ($auth_header && preg_match( '/Bearer\\s+(.*)$/i', $auth_header, $matches )) {
- $this->access_token = trim( $matches[1] );
- }
- }
- private function getHttpHeaders()
- {
- $headers = array( );
- foreach ($_SERVER as $name => $value) {
- if (substr( $name, 0, 5 ) == 'HTTP_') {
- $headers[str_replace( ' ', '-', ucwords( strtolower( str_replace( '_', ' ', substr( $name, 5 ) ) ) ) )] = $value;
- }
- }
- return $headers;
- }
- public function handshake()
- {
- $debug_key = $this->getDebugKey( );
- $random = self::getCheck( );
- if (!(empty( $debug_key )) && $this->checkDebugKey( $debug_key )) {
- return array(
- 'token' => $this->getParam( 'access_token' ),
- 'random' => $random
- );
- }
- if (!(empty( $_REQUEST['token'] )) && ($_REQUEST['token'] == $this->getParam( 'access_token' ))) {
- return array(
- 'token' => $this->getParam( 'access_token' ),
- 'random' => $random
- );
- }
- $token = strtoupper( md5( microtime( 1 ) . uniqid( ) ) );
- return array(
- 'token' => $token,
- 'random' => $random,
- 'not_valid' => (Config::exist( 'auth_url' ) && !(empty( $_REQUEST['token'] )) && ($_REQUEST['token'] != $this->getParam( 'access_token' )) ? 1 : 0)
- );
- }
- private function passAccessFilter($country, &$model, $mac, $serial_number, $version, $device_id, $signature, &$force_auth)
- {
- $filter_file = PROJECT_PATH . '/access_filter.php';
- $rnd = $this->access_token;
- if (is_readable( $filter_file )) {
- return require_once $filter_file;
- }
- return true;
- }
- public function getProfile($apiv3 = false, $disable_device_id_validation = false)
- {
- $debug_key = $this->getDebugKey( );
- if (Config::getSafe( 'disable_portal', false ) && (empty( $debug_key ) || !($this->checkDebugKey( $debug_key )))) {
- try {
- Mysql::getInstance( )->update( 'users', array( 'access_token' => $this->access_token ), array( 'id' => $this->id ) );
- if (Config::getSafe( 'bind_stb_auth_and_oauth', true )) {
- $this->resetOauthToken( );
- /* 可能存在bug 一般代表DEFAULT的结束或者ELSE语句封闭,需要将此后的代码,移到switch 结束括号外面 */
- goto label47;
- echo $e->getMessage( ) . PHP_EOL;
- }
- }
- catch (MysqlException $e) {
- echo $e->getMessage( ) . PHP_EOL;
- }
- label47:
- return array(
- 'status' => 1,
- 'msg' => 'Portal is disabled',
- 'block_msg' => _( 'The portal is temporarily unavailable.<br>Please try again later.<br>Sorry for the inconvenience.' ),
- 'portal_disabled' => true
- );
- }
- if (function_exists( 'geoip_country_code_by_name' )) {
- $country = @geoip_country_code_by_name( $this->ip );
- if (!($country)) {
- error_log( 'geoip_country_code_by_name(): Host ' . $this->ip . ' not found' );
- }
- }
- else {
- $country = '';
- }
- $model = ((isset( $_REQUEST['stb_type'] ) ? $_REQUEST['stb_type'] : ''));
- $serial_number = ((isset( $_REQUEST['sn'] ) ? $_REQUEST['sn'] : ''));
- $version = ((isset( $_REQUEST['ver'] ) ? $_REQUEST['ver'] : ''));
- $device_id = ((isset( $_REQUEST['device_id'] ) ? $_REQUEST['device_id'] : ''));
- $device_id2 = ((isset( $_REQUEST['device_id2'] ) ? $_REQUEST['device_id2'] : ''));
- $signature = ((isset( $_REQUEST['signature'] ) ? $_REQUEST['signature'] : ''));
- $client_type = ((isset( $_REQUEST['client_type'] ) ? $_REQUEST['client_type'] : ''));
- $hw_version_2 = ((isset( $_REQUEST['hw_version_2'] ) ? $_REQUEST['hw_version_2'] : ''));
- $metrics = ((isset( $_REQUEST['metrics'] ) ? $_REQUEST['metrics'] : ''));
- $force_auth = null;
- $filter_response = $this->passAccessFilter( $country, $model, $this->mac, $serial_number, $version, $device_id2, $signature, $force_auth );
- $this->params['stb_type'] = $model;
- if (is_array( $filter_response )) {
- $filter_result = $filter_response['result'];
- .....................................................................
- .......................................
- .............
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement