123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540 |
- <?php
- class OmegaTvFrontend {
- /**
- * Contains available omegatv service tariffs id=>tariffdata
- *
- * @var array
- */
- protected $allTariffs = array();
- /**
- * Contains all tariff names as tariffid=>name
- *
- * @var array
- */
- protected $tariffNames = array();
- /**
- * Contains all of internet users data as login=>data
- *
- * @var array
- */
- protected $allUsers = array();
- /**
- * Contains available and active omegatv service subscriptions as customerid=>data
- *
- * @var array
- */
- protected $allSubscribers = array();
- /**
- * Contains system config as key=>value
- *
- * @var array
- */
- protected $usConfig = array();
- /**
- * Current instance user login
- *
- * @var string
- */
- protected $userLogin = '';
- /**
- * Contains Ubilling RemoteAPI URL
- *
- * @var string
- */
- protected $apiUrl = '';
- /**
- * Contains Ubilling RemoteAPI Key
- *
- * @var string
- */
- protected $apiKey = '';
- /**
- * Contains some configurable amount of maximum count of devices/playlists per user
- *
- * @var int
- */
- protected $maxDevices = 3;
- public function __construct() {
- $this->loadUsConfig();
- $this->setOptions();
- $this->loadTariffs();
- $this->loadUsers();
- $this->loadUserSubscriptions();
- }
- /**
- * Loads userstats config into protected usConfig variable
- *
- * @return void
- */
- protected function loadUsConfig() {
- $this->usConfig = zbs_LoadConfig();
- }
- /**
- * Sets required object options
- *
- * @return void
- */
- protected function setOptions() {
- $this->apiUrl = $this->usConfig['API_URL'];
- $this->apiKey = $this->usConfig['API_KEY'];
- if (isset($this->usConfig['OM_MAXDEV'])) {
- $this->maxDevices = $this->usConfig['OM_MAXDEV'];
- }
- }
- /**
- * Loads available users from database
- *
- * @return void
- */
- protected function loadUsers() {
- $query = "SELECT * from `users`";
- $all = simple_queryall($query);
- if (!empty($all)) {
- foreach ($all as $io => $each) {
- $this->allUsers[$each['login']] = $each;
- }
- }
- }
- /**
- * Loads existing users profiles/subscriptions
- *
- * @return void
- */
- protected function loadUserSubscriptions() {
- $query = "SELECT * from `om_users`";
- $all = simple_queryall($query);
- if (!empty($all)) {
- foreach ($all as $io => $each) {
- $this->allSubscribers[$each['customerid']] = $each;
- }
- }
- }
- /**
- * Sets current user login
- *
- * @return void
- */
- public function setLogin($login) {
- $this->userLogin = $login;
- }
- /**
- * Loads existing tariffs from database
- *
- * @return void
- */
- protected function loadTariffs() {
- $query = "SELECT * from `om_tariffs` WHERE `type`='base' OR `type`='bundle' ORDER BY `type` ASC";
- $all = simple_queryall($query);
- if (!empty($all)) {
- foreach ($all as $io => $each) {
- $this->allTariffs[$each['id']] = $each;
- $this->tariffNames[$each['tariffid']] = $each['tariffname'];
- }
- }
- }
- /**
- * Checks is user subscribed for some tariff or not?
- *
- * @param string $login
- * @param int $tariffid
- *
- * @return bool
- */
- protected function isUserSubscribed($login, $tariffid) {
- $result = false;
- if (!empty($this->allSubscribers)) {
- $tariffExternalId = $this->allTariffs[$tariffid]['tariffid'];
- foreach ($this->allSubscribers as $io => $each) {
- if (($each['login'] == $login)) {
- if ($each['basetariffid'] == $tariffExternalId) {
- $result = true;
- break;
- }
- if (!empty($each['bundletariffs'])) {
- $bundleTariffs = unserialize($each['bundletariffs']);
- if (isset($bundleTariffs[$tariffExternalId])) {
- $result = true;
- break;
- }
- }
- }
- }
- }
- return ($result);
- }
- /**
- * Returns user login transformed to some numeric hash
- *
- * @param string $login
- *
- * @return int
- */
- public function generateCustormerId($login) {
- $result = '';
- if (!empty($login)) {
- $result = crc32($login);
- }
- return($result);
- }
- /**
- * Renders available user devices list and some controls
- *
- * @return string
- */
- public function renderUserDevicesForm() {
- $result = '';
- if (!empty($this->userLogin)) {
- //available devices
- $currentDevices = $this->getDevicesData();
- $currentPlaylists = $this->getPlaylistsData();
- $devCount = 0;
- $rows = '';
- if (!empty($currentDevices) OR ( !empty($currentPlaylists))) {
- $currentDevices = json_decode($currentDevices, true);
- $currentPlaylists = json_decode($currentPlaylists, true);
- if (!empty($currentDevices)) {
- foreach ($currentDevices as $io => $each) {
- $cells = la_TableCell($each['uniq']);
- $cells .= la_TableCell(date("Y-m-d H:i:s", $each['activation_data']));
- $cells .= la_TableCell($each['model']);
- $deviceControls = la_JSAlert('?module=omegatv&deletedevice=' . $each['uniq'], __('Delete'), __('Are you sure') . '?');
- $cells .= la_TableCell($deviceControls);
- $rows .= la_TableRow($cells, 'row3');
- $devCount++;
- }
- }
- if (!empty($currentPlaylists)) {
- foreach ($currentPlaylists as $io => $each) {
- $cells = la_TableCell($each['uniq']);
- $actDate = ($each['activation_data']) ? date("Y-m-d H:i:s", $each['activation_data']) : '-';
- $cells .= la_TableCell($actDate);
- $playlistControls = la_Link($each['url'], __('Playlist'));
- $cells .= la_TableCell($playlistControls);
- $deviceControls = la_JSAlert('?module=omegatv&deleteplaylist=' . $each['uniq'], __('Delete'), __('Are you sure') . '?');
- $cells .= la_TableCell($deviceControls);
- $rows .= la_TableRow($cells, 'row3');
- $devCount++;
- }
- }
- $result .= la_TableBody($rows, '100%', 0, 'sortable');
- }
- //maximum devices limit
- if ($devCount < $this->maxDevices) {
- //new device activation
- if (la_CheckGet(array('getcode'))) {
- $actCode = $this->getDeviceActivationCode();
- $result .= la_tag('br');
- $result .= la_tag('h3', false) . __('Activation code') . ': ' . $actCode . la_tag('h3', true);
- } else {
- $result .= la_tag('br');
- $actCodeControl = la_Link('?module=omegatv&getcode=true', __('Get device activation code'));
- $newPlControl = la_Link('?module=omegatv&newplaylist=true', __('Add playlist'));
- $result .= $actCodeControl . ' / ' . $newPlControl;
- }
- } else {
- $result .= __('Devices count limit is exceeded');
- }
- }
- return ($result);
- }
- /**
- * Checks is user protected from his own stupidity?
- *
- * @param int $tariffId
- * @return bool
- */
- protected function checkUserProtection($tariffId) {
- $tariffId = vf($tariffId, 3);
- $result = true;
- if (isset($this->usConfig['OM_PROTECTION'])) {
- if ($this->usConfig['OM_PROTECTION']) {
- if (isset($this->allTariffs[$tariffId])) {
- $tariffFee = $this->allTariffs[$tariffId]['fee'];
- $tariffData = $this->allTariffs[$tariffId];
- $userData = $this->allUsers[$this->userLogin];
- $userBalance = $userData['Cash'];
- if ($userBalance < $tariffFee) {
- $result = false;
- }
- } else {
- $result = false;
- }
- }
- }
- return ($result);
- }
- /**
- * Renders tariffs list with subscribtion form
- *
- * @return string
- */
- public function renderSubscribeForm() {
- $result = '';
- $iconsPath = zbs_GetCurrentSkinPath($this->usConfig) . 'iconz/';
- if (!empty($this->allTariffs)) {
- foreach ($this->allTariffs as $io => $each) {
- $headerType = ($each['type'] == 'base') ? 'mgheaderprimary' : 'mgheader';
- $freeAppend = la_delimiter();
- $tariffFee = $each['fee'];
- $primaryLabel = ($each['type'] == 'base') ? la_img($iconsPath . 'ok_small.png') : la_img($iconsPath . 'unavail_small.png');
- $subscribedLabel = ($this->isUserSubscribed($this->userLogin, $each['id'])) ? la_img($iconsPath . 'ok_small.png') : la_img($iconsPath . 'unavail_small.png');
- $tariffInfo = la_tag('div', false, $headerType) . $each['tariffname'] . la_tag('div', true);
- $cells = la_TableCell(la_tag('b') . __('Fee') . la_tag('b', true));
- $cells .= la_TableCell($tariffFee . ' ' . $this->usConfig['currency']);
- $rows = la_TableRow($cells);
- $cells = la_TableCell(la_tag('b') . __('Base') . la_tag('b', true));
- $cells .= la_TableCell($primaryLabel);
- $rows .= la_TableRow($cells);
- $cells = la_TableCell(la_tag('b') . __('You subscribed') . la_tag('b', true));
- $cells .= la_TableCell($subscribedLabel);
- $rows .= la_TableRow($cells);
- $tariffInfo .= la_TableBody($rows, '100%', 0);
- $tariffInfo .= $freeAppend;
- if ($this->checkBalance()) {
- if ($this->isUserSubscribed($this->userLogin, $each['id'])) {
- $subscribeControl = la_Link('?module=omegatv&unsubscribe=' . $each['tariffid'], __('Unsubscribe'), false, 'mgunsubcontrol');
- } else {
- if ($this->checkUserProtection($each['id'])) {
- $alertText = __('I have thought well and understand that I activate this service for myself not by chance and completely meaningfully and I am aware of all the consequences.');
- $subscribeControl = la_ConfirmDialog('?module=omegatv&subscribe=' . $each['tariffid'], __('Subscribe'), $alertText, 'mgsubcontrol', '?module=omegatv');
- } else {
- $subscribeControl = __('The amount of money in your account is not sufficient to process subscription');
- }
- }
- $tariffInfo .= $subscribeControl;
- } else {
- $tariffInfo .= __('The amount of money in your account is not sufficient to process subscription');
- }
- $result .= la_tag('div', false, 'mgcontainer') . $tariffInfo . la_tag('div', true);
- }
- }
- return ($result);
- }
- /**
- * Check user balance for subscribtion availability
- *
- * @return bool
- */
- protected function checkBalance() {
- $result = false;
- if (!empty($this->userLogin)) {
- if (isset($this->allUsers[$this->userLogin])) {
- $userData = $this->allUsers[$this->userLogin];
- $userBalance = $this->allUsers[$this->userLogin]['Cash'];
- if ($userBalance >= 0) {
- $result = true;
- }
- }
- }
- return ($result);
- }
- /**
- * Returns local customer ID from database
- *
- * @param string $userLogin
- *
- * @return int
- */
- public function getLocalCustomerId() {
- $result = '';
- if (!empty($this->allSubscribers)) {
- foreach ($this->allSubscribers as $io => $each) {
- if ($each['login'] == $this->userLogin) {
- $result = $each['customerid'];
- break;
- }
- }
- }
- return ($result);
- }
- /**
- * Returns array of subscribed tariff Ids=>type
- *
- * @return array
- */
- public function getSubscribedTariffs() {
- $result = array();
- $customerId = $this->getLocalCustomerId();
- if (!empty($customerId)) {
- if (isset($this->allSubscribers[$customerId])) {
- $localCustomerData = $this->allSubscribers[$customerId];
- if (!empty($localCustomerData['basetariffid'])) {
- $result[$localCustomerData['basetariffid']] = 'base';
- }
- if (!empty($localCustomerData['bundletariffs'])) {
- $bundleTariffs = unserialize($localCustomerData['bundletariffs']);
- if (!empty($bundleTariffs)) {
- foreach ($bundleTariffs as $io => $each) {
- $result[$io] = 'bundle';
- }
- }
- }
- }
- }
- return ($result);
- }
- /**
- * Gets view URL via remote API
- *
- * @return string
- */
- public function getViewButtonURL() {
- $result = '';
- $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=omcontrol¶m=viewurl&userlogin=' . $this->userLogin;
- @$result = file_get_contents($action);
- return ($result);
- }
- /**
- * Gets device activation code via remote API
- *
- * @return string
- */
- public function getDeviceActivationCode() {
- $result = '';
- $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=omcontrol¶m=getcode&userlogin=' . $this->userLogin;
- @$result = file_get_contents($action);
- return ($result);
- }
- /**
- * Gets devices data via remote API
- *
- * @return string
- */
- public function getDevicesData() {
- $result = '';
- $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=omcontrol¶m=getdevices&userlogin=' . $this->userLogin;
- @$result = file_get_contents($action);
- return ($result);
- }
- /**
- * Gets playlists data via remote API
- *
- * @return string
- */
- public function getPlaylistsData() {
- $result = '';
- $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=omcontrol¶m=getplaylists&userlogin=' . $this->userLogin;
- @$result = file_get_contents($action);
- return ($result);
- }
- /**
- * Pushes device deletion request via remote API
- *
- * @param string $uniq
- *
- * @return string
- */
- public function pushDeviceDelete($uniq) {
- $result = '';
- $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=omcontrol¶m=deletedev&userlogin=' . $this->userLogin . '&uniq=' . $uniq;
- @$result = file_get_contents($action);
- return ($result);
- }
- /**
- * Pushes playlist deletion request via remote API
- *
- * @param string $uniq
- *
- * @return string
- */
- public function pushPlaylistDelete($uniq) {
- $result = '';
- $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=omcontrol¶m=deletepl&userlogin=' . $this->userLogin . '&uniq=' . $uniq;
- @$result = file_get_contents($action);
- return ($result);
- }
- /**
- * Pushes playlist assign request via remote API
- *
- * @param string $uniq
- *
- * @return string
- */
- public function pushPlaylistAssign() {
- $result = '';
- $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=omcontrol¶m=assignpl&userlogin=' . $this->userLogin;
- @$result = file_get_contents($action);
- return ($result);
- }
- /**
- * Pushes tariff subscription request via remote API
- *
- * @param int $tariffId
- *
- * @return string
- */
- public function pushSubscribeRequest($tariffId) {
- $result = '';
- $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=omcontrol¶m=subscribe&userlogin=' . $this->userLogin . '&tariffid=' . $tariffId;
- @$result = file_get_contents($action);
- return ($result);
- }
- /**
- * Pushes tariff unsubscription request via remote API
- *
- * @param int $tariffId
- *
- * @return string
- */
- public function pushUnsubscribeRequest($tariffId) {
- $result = '';
- $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=omcontrol¶m=unsubscribe&userlogin=' . $this->userLogin . '&tariffid=' . $tariffId;
- @$result = file_get_contents($action);
- return ($result);
- }
- }
- ?>
|