api.omegatv.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. <?php
  2. class OmegaTvFrontend {
  3. /**
  4. * Contains available omegatv service tariffs id=>tariffdata
  5. *
  6. * @var array
  7. */
  8. protected $allTariffs = array();
  9. /**
  10. * Contains all tariff names as tariffid=>name
  11. *
  12. * @var array
  13. */
  14. protected $tariffNames = array();
  15. /**
  16. * Contains all of internet users data as login=>data
  17. *
  18. * @var array
  19. */
  20. protected $allUsers = array();
  21. /**
  22. * Contains available and active omegatv service subscriptions as customerid=>data
  23. *
  24. * @var array
  25. */
  26. protected $allSubscribers = 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. * Contains some configurable amount of maximum count of devices/playlists per user
  53. *
  54. * @var int
  55. */
  56. protected $maxDevices = 3;
  57. public function __construct() {
  58. $this->loadUsConfig();
  59. $this->setOptions();
  60. $this->loadTariffs();
  61. $this->loadUsers();
  62. $this->loadUserSubscriptions();
  63. }
  64. /**
  65. * Loads userstats config into protected usConfig variable
  66. *
  67. * @return void
  68. */
  69. protected function loadUsConfig() {
  70. $this->usConfig = zbs_LoadConfig();
  71. }
  72. /**
  73. * Sets required object options
  74. *
  75. * @return void
  76. */
  77. protected function setOptions() {
  78. $this->apiUrl = $this->usConfig['API_URL'];
  79. $this->apiKey = $this->usConfig['API_KEY'];
  80. if (isset($this->usConfig['OM_MAXDEV'])) {
  81. $this->maxDevices = $this->usConfig['OM_MAXDEV'];
  82. }
  83. }
  84. /**
  85. * Loads available users from database
  86. *
  87. * @return void
  88. */
  89. protected function loadUsers() {
  90. $query = "SELECT * from `users`";
  91. $all = simple_queryall($query);
  92. if (!empty($all)) {
  93. foreach ($all as $io => $each) {
  94. $this->allUsers[$each['login']] = $each;
  95. }
  96. }
  97. }
  98. /**
  99. * Loads existing users profiles/subscriptions
  100. *
  101. * @return void
  102. */
  103. protected function loadUserSubscriptions() {
  104. $query = "SELECT * from `om_users`";
  105. $all = simple_queryall($query);
  106. if (!empty($all)) {
  107. foreach ($all as $io => $each) {
  108. $this->allSubscribers[$each['customerid']] = $each;
  109. }
  110. }
  111. }
  112. /**
  113. * Sets current user login
  114. *
  115. * @return void
  116. */
  117. public function setLogin($login) {
  118. $this->userLogin = $login;
  119. }
  120. /**
  121. * Loads existing tariffs from database
  122. *
  123. * @return void
  124. */
  125. protected function loadTariffs() {
  126. $query = "SELECT * from `om_tariffs` WHERE `type`='base' OR `type`='bundle' ORDER BY `type` ASC";
  127. $all = simple_queryall($query);
  128. if (!empty($all)) {
  129. foreach ($all as $io => $each) {
  130. $this->allTariffs[$each['id']] = $each;
  131. $this->tariffNames[$each['tariffid']] = $each['tariffname'];
  132. }
  133. }
  134. }
  135. /**
  136. * Checks is user subscribed for some tariff or not?
  137. *
  138. * @param string $login
  139. * @param int $tariffid
  140. *
  141. * @return bool
  142. */
  143. protected function isUserSubscribed($login, $tariffid) {
  144. $result = false;
  145. if (!empty($this->allSubscribers)) {
  146. $tariffExternalId = $this->allTariffs[$tariffid]['tariffid'];
  147. foreach ($this->allSubscribers as $io => $each) {
  148. if (($each['login'] == $login)) {
  149. if ($each['basetariffid'] == $tariffExternalId) {
  150. $result = true;
  151. break;
  152. }
  153. if (!empty($each['bundletariffs'])) {
  154. $bundleTariffs = unserialize($each['bundletariffs']);
  155. if (isset($bundleTariffs[$tariffExternalId])) {
  156. $result = true;
  157. break;
  158. }
  159. }
  160. }
  161. }
  162. }
  163. return ($result);
  164. }
  165. /**
  166. * Returns user login transformed to some numeric hash
  167. *
  168. * @param string $login
  169. *
  170. * @return int
  171. */
  172. public function generateCustormerId($login) {
  173. $result = '';
  174. if (!empty($login)) {
  175. $result = crc32($login);
  176. }
  177. return($result);
  178. }
  179. /**
  180. * Renders available user devices list and some controls
  181. *
  182. * @return string
  183. */
  184. public function renderUserDevicesForm() {
  185. $result = '';
  186. if (!empty($this->userLogin)) {
  187. //available devices
  188. $currentDevices = $this->getDevicesData();
  189. $currentPlaylists = $this->getPlaylistsData();
  190. $devCount = 0;
  191. $rows = '';
  192. if (!empty($currentDevices) OR ( !empty($currentPlaylists))) {
  193. $currentDevices = json_decode($currentDevices, true);
  194. $currentPlaylists = json_decode($currentPlaylists, true);
  195. if (!empty($currentDevices)) {
  196. foreach ($currentDevices as $io => $each) {
  197. $cells = la_TableCell($each['uniq']);
  198. $cells .= la_TableCell(date("Y-m-d H:i:s", $each['activation_data']));
  199. $cells .= la_TableCell($each['model']);
  200. $deviceControls = la_JSAlert('?module=omegatv&deletedevice=' . $each['uniq'], __('Delete'), __('Are you sure') . '?');
  201. $cells .= la_TableCell($deviceControls);
  202. $rows .= la_TableRow($cells, 'row3');
  203. $devCount++;
  204. }
  205. }
  206. if (!empty($currentPlaylists)) {
  207. foreach ($currentPlaylists as $io => $each) {
  208. $cells = la_TableCell($each['uniq']);
  209. $actDate = ($each['activation_data']) ? date("Y-m-d H:i:s", $each['activation_data']) : '-';
  210. $cells .= la_TableCell($actDate);
  211. $playlistControls = la_Link($each['url'], __('Playlist'));
  212. $cells .= la_TableCell($playlistControls);
  213. $deviceControls = la_JSAlert('?module=omegatv&deleteplaylist=' . $each['uniq'], __('Delete'), __('Are you sure') . '?');
  214. $cells .= la_TableCell($deviceControls);
  215. $rows .= la_TableRow($cells, 'row3');
  216. $devCount++;
  217. }
  218. }
  219. $result .= la_TableBody($rows, '100%', 0, 'sortable');
  220. }
  221. //maximum devices limit
  222. if ($devCount < $this->maxDevices) {
  223. //new device activation
  224. if (la_CheckGet(array('getcode'))) {
  225. $actCode = $this->getDeviceActivationCode();
  226. $result .= la_tag('br');
  227. $result .= la_tag('h3', false) . __('Activation code') . ': ' . $actCode . la_tag('h3', true);
  228. } else {
  229. $result .= la_tag('br');
  230. $actCodeControl = la_Link('?module=omegatv&getcode=true', __('Get device activation code'));
  231. $newPlControl = la_Link('?module=omegatv&newplaylist=true', __('Add playlist'));
  232. $result .= $actCodeControl . ' / ' . $newPlControl;
  233. }
  234. } else {
  235. $result .= __('Devices count limit is exceeded');
  236. }
  237. }
  238. return ($result);
  239. }
  240. /**
  241. * Checks is user protected from his own stupidity?
  242. *
  243. * @param int $tariffId
  244. * @return bool
  245. */
  246. protected function checkUserProtection($tariffId) {
  247. $tariffId = vf($tariffId, 3);
  248. $result = true;
  249. if (isset($this->usConfig['OM_PROTECTION'])) {
  250. if ($this->usConfig['OM_PROTECTION']) {
  251. if (isset($this->allTariffs[$tariffId])) {
  252. $tariffFee = $this->allTariffs[$tariffId]['fee'];
  253. $tariffData = $this->allTariffs[$tariffId];
  254. $userData = $this->allUsers[$this->userLogin];
  255. $userBalance = $userData['Cash'];
  256. if ($userBalance < $tariffFee) {
  257. $result = false;
  258. }
  259. } else {
  260. $result = false;
  261. }
  262. }
  263. }
  264. return ($result);
  265. }
  266. /**
  267. * Renders tariffs list with subscribtion form
  268. *
  269. * @return string
  270. */
  271. public function renderSubscribeForm() {
  272. $result = '';
  273. $iconsPath = zbs_GetCurrentSkinPath($this->usConfig) . 'iconz/';
  274. if (!empty($this->allTariffs)) {
  275. foreach ($this->allTariffs as $io => $each) {
  276. $headerType = ($each['type'] == 'base') ? 'mgheaderprimary' : 'mgheader';
  277. $freeAppend = la_delimiter();
  278. $tariffFee = $each['fee'];
  279. $primaryLabel = ($each['type'] == 'base') ? la_img($iconsPath . 'ok_small.png') : la_img($iconsPath . 'unavail_small.png');
  280. $subscribedLabel = ($this->isUserSubscribed($this->userLogin, $each['id'])) ? la_img($iconsPath . 'ok_small.png') : la_img($iconsPath . 'unavail_small.png');
  281. $tariffInfo = la_tag('div', false, $headerType) . $each['tariffname'] . la_tag('div', true);
  282. $cells = la_TableCell(la_tag('b') . __('Fee') . la_tag('b', true));
  283. $cells .= la_TableCell($tariffFee . ' ' . $this->usConfig['currency']);
  284. $rows = la_TableRow($cells);
  285. $cells = la_TableCell(la_tag('b') . __('Base') . la_tag('b', true));
  286. $cells .= la_TableCell($primaryLabel);
  287. $rows .= la_TableRow($cells);
  288. $cells = la_TableCell(la_tag('b') . __('You subscribed') . la_tag('b', true));
  289. $cells .= la_TableCell($subscribedLabel);
  290. $rows .= la_TableRow($cells);
  291. $tariffInfo .= la_TableBody($rows, '100%', 0);
  292. $tariffInfo .= $freeAppend;
  293. if ($this->checkBalance()) {
  294. if ($this->isUserSubscribed($this->userLogin, $each['id'])) {
  295. $subscribeControl = la_Link('?module=omegatv&unsubscribe=' . $each['tariffid'], __('Unsubscribe'), false, 'mgunsubcontrol');
  296. } else {
  297. if ($this->checkUserProtection($each['id'])) {
  298. $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.');
  299. $subscribeControl = la_ConfirmDialog('?module=omegatv&subscribe=' . $each['tariffid'], __('Subscribe'), $alertText, 'mgsubcontrol', '?module=omegatv');
  300. } else {
  301. $subscribeControl = __('The amount of money in your account is not sufficient to process subscription');
  302. }
  303. }
  304. $tariffInfo .= $subscribeControl;
  305. } else {
  306. $tariffInfo .= __('The amount of money in your account is not sufficient to process subscription');
  307. }
  308. $result .= la_tag('div', false, 'mgcontainer') . $tariffInfo . la_tag('div', true);
  309. }
  310. }
  311. return ($result);
  312. }
  313. /**
  314. * Check user balance for subscribtion availability
  315. *
  316. * @return bool
  317. */
  318. protected function checkBalance() {
  319. $result = false;
  320. if (!empty($this->userLogin)) {
  321. if (isset($this->allUsers[$this->userLogin])) {
  322. $userData = $this->allUsers[$this->userLogin];
  323. $userBalance = $this->allUsers[$this->userLogin]['Cash'];
  324. if ($userBalance >= 0) {
  325. $result = true;
  326. }
  327. }
  328. }
  329. return ($result);
  330. }
  331. /**
  332. * Returns local customer ID from database
  333. *
  334. * @param string $userLogin
  335. *
  336. * @return int
  337. */
  338. public function getLocalCustomerId() {
  339. $result = '';
  340. if (!empty($this->allSubscribers)) {
  341. foreach ($this->allSubscribers as $io => $each) {
  342. if ($each['login'] == $this->userLogin) {
  343. $result = $each['customerid'];
  344. break;
  345. }
  346. }
  347. }
  348. return ($result);
  349. }
  350. /**
  351. * Returns array of subscribed tariff Ids=>type
  352. *
  353. * @return array
  354. */
  355. public function getSubscribedTariffs() {
  356. $result = array();
  357. $customerId = $this->getLocalCustomerId();
  358. if (!empty($customerId)) {
  359. if (isset($this->allSubscribers[$customerId])) {
  360. $localCustomerData = $this->allSubscribers[$customerId];
  361. if (!empty($localCustomerData['basetariffid'])) {
  362. $result[$localCustomerData['basetariffid']] = 'base';
  363. }
  364. if (!empty($localCustomerData['bundletariffs'])) {
  365. $bundleTariffs = unserialize($localCustomerData['bundletariffs']);
  366. if (!empty($bundleTariffs)) {
  367. foreach ($bundleTariffs as $io => $each) {
  368. $result[$io] = 'bundle';
  369. }
  370. }
  371. }
  372. }
  373. }
  374. return ($result);
  375. }
  376. /**
  377. * Gets view URL via remote API
  378. *
  379. * @return string
  380. */
  381. public function getViewButtonURL() {
  382. $result = '';
  383. $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=omcontrol&param=viewurl&userlogin=' . $this->userLogin;
  384. @$result = file_get_contents($action);
  385. return ($result);
  386. }
  387. /**
  388. * Gets device activation code via remote API
  389. *
  390. * @return string
  391. */
  392. public function getDeviceActivationCode() {
  393. $result = '';
  394. $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=omcontrol&param=getcode&userlogin=' . $this->userLogin;
  395. @$result = file_get_contents($action);
  396. return ($result);
  397. }
  398. /**
  399. * Gets devices data via remote API
  400. *
  401. * @return string
  402. */
  403. public function getDevicesData() {
  404. $result = '';
  405. $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=omcontrol&param=getdevices&userlogin=' . $this->userLogin;
  406. @$result = file_get_contents($action);
  407. return ($result);
  408. }
  409. /**
  410. * Gets playlists data via remote API
  411. *
  412. * @return string
  413. */
  414. public function getPlaylistsData() {
  415. $result = '';
  416. $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=omcontrol&param=getplaylists&userlogin=' . $this->userLogin;
  417. @$result = file_get_contents($action);
  418. return ($result);
  419. }
  420. /**
  421. * Pushes device deletion request via remote API
  422. *
  423. * @param string $uniq
  424. *
  425. * @return string
  426. */
  427. public function pushDeviceDelete($uniq) {
  428. $result = '';
  429. $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=omcontrol&param=deletedev&userlogin=' . $this->userLogin . '&uniq=' . $uniq;
  430. @$result = file_get_contents($action);
  431. return ($result);
  432. }
  433. /**
  434. * Pushes playlist deletion request via remote API
  435. *
  436. * @param string $uniq
  437. *
  438. * @return string
  439. */
  440. public function pushPlaylistDelete($uniq) {
  441. $result = '';
  442. $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=omcontrol&param=deletepl&userlogin=' . $this->userLogin . '&uniq=' . $uniq;
  443. @$result = file_get_contents($action);
  444. return ($result);
  445. }
  446. /**
  447. * Pushes playlist assign request via remote API
  448. *
  449. * @param string $uniq
  450. *
  451. * @return string
  452. */
  453. public function pushPlaylistAssign() {
  454. $result = '';
  455. $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=omcontrol&param=assignpl&userlogin=' . $this->userLogin;
  456. @$result = file_get_contents($action);
  457. return ($result);
  458. }
  459. /**
  460. * Pushes tariff subscription request via remote API
  461. *
  462. * @param int $tariffId
  463. *
  464. * @return string
  465. */
  466. public function pushSubscribeRequest($tariffId) {
  467. $result = '';
  468. $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=omcontrol&param=subscribe&userlogin=' . $this->userLogin . '&tariffid=' . $tariffId;
  469. @$result = file_get_contents($action);
  470. return ($result);
  471. }
  472. /**
  473. * Pushes tariff unsubscription request via remote API
  474. *
  475. * @param int $tariffId
  476. *
  477. * @return string
  478. */
  479. public function pushUnsubscribeRequest($tariffId) {
  480. $result = '';
  481. $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=omcontrol&param=unsubscribe&userlogin=' . $this->userLogin . '&tariffid=' . $tariffId;
  482. @$result = file_get_contents($action);
  483. return ($result);
  484. }
  485. }
  486. ?>