api.youtv.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <?php
  2. /**
  3. * YouTV users frontend basic class
  4. */
  5. class YTVInterface {
  6. /**
  7. * Contains current instance user login
  8. *
  9. * @var string
  10. */
  11. protected $myLogin = '';
  12. /**
  13. * Contains userstats config as key=>value
  14. *
  15. * @var array
  16. */
  17. protected $usConfig = array();
  18. /**
  19. * Contains service-side subscriber ID
  20. *
  21. * @var int
  22. */
  23. protected $subscriberId = 0;
  24. /**
  25. * Contains current instance subscriber data
  26. *
  27. * @var array
  28. */
  29. protected $subscriberData = array();
  30. /**
  31. * Contains remote subscriber full data
  32. *
  33. * @var array
  34. */
  35. protected $fullData = array();
  36. /**
  37. * Contains available tariffs data as serviceId=>tariffData
  38. *
  39. * @var array
  40. */
  41. protected $tariffsData = array();
  42. /**
  43. * Contains all users data as login=>userdata
  44. *
  45. * @var string
  46. */
  47. protected $allUsers = array();
  48. /**
  49. * Some predefined routes/URLs etc..
  50. */
  51. const URL_ME = '?module=omyoutv';
  52. const REQ_BASE = '&action=youtvui&';
  53. /**
  54. * Creates new instance
  55. *
  56. * @param string $userLogin
  57. */
  58. public function __construct($userLogin) {
  59. if (!empty($userLogin)) {
  60. $this->loadConfig();
  61. $this->setLogin($userLogin);
  62. $this->loadUsers();
  63. $this->subscriberData = $this->getSubscriberData();
  64. if (!empty($this->subscriberData)) {
  65. $this->subscriberId = $this->subscriberData['subscriberid'];
  66. $this->fullData = $this->getFullData();
  67. }
  68. $this->tariffsData = $this->getTariffsData();
  69. } else {
  70. die('ERROR:NO_USER_LOGIN');
  71. }
  72. }
  73. /**
  74. * Sets current instance user login
  75. *
  76. * @param string $userLogin
  77. *
  78. * @return void
  79. */
  80. protected function setLogin($userLogin) {
  81. $this->myLogin = $userLogin;
  82. }
  83. /**
  84. * Preloads userstats config to protected property
  85. *
  86. * @global array $us_config
  87. *
  88. * @return void
  89. */
  90. protected function loadConfig() {
  91. global $us_config;
  92. $this->usConfig = $us_config;
  93. }
  94. /**
  95. * Performs some RemoteAPI request and returns its results as array
  96. *
  97. * @param string $request
  98. *
  99. * @return array/bool on error
  100. */
  101. protected function getRemoteData($request) {
  102. $result = false;
  103. if (!empty($request)) {
  104. $requestUrl = self::REQ_BASE . $request;
  105. $rawReply = zbs_remoteApiRequest($requestUrl);
  106. if (!empty($rawReply)) {
  107. $result = json_decode($rawReply, true);
  108. }
  109. }
  110. return($result);
  111. }
  112. /**
  113. * Returns some subscriber data assigned to s
  114. *
  115. * @return array
  116. */
  117. protected function getSubscriberData() {
  118. $request = 'subdata=' . $this->myLogin;
  119. $result = $this->getRemoteData($request);
  120. return($result);
  121. }
  122. /**
  123. * Returns current subscriberId or void if user is unregistered yet.
  124. *
  125. * @return int/void
  126. */
  127. public function getSubscriberId() {
  128. return($this->subscriberId);
  129. }
  130. /**
  131. * Checks is user use service?
  132. *
  133. * @return bool
  134. */
  135. public function userUseService() {
  136. $result = false;
  137. if (!empty($this->subscriberData)) {
  138. if ($this->subscriberData['maintariff']) {
  139. $result = true;
  140. }
  141. }
  142. return($result);
  143. }
  144. /**
  145. * Returns available tariffs data
  146. *
  147. * @return array
  148. */
  149. protected function getTariffsData() {
  150. $request = 'tardata=true';
  151. $result = $this->getRemoteData($request);
  152. return($result);
  153. }
  154. /**
  155. * Returns full subscriber data
  156. *
  157. * @return array
  158. */
  159. protected function getFullData() {
  160. $request = 'fulldata=' . $this->myLogin;
  161. $result = $this->getRemoteData($request);
  162. return($result);
  163. }
  164. /**
  165. * Loads available users data from database
  166. *
  167. * @return void
  168. */
  169. protected function loadUsers() {
  170. $query = "SELECT * from `users` WHERE `login`='" . $this->myLogin . "'";
  171. $all = simple_queryall($query);
  172. if (!empty($all)) {
  173. foreach ($all as $io => $each) {
  174. $this->allUsers[$each['login']] = $each;
  175. }
  176. }
  177. }
  178. /**
  179. * Renders standard bool led
  180. *
  181. * @param mixed $state
  182. *
  183. * @return string
  184. */
  185. protected function webBoolLed($state) {
  186. $iconsPath = zbs_GetCurrentSkinPath($this->usConfig) . 'iconz/';
  187. $result = ($state) ? la_img($iconsPath . 'anread.gif') : la_img($iconsPath . 'anunread.gif');
  188. return($result);
  189. }
  190. /**
  191. * Renders current subscription details
  192. *
  193. * @return string
  194. */
  195. public function renderSubscriptionDetails() {
  196. $result = '';
  197. if (!empty($this->subscriberData) AND $this->subscriberData['active'] == 1) {
  198. $mainTariff = @$this->tariffsData[$this->subscriberData['maintariff']];
  199. $cells = la_TableCell(__('Active'));
  200. $cells .= la_TableCell(__('Tariff'));
  201. $cells .= la_TableCell(__('Primary'));
  202. $cells .= la_TableCell(__('Fee'));
  203. $rows = la_TableRow($cells, 'row1');
  204. if (!empty($mainTariff)) {
  205. $cells = la_TableCell($this->webBoolLed($this->subscriberData['active']));
  206. $cells .= la_TableCell($mainTariff['name']);
  207. $cells .= la_TableCell($this->webBoolLed($mainTariff['main']));
  208. $cells .= la_TableCell($mainTariff['fee'] . ' ' . $this->usConfig['currency']);
  209. $rows .= la_TableRow($cells, 'row1');
  210. }
  211. $result .= la_TableBody($rows, '100%', 0, 'resp-table');
  212. } else {
  213. $result = __('No subscriptions yet');
  214. }
  215. return($result);
  216. }
  217. /**
  218. * Check user balance for subscribtion availability
  219. *
  220. * @return bool
  221. */
  222. protected function checkBalance() {
  223. $result = false;
  224. if (!empty($this->myLogin)) {
  225. if (isset($this->allUsers[$this->myLogin])) {
  226. $userBalance = $this->allUsers[$this->myLogin]['Cash'];
  227. if ($userBalance >= 0) {
  228. $result = true;
  229. }
  230. }
  231. }
  232. return ($result);
  233. }
  234. /**
  235. * Checks is user protected from his own stupidity?
  236. *
  237. * @param int $tariffId
  238. *
  239. * @return bool
  240. */
  241. protected function checkUserProtection($tariffId) {
  242. $tariffId = vf($tariffId, 3);
  243. $result = true;
  244. if (isset($this->tariffsData[$tariffId])) {
  245. $tariffFee = $this->tariffsData[$tariffId]['fee'];
  246. $userData = $this->allUsers[$this->myLogin];
  247. $userBalance = $userData['Cash'];
  248. if ($userBalance < $tariffFee) {
  249. $result = false;
  250. }
  251. } else {
  252. $result = false;
  253. }
  254. return ($result);
  255. }
  256. /**
  257. * Checks is user subscribed for some tariff or not?
  258. *
  259. * @param int $tariffid
  260. *
  261. * @return bool
  262. */
  263. protected function isUserSubscribed($tariffid) {
  264. $result = false;
  265. if (!empty($this->subscriberData)) {
  266. if ($this->subscriberData['active']) {
  267. if ($this->subscriberData['maintariff'] == $tariffid) {
  268. $result = true;
  269. }
  270. }
  271. }
  272. return ($result);
  273. }
  274. public function renderInfoForm(){
  275. $result = '';
  276. if (!empty($this->subscriberData) AND $this->subscriberData['active'] == 1) {
  277. $result .= la_tag('b') . __('Authorization data') . ': ' . la_tag('b', true) . la_tag('br');
  278. $result .= __('Login') . ': ' . zbs_UserGetEmail($this->myLogin) . la_tag('br');
  279. $result .= __('Password') . ': ' . $this->allUsers[$this->myLogin]['Password'] . la_delimiter();;
  280. // youtv promo start
  281. $result .= la_tag('div', false, 'text-center', 'style="background: url(//youtv.ua/assets/images/svg/components/abstract-shapes-19.svg) center no-repeat;"');
  282. $result .= la_tag('h2') . __('Convenient applications') . la_tag('h2', true);
  283. $result .= la_tag('p') . __('Modern youtv applications for various devices.') . la_tag('p', true);
  284. $result .= la_tag('div', false, 'mt-2 mx-n8')
  285. . la_Link('https://play.google.com/store/apps/details?id=ua.youtv.youtv&amp;hl=uk', la_img('skins/paper/iconz/google_play.png'))
  286. . la_Link('https://apps.apple.com/us/app/you-tv-onlajn-tv/id1176282993?l=uk', la_img('skins/paper/iconz/app_store.png'))
  287. . la_tag('div', true);
  288. $result .= la_tag('div', false, 'mt-2 mx-n8')
  289. . la_Link('#', la_img('skins/paper/iconz/smart_tv.png'))
  290. . la_Link('https://appgallery.huawei.com/#/app/C103041047', la_img('skins/paper/iconz/app_gallery.png'))
  291. . la_tag('div', true);
  292. $result .= la_tag('div', true); // end youtv promo
  293. } else {
  294. $result = '';
  295. }
  296. return $result;
  297. }
  298. /**
  299. * Renders available subscriptions list
  300. *
  301. * @return string
  302. */
  303. public function renderSubscribeForm() {
  304. $result = '';
  305. $result .= la_tag('b') . __('Attention!') . la_tag('b', true) . ' ';
  306. $result .= __('When activated subscription account will be charged fee the equivalent value of the subscription.') . la_delimiter();
  307. if (!empty($this->tariffsData)) {
  308. foreach ($this->tariffsData as $serviceId => $tariff) {
  309. $tariffFee = $tariff['fee'];
  310. $tariffInfo = la_tag('div', false, 'youtv-col') . la_tag('div', false, 'youtv-bl1');
  311. $tariffInfo .= la_tag('div', false, 'youtv-price');
  312. $tariffInfo .= la_tag('b', false, 's') . $tariffFee . la_tag('b', true, 's');
  313. $tariffInfo .= la_tag('sup', false) . $this->usConfig['currency'] . ' ' . la_tag('br') . ' ' . __('per month') . la_tag('sup', true);
  314. $tariffInfo .= la_tag('div', true, 'youtv-price');
  315. $tariffInfo .= la_tag('div', false, 'youtv-red s') . $tariff['name'] . la_tag('div', true, 'youtv-red s');
  316. $tariffInfo .= la_tag('br');
  317. if (!empty($tariff['chans'])) {
  318. $desc = $tariff['chans'];
  319. } else {
  320. $desc = '';
  321. }
  322. $descriptionLabel = $desc;
  323. $tariffInfo .= la_tag('div', false, 'youtv-list') . $descriptionLabel . la_tag('div', true, 'youtv-list');
  324. if ($this->checkBalance()) {
  325. if ($this->isUserSubscribed($tariff['serviceid'])) {
  326. $tariffInfo .= la_Link(self::URL_ME . '&unsubscribe=' . $tariff['serviceid'], __('Unsubscribe'), false, 'youtv-button-u');
  327. } else {
  328. if ($this->checkUserProtection($tariff['serviceid'])) {
  329. $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.');
  330. $tariffInfo .= la_ConfirmDialog(self::URL_ME . '&subscribe=' . $tariff['serviceid'], __('Subscribe'), $alertText, 'youtv-button-s', self::URL_ME);
  331. } else {
  332. $tariffInfo .= la_tag('div', false, 'youtv-list') . __('The amount of money in your account is not sufficient to process subscription') . la_tag('div', true, 'youtv-list');
  333. }
  334. }
  335. } else {
  336. $tariffInfo .= la_tag('div', false, 'youtv-list') . __('The amount of money in your account is not sufficient to process subscription') . la_tag('div', true, 'youtv-list');
  337. }
  338. $tariffInfo .= la_tag('div', true, 'youtv-bl1') . la_tag('div', true, 'youtv-col');
  339. $result .= $tariffInfo;
  340. }
  341. }
  342. return($result);
  343. }
  344. /**
  345. * Deactivates user service due deleting of tariff
  346. *
  347. * @param int $tariffId
  348. *
  349. * @return void
  350. */
  351. public function unsubscribe($tariffId) {
  352. $request = 'unsub=' . $tariffId . '&subid=' . $this->subscriberId;
  353. $this->getRemoteData($request);
  354. }
  355. /**
  356. * Activates new service for user
  357. *
  358. * @param int $tariffId
  359. *
  360. * @return void
  361. */
  362. public function subscribe($tariffId) {
  363. $request = 'subserv=' . $tariffId . '&sublogin=' . $this->myLogin;
  364. $this->getRemoteData($request);
  365. }
  366. }