api.prostotv.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. <?php
  2. /**
  3. * ProstoTV users frontend basic class
  4. */
  5. class PTVInterface {
  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. * Maximum count of devices
  50. *
  51. * @var int
  52. */
  53. protected $maxDev = 3;
  54. /**
  55. * Maximum count of playlists
  56. *
  57. * @var int
  58. */
  59. protected $maxPl = 3;
  60. /**
  61. * Some predefined routes/URLs etc..
  62. */
  63. const URL_ME = '?module=omprostotv';
  64. const REQ_BASE = '&action=ptvui&';
  65. /**
  66. * Creates new instance
  67. *
  68. * @param string $userLogin
  69. */
  70. public function __construct($userLogin) {
  71. if (!empty($userLogin)) {
  72. $this->loadConfig();
  73. $this->setLogin($userLogin);
  74. $this->loadUsers();
  75. $this->subscriberData = $this->getSubscriberData();
  76. if (!empty($this->subscriberData)) {
  77. $this->subscriberId = $this->subscriberData['subscriberid'];
  78. $this->fullData = $this->getFullData();
  79. }
  80. $this->tariffsData = $this->getTariffsData();
  81. } else {
  82. die('ERROR:NO_USER_LOGIN');
  83. }
  84. }
  85. /**
  86. * Sets current instance user login
  87. *
  88. * @param string $userLogin
  89. *
  90. * @return void
  91. */
  92. protected function setLogin($userLogin) {
  93. $this->myLogin = $userLogin;
  94. }
  95. /**
  96. * Preloads userstats config to protected property
  97. *
  98. * @global array $us_config
  99. *
  100. * @return void
  101. */
  102. protected function loadConfig() {
  103. global $us_config;
  104. $this->usConfig = $us_config;
  105. }
  106. /**
  107. * Performs some RemoteAPI request and returns its results as array
  108. *
  109. * @param string $request
  110. *
  111. * @return array/bool on error
  112. */
  113. protected function getRemoteData($request) {
  114. $result = false;
  115. if (!empty($request)) {
  116. $requestUrl = self::REQ_BASE . $request;
  117. $rawReply = zbs_remoteApiRequest($requestUrl);
  118. if (!empty($rawReply)) {
  119. $result = json_decode($rawReply, true);
  120. }
  121. }
  122. return($result);
  123. }
  124. /**
  125. * Returns some subscriber data assigned to s
  126. *
  127. * @return array
  128. */
  129. protected function getSubscriberData() {
  130. $request = 'subdata=' . $this->myLogin;
  131. $result = $this->getRemoteData($request);
  132. return($result);
  133. }
  134. /**
  135. * Returns current subscriberId or void if user is unregistered yet.
  136. *
  137. * @return int/void
  138. */
  139. public function getSubscriberId() {
  140. return($this->subscriberId);
  141. }
  142. /**
  143. * Checks is user use service?
  144. *
  145. * @return bool
  146. */
  147. public function userUseService() {
  148. $result = false;
  149. if (!empty($this->subscriberData)) {
  150. if ($this->subscriberData['maintariff']) {
  151. $result = true;
  152. }
  153. }
  154. return($result);
  155. }
  156. /**
  157. * Returns available tariffs data
  158. *
  159. * @return array
  160. */
  161. protected function getTariffsData() {
  162. $request = 'tardata=true';
  163. $result = $this->getRemoteData($request);
  164. return($result);
  165. }
  166. /**
  167. * Returns full subscriber data
  168. *
  169. * @return array
  170. */
  171. protected function getFullData() {
  172. $request = 'fulldata=' . $this->myLogin;
  173. $result = $this->getRemoteData($request);
  174. return($result);
  175. }
  176. /**
  177. * Loads available users data from database
  178. *
  179. * @return void
  180. */
  181. protected function loadUsers() {
  182. $query = "SELECT * from `users` WHERE `login`='" . $this->myLogin . "'";
  183. $all = simple_queryall($query);
  184. if (!empty($all)) {
  185. foreach ($all as $io => $each) {
  186. $this->allUsers[$each['login']] = $each;
  187. }
  188. }
  189. }
  190. /**
  191. * Renders standard bool led
  192. *
  193. * @param mixed $state
  194. *
  195. * @return string
  196. */
  197. protected function webBoolLed($state) {
  198. $iconsPath = zbs_GetCurrentSkinPath($this->usConfig) . 'iconz/';
  199. $result = ($state) ? la_img($iconsPath . 'anread.gif') : la_img($iconsPath . 'anunread.gif');
  200. return($result);
  201. }
  202. /**
  203. * Renders current subscription details
  204. *
  205. * @return string
  206. */
  207. public function renderSubscriptionDetails() {
  208. $result = '';
  209. if (!empty($this->subscriberData)) {
  210. $mainTariff = @$this->tariffsData[$this->subscriberData['maintariff']];
  211. $cells = la_TableCell(__('Active'));
  212. $cells .= la_TableCell(__('Tariff'));
  213. $cells .= la_TableCell(__('Primary'));
  214. $cells .= la_TableCell(__('Fee'));
  215. $rows = la_TableRow($cells, 'row1');
  216. if (!empty($mainTariff)) {
  217. $cells = la_TableCell($this->webBoolLed($this->subscriberData['active']));
  218. $cells .= la_TableCell($mainTariff['name']);
  219. $cells .= la_TableCell($this->webBoolLed($mainTariff['main']));
  220. $cells .= la_TableCell($mainTariff['fee'] . ' ' . $this->usConfig['currency']);
  221. $rows .= la_TableRow($cells, 'row1');
  222. }
  223. $result .= la_TableBody($rows, '100%', 0, 'resp-table');
  224. } else {
  225. $result = __('No subscriptions yet');
  226. }
  227. return($result);
  228. }
  229. /**
  230. * Check user balance for subscribtion availability
  231. *
  232. * @return bool
  233. */
  234. protected function checkBalance() {
  235. $result = false;
  236. if (!empty($this->myLogin)) {
  237. if (isset($this->allUsers[$this->myLogin])) {
  238. $userBalance = $this->allUsers[$this->myLogin]['Cash'];
  239. if ($userBalance >= 0) {
  240. $result = true;
  241. }
  242. }
  243. }
  244. return ($result);
  245. }
  246. /**
  247. * Checks is user protected from his own stupidity?
  248. *
  249. * @param int $tariffId
  250. *
  251. * @return bool
  252. */
  253. protected function checkUserProtection($tariffId) {
  254. $tariffId = vf($tariffId, 3);
  255. $result = true;
  256. if (isset($this->tariffsData[$tariffId])) {
  257. $tariffFee = $this->tariffsData[$tariffId]['fee'];
  258. $userData = $this->allUsers[$this->myLogin];
  259. $userBalance = $userData['Cash'];
  260. if ($userBalance < $tariffFee) {
  261. $result = false;
  262. }
  263. } else {
  264. $result = false;
  265. }
  266. return ($result);
  267. }
  268. /**
  269. * Checks is user subscribed for some tariff or not?
  270. *
  271. * @param int $tariffid
  272. *
  273. * @return bool
  274. */
  275. protected function isUserSubscribed($tariffid) {
  276. $result = false;
  277. if (!empty($this->subscriberData)) {
  278. if ($this->subscriberData['active']) {
  279. if ($this->subscriberData['maintariff'] == $tariffid) {
  280. $result = true;
  281. }
  282. }
  283. }
  284. return ($result);
  285. }
  286. /**
  287. * Renders available subscriptions list
  288. *
  289. * @return string
  290. */
  291. public function renderSubscribeForm() {
  292. $result = '';
  293. $result = '';
  294. $result .= la_tag('b') . __('Attention!') . la_tag('b', true) . ' ';
  295. $result .= __('When activated subscription account will be charged fee the equivalent value of the subscription.') . la_delimiter();
  296. if (!empty($this->tariffsData)) {
  297. foreach ($this->tariffsData as $serviceId => $tariff) {
  298. $tariffFee = $tariff['fee'];
  299. $tariffInfo = la_tag('div', false, 'trinity-col') . la_tag('div', false, 'trinity-bl1');
  300. $tariffInfo .= la_tag('div', false, 'trinity-price');
  301. $tariffInfo .= la_tag('b', false, 's') . $tariffFee . la_tag('b', true, 's');
  302. $tariffInfo .= la_tag('sup', false) . $this->usConfig['currency'] . ' ' . la_tag('br') . ' ' . __('per month') . la_tag('sup', true);
  303. $tariffInfo .= la_tag('div', true, 'trinity-price');
  304. $tariffInfo .= la_tag('div', false, 'trinity-green s') . $tariff['name'] . la_tag('div', true, 'trinity-green s');
  305. $tariffInfo .= la_tag('br');
  306. if (!empty($tariff['chans'])) {
  307. $desc = $tariff['chans'];
  308. } else {
  309. $desc = '';
  310. }
  311. $descriptionLabel = $desc;
  312. $tariffInfo .= la_tag('div', false, 'trinity-list') . $descriptionLabel . la_tag('div', true, 'trinity-list');
  313. if ($this->checkBalance()) {
  314. if ($this->isUserSubscribed($tariff['serviceid'])) {
  315. $tariffInfo .= la_Link(self::URL_ME . '&unsubscribe=' . $tariff['serviceid'], __('Unsubscribe'), false, 'trinity-button-u');
  316. } else {
  317. if ($this->checkUserProtection($tariff['serviceid'])) {
  318. $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.');
  319. $tariffInfo .= la_ConfirmDialog(self::URL_ME . '&subscribe=' . $tariff['serviceid'], __('Subscribe'), $alertText, 'trinity-button-s', self::URL_ME);
  320. } else {
  321. $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');
  322. }
  323. }
  324. } else {
  325. $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');
  326. }
  327. $tariffInfo .= la_tag('div', true, 'trinity-bl1') . la_tag('div', true, 'trinity-col');
  328. $result .= $tariffInfo;
  329. }
  330. }
  331. return($result);
  332. }
  333. /**
  334. * Renders devices of some subscriber
  335. *
  336. * @return string
  337. */
  338. public function renderDevices() {
  339. $result = '';
  340. $subData = $this->fullData;
  341. $devCount = 0;
  342. if (!empty($subData['devices'])) {
  343. $subscriberId = $subData['id'];
  344. $userLogin = $this->myLogin;
  345. $cells = la_TableCell(__('Login'));
  346. $cells .= la_TableCell(__('Password'));
  347. $cells .= la_TableCell(__('Actions'));
  348. $rows = la_TableRow($cells, 'row1');
  349. foreach ($subData['devices'] as $io => $eachDevice) {
  350. $cells = la_TableCell($eachDevice['login']);
  351. $cells .= la_TableCell($eachDevice['password']);
  352. $devDelForm = la_ConfirmDialog(self::URL_ME . '&deldev=' . $eachDevice['id'], __('Delete'), __('Are you sure') . '?', '', self::URL_ME);
  353. $cells .= la_TableCell($devDelForm);
  354. $rows .= la_TableRow($cells, 'row5');
  355. $devCount++;
  356. }
  357. $result .= la_TableBody($rows, '100%', 0, 'resp-table');
  358. }
  359. if ($this->subscriberId) {
  360. if ($devCount < $this->maxDev) {
  361. $result .= la_Link(self::URL_ME . '&newdev=true', __('Assign device'), false, 'trinity-button');
  362. } else {
  363. $result .= __('Devices count limit is exceeded');
  364. }
  365. }
  366. return($result);
  367. }
  368. /**
  369. * Renders available user playlists
  370. *
  371. * @return string
  372. */
  373. public function renderPlaylists() {
  374. $result = '';
  375. $subData = $this->fullData;
  376. $plCount = 0;
  377. if (!empty($subData['playlists'])) {
  378. $cells = la_TableCell(__('Date'));
  379. $cells .= la_TableCell(__('Playlist'));
  380. $cells .= la_TableCell(__('Actions'));
  381. $rows = la_TableRow($cells, 'row1');
  382. foreach ($subData['playlists'] as $io => $eachPlaylist) {
  383. $cells = la_TableCell($eachPlaylist['created']);
  384. $cells .= la_TableCell(la_Link($eachPlaylist['url'], __('Download')));
  385. $plDevForm = la_ConfirmDialog(self::URL_ME . '&delpl=' . $eachPlaylist['id'], __('Delete'), __('Are you sure') . '?', '', self::URL_ME);
  386. $cells .= la_TableCell($plDevForm);
  387. $rows .= la_TableRow($cells, 'row3');
  388. $plCount++;
  389. }
  390. $result .= la_TableBody($rows, '100%', 0, 'resp-table');
  391. }
  392. if ($this->subscriberId) {
  393. if ($plCount < $this->maxPl) {
  394. $result .= la_Link(self::URL_ME . '&newpl=true', __('Add playlist'), false, 'trinity-button');
  395. } else {
  396. $result .= __('Devices count limit is exceeded');
  397. }
  398. }
  399. return($result);
  400. }
  401. /**
  402. * Creates new device for subscriber
  403. *
  404. * @return void
  405. */
  406. public function createNewDevice() {
  407. $request = 'newdev=' . $this->subscriberId;
  408. $this->getRemoteData($request);
  409. }
  410. /**
  411. * Deletes existing device
  412. *
  413. * @param string $devId
  414. *
  415. * $return void
  416. */
  417. public function deleteDevice($devId) {
  418. $request = 'deldev=' . $devId . '&subid=' . $this->subscriberId;
  419. $this->getRemoteData($request);
  420. }
  421. /**
  422. * Creates new playlist for user
  423. *
  424. * @return void
  425. */
  426. public function createPlaylist() {
  427. $request = 'newpl=' . $this->subscriberId;
  428. $this->getRemoteData($request);
  429. }
  430. /**
  431. * Deletes playlist from user
  432. *
  433. * @param string $playlistId
  434. *
  435. * @return void
  436. */
  437. public function deletePlaylist($playlistId) {
  438. $request = 'delpl=' . $playlistId . '&subid=' . $this->subscriberId;
  439. $this->getRemoteData($request);
  440. }
  441. /**
  442. * Deactivates user service due deleting of tariff
  443. *
  444. * @param int $tariffId
  445. *
  446. * @return void
  447. */
  448. public function unsubscribe($tariffId) {
  449. $request = 'unsub=' . $tariffId . '&subid=' . $this->subscriberId;
  450. $this->getRemoteData($request);
  451. }
  452. /**
  453. * Activates new service for user
  454. *
  455. * @param int $tariffId
  456. *
  457. * @return void
  458. */
  459. public function subscribe($tariffId) {
  460. $request = 'subserv=' . $tariffId . '&sublogin=' . $this->myLogin;
  461. $this->getRemoteData($request);
  462. }
  463. }