api.trinitytv.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. <?php
  2. class TrinityTvFrontend {
  3. /**
  4. * Contains available TrinityTv service tariffs id=>tariffdata
  5. *
  6. * @var array
  7. */
  8. protected $allTariffs = array();
  9. /**
  10. * Contains available and active TrinityTV 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. * Contains basic module controller URL
  53. *
  54. * @var string
  55. */
  56. protected $urlMe = '?module=trinitytv';
  57. /**
  58. * Contains default devices per account limit
  59. *
  60. * @var int
  61. */
  62. protected $deviceLimit = 4;
  63. const TABLE_SUBS = 'trinitytv_subscribers';
  64. const TABLE_TARIFFS = 'trinitytv_tariffs';
  65. const TABLE_DEVICES = 'trinitytv_devices';
  66. const TABLE_SUSPENDS = 'trinitytv_suspend';
  67. const TABLE_QUEUE = 'trinitytv_queue';
  68. public function __construct($url = '') {
  69. $this->setUrl($url);
  70. $this->loadUsConfig();
  71. $this->setOptions();
  72. $this->loadUsers();
  73. $this->loadTariffs();
  74. $this->loadSubscribers();
  75. }
  76. /**
  77. * Sets current user login
  78. *
  79. * @param $login
  80. */
  81. public function setLogin($login) {
  82. $this->userLogin = $login;
  83. }
  84. /**
  85. * Loads userstats config into protected usConfig variable
  86. *
  87. * @return void
  88. */
  89. protected function loadUsConfig() {
  90. $this->usConfig = zbs_LoadConfig();
  91. }
  92. /**
  93. * Control module URL setter
  94. *
  95. * @param string $url
  96. *
  97. * @return void
  98. */
  99. protected function setUrl($url) {
  100. if (!empty($url)) {
  101. $this->urlMe = $url;
  102. }
  103. }
  104. /**
  105. * Control module URL getter
  106. *
  107. * @return string
  108. */
  109. public function getUrl() {
  110. return ($this->urlMe);
  111. }
  112. /**
  113. * Sets required object options
  114. *
  115. * @return void
  116. */
  117. protected function setOptions() {
  118. $this->apiUrl = $this->usConfig['API_URL'];
  119. $this->apiKey = $this->usConfig['API_KEY'];
  120. }
  121. /**
  122. * Loads existing tariffs from database for further usage
  123. *
  124. * @return void
  125. */
  126. protected function loadTariffs() {
  127. $query = "SELECT * from " . self::TABLE_TARIFFS;
  128. $tariffs = simple_queryall($query);
  129. if (!empty($tariffs)) {
  130. foreach ($tariffs as $tariff) {
  131. $this->allTariffs[$tariff['id']] = $tariff;
  132. }
  133. }
  134. }
  135. /**
  136. * Loads existing subscribers data
  137. *
  138. * @return void
  139. */
  140. protected function loadSubscribers() {
  141. $query = "SELECT * from " . self::TABLE_SUBS;
  142. $subscribers = simple_queryall($query);
  143. if (!empty($subscribers)) {
  144. foreach ($subscribers as $subscriber) {
  145. $this->allSubscribers[$subscriber['id']] = $subscriber;
  146. }
  147. }
  148. }
  149. /**
  150. * Get subscriber devices
  151. *
  152. * @return array
  153. */
  154. protected function getSubscriberDevices() {
  155. $result = array();
  156. $subscriberId = $this->getSubscriberId($this->userLogin);
  157. if (!empty($subscriberId)) {
  158. $query = "SELECT * from `" . self::TABLE_DEVICES . "` WHERE `subscriber_id` = " . $subscriberId;
  159. $devices = simple_queryall($query);
  160. if (!empty($devices)) {
  161. foreach ($devices AS $device) {
  162. $result[$device['id']] = $device;
  163. }
  164. }
  165. }
  166. return $result;
  167. }
  168. /**
  169. * Checks can user add more devices or not?
  170. *
  171. * @return bool
  172. */
  173. public function canAddMoreDevices() {
  174. $result = true;
  175. $subscriberId = $this->getSubscriberId($this->userLogin);
  176. $query = "SELECT COUNT(`id`) from `" . self::TABLE_DEVICES . "` WHERE `subscriber_id` = '" . $subscriberId . "'";
  177. $rawData = simple_query($query);
  178. if (!empty($rawData)) {
  179. if (isset($rawData['COUNT(`id`)'])) {
  180. $devicesCount = $rawData['COUNT(`id`)'];
  181. if ($devicesCount >= $this->deviceLimit) {
  182. $result = false;
  183. }
  184. }
  185. }
  186. return($result);
  187. }
  188. /**
  189. * Loads available users from database
  190. *
  191. * @return void
  192. */
  193. protected function loadUsers() {
  194. $query = "SELECT * from `users`";
  195. $all = simple_queryall($query);
  196. if (!empty($all)) {
  197. foreach ($all as $io => $each) {
  198. $this->allUsers[$each['login']] = $each;
  199. }
  200. }
  201. }
  202. /**
  203. * Checks is user subscribed for some tariff or not?
  204. *
  205. * @param string $login
  206. * @param int $tariffid
  207. *
  208. * @return bool
  209. */
  210. protected function isUserSubscribed($login, $tariffid) {
  211. $result = false;
  212. if (!empty($this->allSubscribers)) {
  213. foreach ($this->allSubscribers as $subscriber) {
  214. if (($subscriber['login'] == $login) AND $subscriber['tariffid'] == $tariffid AND $subscriber['active']) {
  215. $result = true;
  216. break;
  217. }
  218. }
  219. }
  220. return ($result);
  221. }
  222. /**
  223. * Returns local subscriber ID from database
  224. *
  225. * @param string $userLogin
  226. *
  227. * @return int
  228. */
  229. public function getSubscriberId($userLogin) {
  230. $result = '';
  231. if (!empty($this->allSubscribers)) {
  232. foreach ($this->allSubscribers as $subscriber) {
  233. if ($subscriber['login'] == $userLogin) {
  234. $result = $subscriber['id'];
  235. break;
  236. }
  237. }
  238. }
  239. return ($result);
  240. }
  241. /**
  242. * Renders tariffs list with subscribtion form
  243. *
  244. * @return string
  245. */
  246. public function renderSubscribeForm() {
  247. $result = '';
  248. $result .= la_tag('b') . __('Attention!') . la_tag('b', true) . ' ';
  249. $result .= __('When activated subscription account will be charged fee the equivalent value of the subscription.') . la_delimiter();
  250. if (!empty($this->allTariffs)) {
  251. foreach ($this->allTariffs as $tariff) {
  252. $tariffFee = $tariff['fee'];
  253. $tariffInfo = la_tag('div', false, 'trinity-col') . la_tag('div', false, 'trinity-bl1');
  254. $tariffInfo .= la_tag('div', false, 'trinity-price');
  255. $tariffInfo .= la_tag('b', false, 's') . $tariffFee . la_tag('b', true, 's');
  256. $tariffInfo .= la_tag('sup', false) . $this->usConfig['currency'] . ' ' . la_tag('br') . ' ' . __('per month') . la_tag('sup', true);
  257. $tariffInfo .= la_tag('div', true, 'trinity-price');
  258. $tariffInfo .= la_tag('div', false, 'trinity-green s') . $tariff['name'] . la_tag('div', true, 'trinity-green s');
  259. $tariffInfo .= la_tag('br');
  260. if (!empty($tariff['description'])) {
  261. $desc = $tariff['description'];
  262. } else {
  263. $desc = __('Terms') . ': ' . la_tag('br') . $tariff['name'] . la_tag('br') . la_tag('br');
  264. }
  265. if (@$this->usConfig['TRINITYTV_CHANLIST_URL']) {
  266. $descriptionLabel = la_Link($this->usConfig['TRINITYTV_CHANLIST_URL'], $desc);
  267. } else {
  268. $descriptionLabel = $desc;
  269. }
  270. $tariffInfo .= la_tag('div', false, 'trinity-list') . $descriptionLabel . la_tag('div', true, 'trinity-list');
  271. if ($this->checkBalance()) {
  272. if ($this->isUserSubscribed($this->userLogin, $tariff['id'])) {
  273. $tariffInfo .= la_Link($this->urlMe . '&unsubscribe=' . $tariff['id'], __('Unsubscribe'), false, 'trinity-button-u');
  274. } else {
  275. if ($this->checkUserProtection($tariff['id'])) {
  276. $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.');
  277. $tariffInfo .= la_ConfirmDialog($this->urlMe . '&subscribe=' . $tariff['id'], __('Subscribe'), $alertText, 'trinity-button-s', $this->urlMe);
  278. } else {
  279. $tariffInfo .= la_tag('div', false, 'trinity-list') . __('The amount of money in your account is not sufficient to process subscription') . la_tag('div', true, 'trinity-list');
  280. }
  281. }
  282. } else {
  283. $tariffInfo .= la_tag('div', false, 'trinity-list') . __('The amount of money in your account is not sufficient to process subscription') . la_tag('div', true, 'trinity-list');
  284. }
  285. $tariffInfo .= la_tag('div', true, 'trinity-bl1') . la_tag('div', true, 'trinity-col');
  286. $result .= $tariffInfo;
  287. }
  288. }
  289. return ($result);
  290. }
  291. /**
  292. * Runs default add device mac routine
  293. *
  294. * @param $mac
  295. * @return bool|string
  296. */
  297. public function pushDeviceAddMacRequest($mac) {
  298. $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=trinitytvcontrol&param=adddevice&userlogin=' . $this->userLogin . '&mac=' . $mac;
  299. @$result = file_get_contents($action);
  300. return ($result);
  301. }
  302. /**
  303. * Runs default add device by code routine
  304. *
  305. * @param $code
  306. * @return bool|string
  307. */
  308. public function pushDeviceAddCodeRequest($code) {
  309. $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=trinitytvcontrol&param=adddevice&userlogin=' . $this->userLogin . '&code=' . $code;
  310. @$result = file_get_contents($action);
  311. return ($result);
  312. }
  313. /**
  314. * Runs default delete device by code routine
  315. *
  316. * @param $mac
  317. * @return bool|string
  318. */
  319. public function pushDeviceDeleteRequest($mac) {
  320. $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=trinitytvcontrol&param=deldevice&userlogin=' . $this->userLogin . '&mac=' . $mac;
  321. @$result = file_get_contents($action);
  322. return ($result);
  323. }
  324. /**
  325. * Runs device deletion by routine
  326. *
  327. * @param $mac
  328. * @return bool|string
  329. */
  330. public function pushDeviceIdDeleteRequest($deviceId) {
  331. $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=trinitytvcontrol&param=deldeviceid&userlogin=' . $this->userLogin . '&devid=' . $deviceId;
  332. @$result = file_get_contents($action);
  333. return ($result);
  334. }
  335. /**
  336. * Runs default subscribtion routine
  337. *
  338. * @param $tariffid
  339. * @return bool|string
  340. */
  341. public function pushSubscribeRequest($tariffid) {
  342. $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=trinitytvcontrol&param=subscribe&userlogin=' . $this->userLogin . '&tariffid=' . $tariffid;
  343. @$result = file_get_contents($action);
  344. return ($result);
  345. }
  346. /**
  347. * Runs default unsubscribtion routine
  348. *
  349. * @param $tariffid
  350. * @return bool|string
  351. */
  352. public function pushUnsubscribeRequest($tariffid) {
  353. $action = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=trinitytvcontrol&param=unsubscribe&userlogin=' . $this->userLogin . '&tariffid=' . $tariffid;
  354. @$result = file_get_contents($action);
  355. return ($result);
  356. }
  357. /**
  358. * Checks have current user any subscribtions?
  359. *
  360. * @return bool
  361. */
  362. public function haveSubscribtions() {
  363. $result = false;
  364. if (!empty($this->allSubscribers)) {
  365. foreach ($this->allSubscribers as $subscriber) {
  366. if ($subscriber['login'] == $this->userLogin) {
  367. $result = true;
  368. break;
  369. }
  370. }
  371. }
  372. return ($result);
  373. }
  374. /**
  375. * Check user balance for subscribtion availability
  376. *
  377. * @return bool
  378. */
  379. protected function checkBalance() {
  380. $result = false;
  381. if (!empty($this->userLogin)) {
  382. if (isset($this->allUsers[$this->userLogin])) {
  383. $userBalance = $this->allUsers[$this->userLogin]['Cash'];
  384. if ($userBalance >= 0) {
  385. $result = true;
  386. }
  387. }
  388. }
  389. return ($result);
  390. }
  391. /**
  392. * Checks is user protected from his own stupidity?
  393. *
  394. * @param int $tariffId
  395. * @return bool
  396. */
  397. protected function checkUserProtection($tariffId) {
  398. $tariffId = vf($tariffId, 3);
  399. $result = true;
  400. if (isset($this->usConfig['TRINITYTV_PROTECTION'])) {
  401. if ($this->usConfig['TRINITYTV_PROTECTION']) {
  402. if (isset($this->allTariffs[$tariffId])) {
  403. $tariffFee = $this->allTariffs[$tariffId]['fee'];
  404. $userData = $this->allUsers[$this->userLogin];
  405. $userBalance = $userData['Cash'];
  406. if ($userBalance < $tariffFee) {
  407. $result = false;
  408. }
  409. } else {
  410. $result = false;
  411. }
  412. }
  413. }
  414. return ($result);
  415. }
  416. /**
  417. * Renders list of available subscribtions
  418. *
  419. * @return string
  420. */
  421. public function renderSubscribtions() {
  422. $result = '';
  423. $iconsPath = zbs_GetCurrentSkinPath($this->usConfig) . 'iconz/';
  424. if (!empty($this->allSubscribers)) {
  425. $cells = la_TableCell(__('Tariff'));
  426. $cells .= la_TableCell(__('Date'));
  427. $cells .= la_TableCell(__('Active'));
  428. $rows = la_TableRow($cells, 'row1');
  429. foreach ($this->allSubscribers as $io => $each) {
  430. if ($each['login'] == $this->userLogin) {
  431. $cells = la_TableCell(@$this->allTariffs[$each['tariffid']]['name']);
  432. $activeFlag = ($each['active']) ? la_img($iconsPath . 'anread.gif') : la_img($iconsPath . 'anunread.gif');
  433. $cells .= la_TableCell($each['actdate']);
  434. $cells .= la_TableCell($activeFlag);
  435. $rows .= la_TableRow($cells, 'row2');
  436. }
  437. }
  438. $result = la_TableBody($rows, '100%', 0);
  439. $result .= la_tag('br');
  440. }
  441. return ($result);
  442. }
  443. /**
  444. * Return device
  445. *
  446. * @return string
  447. */
  448. public function renderDevices() {
  449. $result = '';
  450. if (!empty($this->userLogin)) {
  451. //available devices
  452. $devices = $this->getSubscriberDevices();
  453. $devicesCount = sizeof($devices);
  454. $noMoreDevs = false;
  455. //check for device count limit
  456. if ($devicesCount < $this->deviceLimit) {
  457. // Add device
  458. $result .= la_modalAuto(__('Assign device by MAC'), __('Assign device'), $this->renderDeviceAddForm(), 'trinity-button');
  459. // Add device by MAC
  460. $result .= la_modalAuto(__('Assign device by Code'), __('Assign device'), $this->renderDeviceByCodeAddForm(), 'trinity-button');
  461. $result .= la_tag('br') . la_tag('br');
  462. } else {
  463. $noMoreDevs = true;
  464. }
  465. $deletionAlertText = __('Delete') . '? ' . __('Are you sure') . '?';
  466. $deletionCancelUrl = $this->urlMe;
  467. $cells = la_TableCell(__('MAC') . ' ' . __('Address'));
  468. $cells .= la_TableCell(__('Date'));
  469. $cells .= la_TableCell(__('Actions'));
  470. $rows = la_TableRow($cells, 'row1');
  471. if (!empty($devices)) {
  472. foreach ($devices as $device) {
  473. $deviceLabel = (!empty($device['mac'])) ? $device['mac'] : '-';
  474. $cells = la_TableCell($deviceLabel);
  475. $cells .= la_TableCell($device['created_at']);
  476. if (!empty($device['mac'])) {
  477. $deviceControls = la_JSAlert($this->urlMe . '&deletedevice=' . $device['mac'], __('Delete'), __('Are you sure') . '?');
  478. $deletionUrl = $this->urlMe . '&deletedevice=' . $device['mac'];
  479. $deviceControls = la_ConfirmDialog($deletionUrl, __('Delete'), $deletionAlertText, '', $deletionCancelUrl);
  480. } else {
  481. //device by ID deletion workaround
  482. $deviceControls = la_JSAlert($this->urlMe . '&deletedeviceid=' . $device['id'], __('Delete'), __('Are you sure') . '?');
  483. $deletionUrl = $this->urlMe . '&deletedeviceid=' . $device['id'];
  484. $deviceControls = la_ConfirmDialog($deletionUrl, __('Delete'), $deletionAlertText, '', $deletionCancelUrl);
  485. }
  486. $cells .= la_TableCell($deviceControls);
  487. $rows .= la_TableRow($cells, 'row3');
  488. }
  489. }
  490. $result .= la_TableBody($rows, '100%', 0, 'sortable');
  491. if ($noMoreDevs) {
  492. $result .= __('Devices count limit is exceeded');
  493. }
  494. }
  495. return ($result);
  496. }
  497. /**
  498. * Renders manual device assign form
  499. *
  500. * @return string
  501. */
  502. protected function renderDeviceAddForm() {
  503. $result = '';
  504. $inputs = la_HiddenInput('device', 'true');
  505. $inputs .= la_TextInput('mac', __('MAC'), '', true, 20, 'mac');
  506. $inputs .= la_Submit(__('Assign device'));
  507. $result .= la_Form('', 'POST', $inputs, 'glamour', '', false);
  508. return ($result);
  509. }
  510. /**
  511. * Renders manual device assign form
  512. *
  513. * @return string
  514. */
  515. protected function renderDeviceByCodeAddForm() {
  516. $result = '';
  517. $inputs = la_HiddenInput('device', 'true');
  518. $inputs .= la_TextInput('code', __('Code'), '', true, 20, 'digits');
  519. $inputs .= la_Submit(__('Assign device'));
  520. $result .= la_Form('', 'POST', $inputs, 'glamour');
  521. return ($result);
  522. }
  523. }
  524. ?>