123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474 |
- <?php
- class MegogoFrontend {
- /**
- * Contains available megogo service tariffs id=>tariffdata
- *
- * @var array
- */
- protected $allTariffs = array();
- /**
- * Contains available and active megogo service subscriptions as id=>data
- *
- * @var array
- */
- protected $allSubscribers = array();
- /**
- * Contains all subscribtions history by all of users id=>data
- *
- * @var array
- */
- protected $allHistory = array();
- /**
- * Contains all of internet users data as login=>data
- *
- * @var array
- */
- protected $allUsers = 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 = '';
- /**
- * Web-auth data database abstraction layer placeholder
- *
- * @var object
- */
- protected $credentialsDb = '';
- public function __construct() {
- $this->loadUsConfig();
- $this->setOptions();
- $this->loadUsers();
- $this->loadTariffs();
- $this->loadSubscribers();
- $this->loadHistory();
- $this->initCredentials();
- }
- /**
- * Sets current user login
- *
- * @return void
- */
- public function setLogin($login) {
- $this->userLogin = $login;
- }
- /**
- * 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'];
- }
- /**
- * Loads existing tariffs from database for further usage
- *
- * @return void
- */
- protected function loadTariffs() {
- $query = "SELECT * from `mg_tariffs` ORDER BY `primary` DESC, `fee` ASC";
- $all = simple_queryall($query);
- if (!empty($all)) {
- foreach ($all as $io => $each) {
- $this->allTariffs[$each['id']] = $each;
- }
- }
- }
- /**
- * Loads existing subscribers data
- *
- * @return void
- */
- protected function loadSubscribers() {
- $query = "SELECT * from `mg_subscribers`";
- $all = simple_queryall($query);
- if (!empty($all)) {
- foreach ($all as $io => $each) {
- $this->allSubscribers[$each['id']] = $each;
- }
- }
- }
- /**
- * Performs init of credentials database abscration layer
- *
- * @return void
- */
- protected function initCredentials() {
- $this->credentialsDb = new NyanORM('mg_credentials');
- }
- /**
- * Loads existing subscribers data
- *
- * @return void
- */
- protected function loadHistory() {
- $query = "SELECT * from `mg_history`";
- $all = simple_queryall($query);
- if (!empty($all)) {
- foreach ($all as $io => $each) {
- $this->allHistory[$each['id']] = $each;
- }
- }
- }
- /**
- * 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;
- }
- }
- }
- /**
- * 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)) {
- foreach ($this->allSubscribers as $io => $each) {
- if (($each['login'] == $login) AND ( $each['tariffid'] == $tariffid)) {
- $result = true;
- break;
- }
- }
- }
- return ($result);
- }
- /**
- * Renders tariffs list with subscribtion form
- *
- * @return string
- */
- public function renderSubscribeForm() {
- $result = '';
- $iconsPath = zbs_GetCurrentSkinPath($this->usConfig) . 'iconz/';
- $result .= la_tag('b') . __('Attention!') . la_tag('b', true) . ' ';
- $result .= __('When activated subscription account will be charged fee the equivalent value of the subscription.') . la_delimiter();
- if (!empty($this->allTariffs)) {
- foreach ($this->allTariffs as $io => $each) {
- $headerType = ($each['primary']) ? 'mgheaderprimary' : 'mgheader';
- $freePeriodLabel = ($each['freeperiod']) ? la_img($iconsPath . 'ok_small.png', __('Available')) : la_img($iconsPath . 'unavail_small.png', __('Unavailable'));
- $freeAppend = la_delimiter();
- $tariffFee = $each['fee'];
- if ($each['freeperiod']) {
- if (!$this->checkFreePeriodAvail($this->userLogin)) {
- $freePeriodLabel = la_img($iconsPath . 'unavail_small.png', __('Unavailable'));
- $freeAppend = la_delimiter();
- } else {
- $freeAppend = la_tag('center') . la_tag('strong') . __('Try it now for free!') . la_tag('strong', true) . la_tag('center', true) . la_tag('br');
- $tariffFee = 0;
- }
- }
- $primaryLabel = ($each['primary']) ? la_img($iconsPath . 'ok_small.png') : la_img($iconsPath . 'unavail_small.png');
- $tariffInfo = la_tag('div', false, $headerType) . $each['name'] . 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') . __('Free period') . la_tag('b', true));
- $cells .= la_TableCell($freePeriodLabel);
- $rows .= la_TableRow($cells);
- $cells = la_TableCell(la_tag('b') . __('Primary') . la_tag('b', true));
- $cells .= la_TableCell($primaryLabel);
- $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=megogo&unsubscribe=' . $each['id'], __('Unsubscribe'), false, 'mgunsubcontrol');
- } else {
- $userProtection = $this->checkUserProtection($each['id']);
- if ($userProtection) {
- $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=megogo&subscribe=' . $each['id'], __('Subscribe'), $alertText, 'mgsubcontrol', '?module=megogo');
- } else {
- //money issues
- if ($userProtection === false) {
- $subscribeControl = __('The amount of money in your account is not sufficient to process subscription');
- }
- //tariff isnt allowed
- if ($userProtection === 0) {
- $subscribeControl = __('Your tariff does not provide this service');
- }
- }
- }
- $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);
- }
- /**
- * Runs default subscribtion routine
- *
- * @return void/string on error
- */
- public function pushSubscribeRequest($tariffid) {
- $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=mgcontrol¶m=subscribe&userlogin=' . $this->userLogin . '&tariffid=' . $tariffid;
- @$result = file_get_contents($action);
- return ($result);
- }
- /**
- * Runs default unsubscribtion routine
- *
- * @return void/string on error
- */
- public function pushUnsubscribeRequest($tariffid) {
- $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=mgcontrol¶m=unsubscribe&userlogin=' . $this->userLogin . '&tariffid=' . $tariffid;
- @$result = file_get_contents($action);
- return ($result);
- }
- /**
- * Checks have current user any subscribtions?
- *
- * @return bool
- */
- public function haveSubscribtions() {
- $result = false;
- if (!empty($this->allSubscribers)) {
- foreach ($this->allSubscribers as $io => $each) {
- if ($each['login'] == $this->userLogin) {
- $result = true;
- break;
- }
- }
- }
- return ($result);
- }
- /**
- * Gets auth URL via remote API
- *
- * @return string
- */
- public function getAuthButtonURL() {
- $result = '';
- $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=mgcontrol¶m=auth&userlogin=' . $this->userLogin;
- $result = file_get_contents($action);
- 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);
- }
- /**
- * Checks free period availability for user
- *
- * @param string $login
- *
- * @return bool
- */
- protected function checkFreePeriodAvail($login) {
- $result = true;
- if (!empty($this->allHistory)) {
- foreach ($this->allHistory as $io => $each) {
- if (($each['login'] == $login) AND ( $each['freeperiod'] == 1)) {
- $result = false;
- break;
- }
- }
- }
- return ($result);
- }
- /**
- * Checks is user protected from his own stupidity?
- *
- * @param int $tariffId
- *
- * @return bool/int true - everything is ok, false - lack of money / 0 - tariff issues
- */
- protected function checkUserProtection($tariffId) {
- $tariffId = vf($tariffId, 3);
- $result = true;
- if (isset($this->usConfig['MG_PROTECTION'])) {
- if ($this->usConfig['MG_PROTECTION']) {
- if (isset($this->allTariffs[$tariffId])) {
- $tariffFee = $this->allTariffs[$tariffId]['fee'];
- $tariffData = $this->allTariffs[$tariffId];
- $userData = $this->allUsers[$this->userLogin];
- $userBalance = $userData['Cash'];
- if ($tariffData['freeperiod']) {
- if ($this->checkFreePeriodAvail($this->userLogin)) {
- $result = true;
- } else {
- if ($userBalance < $tariffFee) {
- $result = false;
- }
- }
- } else {
- if ($userBalance < $tariffFee) {
- $result = false;
- }
- }
- } else {
- $result = false;
- }
- }
- }
- //per-tariff protection controls
- if (isset($this->usConfig['MG_TARIFFSALLOWED'])) {
- if (!empty($this->usConfig['MG_TARIFFSALLOWED'])) {
- $tariffsAllowed = explode(',', $this->usConfig['MG_TARIFFSALLOWED']);
- $tariffsAllowed = array_flip($tariffsAllowed);
- $userTariff = $this->allUsers[$this->userLogin]['Tariff'];
- if (!isset($tariffsAllowed[$userTariff])) {
- $result = 0;
- }
- }
- }
- return ($result);
- }
- /**
- * Renders list of available subscribtions
- *
- * @return string
- */
- public function renderSubscribtions() {
- $result = '';
- $iconsPath = zbs_GetCurrentSkinPath($this->usConfig) . 'iconz/';
- if (!empty($this->allSubscribers)) {
- $cells = la_TableCell(__('Date'));
- $cells .= la_TableCell(__('Tariff'));
- $cells .= la_TableCell(__('Active'));
- $cells .= la_TableCell(__('Primary'));
- $cells .= la_TableCell(__('Free period'));
- $rows = la_TableRow($cells, 'row1');
- foreach ($this->allSubscribers as $io => $each) {
- if ($each['login'] == $this->userLogin) {
- $freePeriodFlag = ($each['freeperiod']) ? la_img($iconsPath . 'anread.gif') : la_img($iconsPath . 'anunread.gif');
- $primaryFlag = ($each['primary']) ? la_img($iconsPath . 'anread.gif') : la_img($iconsPath . 'anunread.gif');
- $activeFlag = ($each['active']) ? la_img($iconsPath . 'anread.gif') : la_img($iconsPath . 'anunread.gif');
- $cells = la_TableCell($each['actdate']);
- $cells .= la_TableCell(@$this->allTariffs[$each['tariffid']]['name']);
- $cells .= la_TableCell($activeFlag);
- $cells .= la_TableCell($primaryFlag);
- $cells .= la_TableCell($freePeriodFlag);
- $rows .= la_TableRow($cells, 'row2');
- }
- }
- $result = la_TableBody($rows, '100%', 0, 'resp-table');
- $result .= la_tag('br');
- //$result .= __('To view the purchased subscription register or log in to Megogo.net, by clicking the button below');
- }
- return ($result);
- }
- /**
- * Renders user credentials for web-auth on megogo service
- *
- * @return string/void
- */
- public function renderCredentials() {
- $result = '';
- $this->credentialsDb->where('login', '=', $this->userLogin);
- $userCredentials = $this->credentialsDb->getAll();
- if (!empty($userCredentials)) {
- $userCredentials = $userCredentials[0];
- $containerStyle = 'style="border:1px solid; text-align:center; width:100%; display:block;"';
- $result .= la_tag('span', false, 'resp-table', $containerStyle);
- $result .= __('Your login and password to usage with MEGOGO are') . ' ' . la_delimiter(1);
- $result .= __('Login') . ': ' . la_tag('b') . $userCredentials['email'] . la_tag('b', true) . la_tag('br');
- $result .= __('Password') . ': ' . la_tag('b') . $userCredentials['password'] . la_tag('b', true) . la_delimiter(1);
- $result .= __('To start using the MEGOGO service, click the button') . ' ' . la_Link('http://megogo.net/login', 'Continue', false, 'anreadbutton', 'target=_blank');
- $result .= la_delimiter(1);
- $result .= la_tag('span', true);
- }
- return($result);
- }
- }
- ?>
|