api.megogo.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. <?php
  2. class MegogoFrontend {
  3. /**
  4. * Contains available megogo service tariffs id=>tariffdata
  5. *
  6. * @var array
  7. */
  8. protected $allTariffs = array();
  9. /**
  10. * Contains available and active megogo service subscriptions as id=>data
  11. *
  12. * @var array
  13. */
  14. protected $allSubscribers = array();
  15. /**
  16. * Contains all subscribtions history by all of users id=>data
  17. *
  18. * @var array
  19. */
  20. protected $allHistory = array();
  21. /**
  22. * Contains all of internet users data as login=>data
  23. *
  24. * @var array
  25. */
  26. protected $allUsers = array();
  27. /**
  28. * Contains system config as key=>value
  29. *
  30. * @var array
  31. */
  32. protected $usConfig = array();
  33. /**
  34. * Current instance user login
  35. *
  36. * @var string
  37. */
  38. protected $userLogin = '';
  39. /**
  40. * Contains Ubilling RemoteAPI URL
  41. *
  42. * @var string
  43. */
  44. protected $apiUrl = '';
  45. /**
  46. * Contains Ubilling RemoteAPI Key
  47. *
  48. * @var string
  49. */
  50. protected $apiKey = '';
  51. /**
  52. * Web-auth data database abstraction layer placeholder
  53. *
  54. * @var object
  55. */
  56. protected $credentialsDb = '';
  57. public function __construct() {
  58. $this->loadUsConfig();
  59. $this->setOptions();
  60. $this->loadUsers();
  61. $this->loadTariffs();
  62. $this->loadSubscribers();
  63. $this->loadHistory();
  64. $this->initCredentials();
  65. }
  66. /**
  67. * Sets current user login
  68. *
  69. * @return void
  70. */
  71. public function setLogin($login) {
  72. $this->userLogin = $login;
  73. }
  74. /**
  75. * Loads userstats config into protected usConfig variable
  76. *
  77. * @return void
  78. */
  79. protected function loadUsConfig() {
  80. $this->usConfig = zbs_LoadConfig();
  81. }
  82. /**
  83. * Sets required object options
  84. *
  85. * @return void
  86. */
  87. protected function setOptions() {
  88. $this->apiUrl = $this->usConfig['API_URL'];
  89. $this->apiKey = $this->usConfig['API_KEY'];
  90. }
  91. /**
  92. * Loads existing tariffs from database for further usage
  93. *
  94. * @return void
  95. */
  96. protected function loadTariffs() {
  97. $query = "SELECT * from `mg_tariffs` ORDER BY `primary` DESC, `fee` ASC";
  98. $all = simple_queryall($query);
  99. if (!empty($all)) {
  100. foreach ($all as $io => $each) {
  101. $this->allTariffs[$each['id']] = $each;
  102. }
  103. }
  104. }
  105. /**
  106. * Loads existing subscribers data
  107. *
  108. * @return void
  109. */
  110. protected function loadSubscribers() {
  111. $query = "SELECT * from `mg_subscribers`";
  112. $all = simple_queryall($query);
  113. if (!empty($all)) {
  114. foreach ($all as $io => $each) {
  115. $this->allSubscribers[$each['id']] = $each;
  116. }
  117. }
  118. }
  119. /**
  120. * Performs init of credentials database abscration layer
  121. *
  122. * @return void
  123. */
  124. protected function initCredentials() {
  125. $this->credentialsDb = new NyanORM('mg_credentials');
  126. }
  127. /**
  128. * Loads existing subscribers data
  129. *
  130. * @return void
  131. */
  132. protected function loadHistory() {
  133. $query = "SELECT * from `mg_history`";
  134. $all = simple_queryall($query);
  135. if (!empty($all)) {
  136. foreach ($all as $io => $each) {
  137. $this->allHistory[$each['id']] = $each;
  138. }
  139. }
  140. }
  141. /**
  142. * Loads available users from database
  143. *
  144. * @return void
  145. */
  146. protected function loadUsers() {
  147. $query = "SELECT * from `users`";
  148. $all = simple_queryall($query);
  149. if (!empty($all)) {
  150. foreach ($all as $io => $each) {
  151. $this->allUsers[$each['login']] = $each;
  152. }
  153. }
  154. }
  155. /**
  156. * Checks is user subscribed for some tariff or not?
  157. *
  158. * @param string $login
  159. * @param int $tariffid
  160. *
  161. * @return bool
  162. */
  163. protected function isUserSubscribed($login, $tariffid) {
  164. $result = false;
  165. if (!empty($this->allSubscribers)) {
  166. foreach ($this->allSubscribers as $io => $each) {
  167. if (($each['login'] == $login) AND ( $each['tariffid'] == $tariffid)) {
  168. $result = true;
  169. break;
  170. }
  171. }
  172. }
  173. return ($result);
  174. }
  175. /**
  176. * Renders tariffs list with subscribtion form
  177. *
  178. * @return string
  179. */
  180. public function renderSubscribeForm() {
  181. $result = '';
  182. $iconsPath = zbs_GetCurrentSkinPath($this->usConfig) . 'iconz/';
  183. $result .= la_tag('b') . __('Attention!') . la_tag('b', true) . ' ';
  184. $result .= __('When activated subscription account will be charged fee the equivalent value of the subscription.') . la_delimiter();
  185. if (!empty($this->allTariffs)) {
  186. foreach ($this->allTariffs as $io => $each) {
  187. $headerType = ($each['primary']) ? 'mgheaderprimary' : 'mgheader';
  188. $freePeriodLabel = ($each['freeperiod']) ? la_img($iconsPath . 'ok_small.png', __('Available')) : la_img($iconsPath . 'unavail_small.png', __('Unavailable'));
  189. $freeAppend = la_delimiter();
  190. $tariffFee = $each['fee'];
  191. if ($each['freeperiod']) {
  192. if (!$this->checkFreePeriodAvail($this->userLogin)) {
  193. $freePeriodLabel = la_img($iconsPath . 'unavail_small.png', __('Unavailable'));
  194. $freeAppend = la_delimiter();
  195. } else {
  196. $freeAppend = la_tag('center') . la_tag('strong') . __('Try it now for free!') . la_tag('strong', true) . la_tag('center', true) . la_tag('br');
  197. $tariffFee = 0;
  198. }
  199. }
  200. $primaryLabel = ($each['primary']) ? la_img($iconsPath . 'ok_small.png') : la_img($iconsPath . 'unavail_small.png');
  201. $tariffInfo = la_tag('div', false, $headerType) . $each['name'] . la_tag('div', true);
  202. $cells = la_TableCell(la_tag('b') . __('Fee') . la_tag('b', true));
  203. $cells .= la_TableCell($tariffFee . ' ' . $this->usConfig['currency']);
  204. $rows = la_TableRow($cells);
  205. $cells = la_TableCell(la_tag('b') . __('Free period') . la_tag('b', true));
  206. $cells .= la_TableCell($freePeriodLabel);
  207. $rows .= la_TableRow($cells);
  208. $cells = la_TableCell(la_tag('b') . __('Primary') . la_tag('b', true));
  209. $cells .= la_TableCell($primaryLabel);
  210. $rows .= la_TableRow($cells);
  211. $tariffInfo .= la_TableBody($rows, '100%', 0);
  212. $tariffInfo .= $freeAppend;
  213. if ($this->checkBalance()) {
  214. if ($this->isUserSubscribed($this->userLogin, $each['id'])) {
  215. $subscribeControl = la_Link('?module=megogo&unsubscribe=' . $each['id'], __('Unsubscribe'), false, 'mgunsubcontrol');
  216. } else {
  217. $userProtection = $this->checkUserProtection($each['id']);
  218. if ($userProtection) {
  219. $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.');
  220. $subscribeControl = la_ConfirmDialog('?module=megogo&subscribe=' . $each['id'], __('Subscribe'), $alertText, 'mgsubcontrol', '?module=megogo');
  221. } else {
  222. //money issues
  223. if ($userProtection === false) {
  224. $subscribeControl = __('The amount of money in your account is not sufficient to process subscription');
  225. }
  226. //tariff isnt allowed
  227. if ($userProtection === 0) {
  228. $subscribeControl = __('Your tariff does not provide this service');
  229. }
  230. }
  231. }
  232. $tariffInfo .= $subscribeControl;
  233. } else {
  234. $tariffInfo .= __('The amount of money in your account is not sufficient to process subscription');
  235. }
  236. $result .= la_tag('div', false, 'mgcontainer') . $tariffInfo . la_tag('div', true);
  237. }
  238. }
  239. return ($result);
  240. }
  241. /**
  242. * Runs default subscribtion routine
  243. *
  244. * @return void/string on error
  245. */
  246. public function pushSubscribeRequest($tariffid) {
  247. $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=mgcontrol&param=subscribe&userlogin=' . $this->userLogin . '&tariffid=' . $tariffid;
  248. @$result = file_get_contents($action);
  249. return ($result);
  250. }
  251. /**
  252. * Runs default unsubscribtion routine
  253. *
  254. * @return void/string on error
  255. */
  256. public function pushUnsubscribeRequest($tariffid) {
  257. $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=mgcontrol&param=unsubscribe&userlogin=' . $this->userLogin . '&tariffid=' . $tariffid;
  258. @$result = file_get_contents($action);
  259. return ($result);
  260. }
  261. /**
  262. * Checks have current user any subscribtions?
  263. *
  264. * @return bool
  265. */
  266. public function haveSubscribtions() {
  267. $result = false;
  268. if (!empty($this->allSubscribers)) {
  269. foreach ($this->allSubscribers as $io => $each) {
  270. if ($each['login'] == $this->userLogin) {
  271. $result = true;
  272. break;
  273. }
  274. }
  275. }
  276. return ($result);
  277. }
  278. /**
  279. * Gets auth URL via remote API
  280. *
  281. * @return string
  282. */
  283. public function getAuthButtonURL() {
  284. $result = '';
  285. $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=mgcontrol&param=auth&userlogin=' . $this->userLogin;
  286. $result = file_get_contents($action);
  287. return ($result);
  288. }
  289. /**
  290. * Check user balance for subscribtion availability
  291. *
  292. * @return bool
  293. */
  294. protected function checkBalance() {
  295. $result = false;
  296. if (!empty($this->userLogin)) {
  297. if (isset($this->allUsers[$this->userLogin])) {
  298. $userData = $this->allUsers[$this->userLogin];
  299. $userBalance = $this->allUsers[$this->userLogin]['Cash'];
  300. if ($userBalance >= 0) {
  301. $result = true;
  302. }
  303. }
  304. }
  305. return ($result);
  306. }
  307. /**
  308. * Checks free period availability for user
  309. *
  310. * @param string $login
  311. *
  312. * @return bool
  313. */
  314. protected function checkFreePeriodAvail($login) {
  315. $result = true;
  316. if (!empty($this->allHistory)) {
  317. foreach ($this->allHistory as $io => $each) {
  318. if (($each['login'] == $login) AND ( $each['freeperiod'] == 1)) {
  319. $result = false;
  320. break;
  321. }
  322. }
  323. }
  324. return ($result);
  325. }
  326. /**
  327. * Checks is user protected from his own stupidity?
  328. *
  329. * @param int $tariffId
  330. *
  331. * @return bool/int true - everything is ok, false - lack of money / 0 - tariff issues
  332. */
  333. protected function checkUserProtection($tariffId) {
  334. $tariffId = vf($tariffId, 3);
  335. $result = true;
  336. if (isset($this->usConfig['MG_PROTECTION'])) {
  337. if ($this->usConfig['MG_PROTECTION']) {
  338. if (isset($this->allTariffs[$tariffId])) {
  339. $tariffFee = $this->allTariffs[$tariffId]['fee'];
  340. $tariffData = $this->allTariffs[$tariffId];
  341. $userData = $this->allUsers[$this->userLogin];
  342. $userBalance = $userData['Cash'];
  343. if ($tariffData['freeperiod']) {
  344. if ($this->checkFreePeriodAvail($this->userLogin)) {
  345. $result = true;
  346. } else {
  347. if ($userBalance < $tariffFee) {
  348. $result = false;
  349. }
  350. }
  351. } else {
  352. if ($userBalance < $tariffFee) {
  353. $result = false;
  354. }
  355. }
  356. } else {
  357. $result = false;
  358. }
  359. }
  360. }
  361. //per-tariff protection controls
  362. if (isset($this->usConfig['MG_TARIFFSALLOWED'])) {
  363. if (!empty($this->usConfig['MG_TARIFFSALLOWED'])) {
  364. $tariffsAllowed = explode(',', $this->usConfig['MG_TARIFFSALLOWED']);
  365. $tariffsAllowed = array_flip($tariffsAllowed);
  366. $userTariff = $this->allUsers[$this->userLogin]['Tariff'];
  367. if (!isset($tariffsAllowed[$userTariff])) {
  368. $result = 0;
  369. }
  370. }
  371. }
  372. return ($result);
  373. }
  374. /**
  375. * Renders list of available subscribtions
  376. *
  377. * @return string
  378. */
  379. public function renderSubscribtions() {
  380. $result = '';
  381. $iconsPath = zbs_GetCurrentSkinPath($this->usConfig) . 'iconz/';
  382. if (!empty($this->allSubscribers)) {
  383. $cells = la_TableCell(__('Date'));
  384. $cells .= la_TableCell(__('Tariff'));
  385. $cells .= la_TableCell(__('Active'));
  386. $cells .= la_TableCell(__('Primary'));
  387. $cells .= la_TableCell(__('Free period'));
  388. $rows = la_TableRow($cells, 'row1');
  389. foreach ($this->allSubscribers as $io => $each) {
  390. if ($each['login'] == $this->userLogin) {
  391. $freePeriodFlag = ($each['freeperiod']) ? la_img($iconsPath . 'anread.gif') : la_img($iconsPath . 'anunread.gif');
  392. $primaryFlag = ($each['primary']) ? la_img($iconsPath . 'anread.gif') : la_img($iconsPath . 'anunread.gif');
  393. $activeFlag = ($each['active']) ? la_img($iconsPath . 'anread.gif') : la_img($iconsPath . 'anunread.gif');
  394. $cells = la_TableCell($each['actdate']);
  395. $cells .= la_TableCell(@$this->allTariffs[$each['tariffid']]['name']);
  396. $cells .= la_TableCell($activeFlag);
  397. $cells .= la_TableCell($primaryFlag);
  398. $cells .= la_TableCell($freePeriodFlag);
  399. $rows .= la_TableRow($cells, 'row2');
  400. }
  401. }
  402. $result = la_TableBody($rows, '100%', 0, 'resp-table');
  403. $result .= la_tag('br');
  404. //$result .= __('To view the purchased subscription register or log in to Megogo.net, by clicking the button below');
  405. }
  406. return ($result);
  407. }
  408. /**
  409. * Renders user credentials for web-auth on megogo service
  410. *
  411. * @return string/void
  412. */
  413. public function renderCredentials() {
  414. $result = '';
  415. $this->credentialsDb->where('login', '=', $this->userLogin);
  416. $userCredentials = $this->credentialsDb->getAll();
  417. if (!empty($userCredentials)) {
  418. $userCredentials = $userCredentials[0];
  419. $containerStyle = 'style="border:1px solid; text-align:center; width:100%; display:block;"';
  420. $result .= la_tag('span', false, 'resp-table', $containerStyle);
  421. $result .= __('Your login and password to usage with MEGOGO are') . ' ' . la_delimiter(1);
  422. $result .= __('Login') . ': ' . la_tag('b') . $userCredentials['email'] . la_tag('b', true) . la_tag('br');
  423. $result .= __('Password') . ': ' . la_tag('b') . $userCredentials['password'] . la_tag('b', true) . la_delimiter(1);
  424. $result .= __('To start using the MEGOGO service, click the button') . ' ' . la_Link('http://megogo.net/login', 'Continue', false, 'anreadbutton', 'target=_blank');
  425. $result .= la_delimiter(1);
  426. $result .= la_tag('span', true);
  427. }
  428. return($result);
  429. }
  430. }
  431. ?>