api.olltv.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. <?php
  2. /**
  3. * OllTV users frontend basic class
  4. */
  5. class OllTvInterface {
  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 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=olltv';
  52. const REQ_BASE = '&action=olltvui&';
  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['id'];
  66. }
  67. $this->tariffsData = $this->getTariffsData();
  68. } else {
  69. die('ERROR:NO_USER_LOGIN');
  70. }
  71. }
  72. /**
  73. * Sets current instance user login
  74. *
  75. * @param string $userLogin
  76. *
  77. * @return void
  78. */
  79. protected function setLogin($userLogin) {
  80. $this->myLogin = $userLogin;
  81. }
  82. /**
  83. * Preloads userstats config to protected property
  84. *
  85. * @global array $us_config
  86. *
  87. * @return void
  88. */
  89. protected function loadConfig() {
  90. global $us_config;
  91. $this->usConfig = $us_config;
  92. }
  93. /**
  94. * Performs some RemoteAPI request and returns its results as array
  95. *
  96. * @param string $request
  97. *
  98. * @return array/bool on error
  99. */
  100. protected function getRemoteData($request) {
  101. $result = false;
  102. if (!empty($request)) {
  103. $requestUrl = self::REQ_BASE . $request;
  104. $rawReply = zbs_remoteApiRequest($requestUrl);
  105. if (!empty($rawReply)) {
  106. $result = json_decode($rawReply, true);
  107. }
  108. }
  109. return($result);
  110. }
  111. /**
  112. * Returns some subscriber data assigned to s
  113. *
  114. * @return array
  115. */
  116. protected function getSubscriberData() {
  117. $request = 'subdata=' . $this->myLogin;
  118. $result = $this->getRemoteData($request);
  119. return($result);
  120. }
  121. /**
  122. * Returns current subscriberId or void if user is unregistered yet.
  123. *
  124. * @return int/void
  125. */
  126. public function getSubscriberId() {
  127. return($this->subscriberId);
  128. }
  129. /**
  130. * Checks is user use service?
  131. *
  132. * @return bool
  133. */
  134. public function userUseService() {
  135. $result = false;
  136. if (!empty($this->subscriberData)) {
  137. if ($this->subscriberData['tariffid']) {
  138. $result = true;
  139. }
  140. }
  141. return($result);
  142. }
  143. /**
  144. * Returns available tariffs data
  145. *
  146. * @return array
  147. */
  148. protected function getTariffsData() {
  149. $request = 'tardata=true';
  150. $result = $this->getRemoteData($request);
  151. return($result);
  152. }
  153. /**
  154. * Loads available users data from database
  155. *
  156. * @return void
  157. */
  158. protected function loadUsers() {
  159. $query = "SELECT * from `users` WHERE `login`='" . $this->myLogin . "'";
  160. $all = simple_queryall($query);
  161. if (!empty($all)) {
  162. foreach ($all as $io => $each) {
  163. $this->allUsers[$each['login']] = $each;
  164. }
  165. }
  166. }
  167. /**
  168. * Renders standard bool led
  169. *
  170. * @param mixed $state
  171. *
  172. * @return string
  173. */
  174. protected function webBoolLed($state) {
  175. $iconsPath = zbs_GetCurrentSkinPath($this->usConfig) . 'iconz/';
  176. $result = ($state) ? la_img($iconsPath . 'anread.gif', __('Yes')) : la_img($iconsPath . 'anunread.gif', __('No'));
  177. return($result);
  178. }
  179. /**
  180. * Renders current subscription details
  181. *
  182. * @return string
  183. */
  184. public function renderSubscriptionDetails() {
  185. $result = '';
  186. if (!empty($this->subscriberData)) {
  187. $mainTariff = @$this->tariffsData[$this->subscriberData['tariffid']];
  188. if (!empty($mainTariff)) {
  189. $cells = la_TableCell(__('Active'));
  190. $cells .= la_TableCell(__('Tariff'));
  191. $cells .= la_TableCell(__('Primary'));
  192. $cells .= la_TableCell(__('Fee'));
  193. $rows = la_TableRow($cells, 'row1');
  194. $cells = la_TableCell($this->webBoolLed($this->subscriberData['active']));
  195. $cells .= la_TableCell($mainTariff['name']);
  196. $cells .= la_TableCell($this->webBoolLed($mainTariff['main']));
  197. $cells .= la_TableCell($mainTariff['fee'] . ' ' . $this->usConfig['currency']);
  198. $rows .= la_TableRow($cells, 'row3');
  199. $additionalTariff = @$this->tariffsData[$this->subscriberData['addtariffid']];
  200. if ($additionalTariff) {
  201. $cells = la_TableCell($this->webBoolLed($this->subscriberData['active']));
  202. $cells .= la_TableCell($additionalTariff['name']);
  203. $cells .= la_TableCell($this->webBoolLed($additionalTariff['main']));
  204. $cells .= la_TableCell($additionalTariff['fee'] . ' ' . $this->usConfig['currency']);
  205. $rows .= la_TableRow($cells, 'row3');
  206. }
  207. $result .= la_TableBody($rows, '100%', 0, 'resp-table');
  208. } else {
  209. $result = __('No subscriptions yet');
  210. }
  211. } else {
  212. $result = __('No subscriptions yet');
  213. }
  214. return($result);
  215. }
  216. /**
  217. * Check user balance for subscribtion availability
  218. *
  219. * @return bool
  220. */
  221. protected function checkBalance() {
  222. $result = false;
  223. if (!empty($this->myLogin)) {
  224. if (isset($this->allUsers[$this->myLogin])) {
  225. $userBalance = $this->allUsers[$this->myLogin]['Cash'];
  226. if ($userBalance >= 0) {
  227. $result = true;
  228. }
  229. }
  230. }
  231. return ($result);
  232. }
  233. /**
  234. * Checks is user protected from his own stupidity?
  235. *
  236. * @param int $tariffId
  237. *
  238. * @return bool
  239. */
  240. protected function checkUserProtection($tariffId) {
  241. $tariffId = ubRouting::filters($tariffId, 'int');
  242. $result = true;
  243. if (isset($this->tariffsData[$tariffId])) {
  244. $tariffFee = $this->tariffsData[$tariffId]['fee'];
  245. $userData = $this->allUsers[$this->myLogin];
  246. $userBalance = $userData['Cash'];
  247. if ($userBalance < $tariffFee) {
  248. $result = false;
  249. }
  250. } else {
  251. $result = false;
  252. }
  253. return ($result);
  254. }
  255. /**
  256. * Checks is user subscribed for some tariff or not?
  257. *
  258. * @param int $tariffid
  259. *
  260. * @return bool
  261. */
  262. protected function isUserSubscribed($tariffid) {
  263. $result = false;
  264. if (!empty($this->subscriberData)) {
  265. if ($this->subscriberData['active']) {
  266. if ($this->subscriberData['tariffid'] == $tariffid OR $this->subscriberData['addtariffid'] == $tariffid) {
  267. $result = true;
  268. }
  269. }
  270. }
  271. return ($result);
  272. }
  273. /**
  274. * Checks have user filled mobile phone number on his profile
  275. *
  276. * @return bool
  277. */
  278. public function userHaveMobile() {
  279. $result = false;
  280. $phonesDb = new NyanORM('phones');
  281. $phonesDb->where('login', '=', $this->myLogin);
  282. $phonesDb->selectable('mobile');
  283. $mobile = $phonesDb->getAll();
  284. //TODO: maybe some checks of mobile number validity required
  285. if (isset($mobile[0])) {
  286. if (isset($mobile[0]['mobile'])) {
  287. if (!empty($mobile[0]['mobile'])) {
  288. $result = true;
  289. }
  290. }
  291. }
  292. return($result);
  293. }
  294. /**
  295. * Renders available subscriptions list
  296. *
  297. * @return string
  298. */
  299. public function renderSubscribeForm() {
  300. $result = '';
  301. $result .= la_tag('b') . __('Attention!') . la_tag('b', true) . ' ';
  302. $result .= __('When activated subscription account will be charged fee the equivalent value of the subscription.') . '!' . la_delimiter();
  303. if (!empty($this->tariffsData)) {
  304. foreach ($this->tariffsData as $serviceId => $tariff) {
  305. $subControl = '';
  306. $tariffFee = $tariff['fee'];
  307. $tariffInfo = la_tag('div', false, 'olltv-col') . la_tag('div', false, 'olltv-bl1');
  308. $tariffInfo .= la_tag('div', false, 'olltv-price');
  309. $tariffInfo .= la_tag('b', false, 's') . $tariffFee . la_tag('b', true, 's');
  310. $tariffInfo .= la_tag('sup', false) . $this->usConfig['currency'] . ' ' . la_tag('br') . ' ' . __('per month') . la_tag('sup', true);
  311. $tariffInfo .= la_tag('div', true, 'olltv-price');
  312. $tariffInfo .= la_tag('div', false, 'olltv-yellow s') . $tariff['name'] . la_tag('div', true, 'olltv-yellow s');
  313. $tariffInfo .= la_tag('br');
  314. if (!empty($tariff['chans'])) {
  315. $desc = $tariff['chans'];
  316. } else {
  317. $desc = '';
  318. }
  319. $descriptionLabel = $desc;
  320. $tariffInfo .= la_tag('div', false, 'olltv-list') . $descriptionLabel . la_tag('div', true, 'olltv-list');
  321. if ($this->checkBalance()) {
  322. if ($this->isUserSubscribed($tariff['id'])) {
  323. $subControl .= la_Link(self::URL_ME . '&unsubscribe=' . $tariff['id'], __('Unsubscribe'), false, 'olltv-button-u');
  324. $tariffInfo .= $subControl;
  325. } else {
  326. if ($this->checkUserProtection($tariff['id'])) {
  327. $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.');
  328. if ($tariff['id'] == @$this->subscriberData['tariffid']) {
  329. $controlLabel = __('Resume');
  330. } else {
  331. $controlLabel = __('Subscribe');
  332. }
  333. $subControl .= la_ConfirmDialog(self::URL_ME . '&subscribe=' . $tariff['id'], $controlLabel, $alertText, 'olltv-button-s', self::URL_ME);
  334. //hide case of resurrection via additional tariffs
  335. if (!$tariff['main'] AND @ !$this->subscriberData['active']) {
  336. $subControl = __('Additional services');
  337. }
  338. //hide another main tariffs subscription if user already have one
  339. if ($tariff['main'] AND @ $this->subscriberData['tariffid'] != $tariff['id'] AND @ $this->subscriberData['tariffid']) {
  340. $subControl = __('Already subscribed') . ' ' . __('on another tariff');
  341. }
  342. $tariffInfo .= $subControl;
  343. } else {
  344. $tariffInfo .= la_tag('div', false, 'olltv-list') . __('The amount of money in your account is not sufficient to process subscription') . la_tag('div', true, 'olltv-list');
  345. }
  346. }
  347. } else {
  348. $tariffInfo .= la_tag('div', false, 'olltv-list') . __('The amount of money in your account is not sufficient to process subscription') . la_tag('div', true, 'olltv-list');
  349. }
  350. $tariffInfo .= la_tag('div', true, 'olltv-bl1') . la_tag('div', true, 'olltv-col');
  351. $result .= $tariffInfo;
  352. }
  353. }
  354. return($result);
  355. }
  356. /**
  357. * Renders devices of some subscriber
  358. *
  359. * @return string
  360. */
  361. public function renderDevices() {
  362. $result = '';
  363. $subDevices = $this->getRemoteData('devdata=' . $this->myLogin);
  364. $devCount = 0;
  365. if (!empty($subDevices)) {
  366. $cells = la_TableCell(__('ID'));
  367. $cells .= la_TableCell(__('Date'));
  368. $cells .= la_TableCell(__('Serial'));
  369. $cells .= la_TableCell(__('MAC'));
  370. $cells .= la_TableCell(__('Code'));
  371. $rows = la_TableRow($cells, 'row1');
  372. foreach ($subDevices as $io => $eachDevice) {
  373. $cells = la_TableCell($eachDevice['ID']);
  374. $cells .= la_TableCell($eachDevice['date_added']);
  375. $cells .= la_TableCell($eachDevice['serial_number']);
  376. $cells .= la_TableCell($eachDevice['mac']);
  377. $cells .= la_TableCell($eachDevice['binding_code']);
  378. $rows .= la_TableRow($cells, 'row5');
  379. $devCount++;
  380. }
  381. $result .= la_TableBody($rows, '100%', 0, 'resp-table');
  382. $result .= la_delimiter();
  383. }
  384. if ($this->subscriberData['code']) {
  385. $containerStyle = 'style="border:1px solid; text-align:center; width:100%; display:block;"';
  386. $result .= la_tag('span', false, 'resp-table', $containerStyle);
  387. $result .= __('You can activate your new devices by logging on oll.tv with mobile') . ' ';
  388. $result .= la_tag('b') . $this->subscriberData['phone'] . la_tag('b', true) . ' ';
  389. $result .= __('and code') . ' ' . la_tag('b') . $this->subscriberData['code'] . la_tag('b', true);
  390. $result .= la_delimiter(0);
  391. $result .= la_tag('span', true);
  392. }
  393. return($result);
  394. }
  395. /**
  396. * Deactivates user service due deleting of tariff
  397. *
  398. * @param int $tariffId
  399. *
  400. * @return void
  401. */
  402. public function unsubscribe($tariffId) {
  403. $request = 'unsub=' . $tariffId . '&sublogin=' . $this->myLogin;
  404. $this->getRemoteData($request);
  405. }
  406. /**
  407. * Activates new service for user
  408. *
  409. * @param int $tariffId
  410. *
  411. * @return void
  412. */
  413. public function subscribe($tariffId) {
  414. $request = 'subserv=' . $tariffId . '&sublogin=' . $this->myLogin;
  415. $this->getRemoteData($request);
  416. }
  417. }