api.ytv.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. <?php
  2. /**
  3. * YouTV Ubilling abstraction layer
  4. *
  5. * https://documenter.getpostman.com/view/13165103/TVYAhgRP#4807007c-a159-4210-9f61-d51807aa36ee
  6. */
  7. class YTV {
  8. /**
  9. * Contains sytem alter.config as key=>value
  10. *
  11. * @var array
  12. */
  13. protected $altCfg = array();
  14. /**
  15. * Contains login preloaded from config
  16. *
  17. * @var string
  18. */
  19. protected $login = '';
  20. /**
  21. * Contains password preloaded from config
  22. *
  23. * @var string
  24. */
  25. protected $password = '';
  26. /**
  27. * Contains dealer id preloaded from config
  28. *
  29. * @var string
  30. */
  31. protected $dealerID = '';
  32. /**
  33. * ProstoTV low-level API abstraction layer
  34. *
  35. * @var object
  36. */
  37. protected $api = '';
  38. /**
  39. * Contains all available system users data as login=>userdata
  40. *
  41. * @var array
  42. */
  43. protected $allUserData = array();
  44. /**
  45. * Contains all subscribers data as login=>subscriberData
  46. *
  47. * @var array
  48. */
  49. protected $allSubscribers = array();
  50. /**
  51. * Contains all tariffs data as serviceid=>tariffData
  52. *
  53. * @var string
  54. */
  55. protected $allTariffs = array();
  56. /**
  57. * Subscribers database abstraction layer
  58. *
  59. * @var object
  60. */
  61. protected $subscribersDb = '';
  62. /**
  63. * Tariffs database abstraction layer
  64. *
  65. * @var object
  66. */
  67. protected $tariffsDb = '';
  68. /**
  69. * System message helper object placeholder
  70. *
  71. * @var object
  72. */
  73. protected $messages = '';
  74. /**
  75. * Predefined routes, options etc.
  76. */
  77. const OPTION_LOGIN = 'YOUTV_LOGIN';
  78. const OPTION_PASSWORD = 'YOUTV_PASSWORD';
  79. const OPTION_DEALER_ID = 'YOUTV_DEALER_ID';
  80. const TABLE_SUBSCRIBERS = 'youtv_subscribers';
  81. const TABLE_TARIFFS = 'youtv_tariffs';
  82. const UNDEF = 'undefined_';
  83. const NEW_WINDOW = 'TARGET="_BLANK"';
  84. const URL_ME = '?module=youtv';
  85. const URL_USERPROFILE = '?module=userprofile&username=';
  86. const ROUTE_SUBLIST = 'subscribers';
  87. const ROUTE_SUBAJ = 'ajaxlist';
  88. const ROUTE_SUBVIEW = 'showsubscriber';
  89. const ROUTE_TARIFFS = 'tariffs';
  90. const ROUTE_BUNDLES = 'bundles';
  91. const ROUTE_PLDEL = 'deleteplaylist';
  92. const ROUTE_SUBID = 'subscriberid';
  93. const ROUTE_DEVCREATE = 'createdevice';
  94. const ROUTE_DEVDEL = 'deletedevice';
  95. const ROUTE_SUBLOOKUP = 'username';
  96. const ROUTE_TARDEL = 'deletetariff';
  97. const PROUTE_SUBREG = 'registersubscriber';
  98. const PROUTE_CREATETARIFFID = 'newtariffserviceid';
  99. const PROUTE_CREATETARIFFMAIN = 'newtariffmainflag';
  100. const PROUTE_CREATETARIFFNAME = 'newtariffname';
  101. const PROUTE_CREATETARIFFCHANS = 'newtariffchans';
  102. const PROUTE_CREATETARIFFFEE = 'newtarifffee';
  103. const PROUTE_TARIFFEDITSUBID = 'changetariffsubscriberid';
  104. const PROUTE_SETMAINTARIFFID = 'changemaintariffserviceid';
  105. const PROUTE_SETADDTARIFFID = 'changeaddionaltariffs';
  106. /**
  107. * I keep my eyes low, looking for my rival
  108. * Eyes Low
  109. * Playing with the rifle
  110. * White dope
  111. * Feeling homicidal
  112. * Ride slow
  113. * Fucking up your spinal
  114. */
  115. public function __construct() {
  116. $this->initMessages();
  117. $this->loadConfig();
  118. $this->setOptions();
  119. $this->initApi();
  120. $this->loadUserData();
  121. $this->initSubscribersDb();
  122. $this->initTariffsDb();
  123. $this->loadSubscribers();
  124. $this->loadTariffs();
  125. }
  126. /**
  127. * Preloads required configs into protected props
  128. *
  129. * @global object $ubillingConfig
  130. *
  131. * @return void
  132. */
  133. protected function loadConfig() {
  134. global $ubillingConfig;
  135. $this->altCfg = $ubillingConfig->getAlter();
  136. }
  137. /**
  138. * Sets required properties via config options
  139. *
  140. * @return void
  141. */
  142. protected function setOptions() {
  143. $this->login = $this->altCfg[self::OPTION_LOGIN];
  144. $this->password = $this->altCfg[self::OPTION_PASSWORD];
  145. $this->dealerID = $this->altCfg[self::OPTION_DEALER_ID];
  146. }
  147. /**
  148. * Inits system message helper
  149. *
  150. * @return void
  151. */
  152. protected function initMessages() {
  153. $this->messages = new UbillingMessageHelper();
  154. }
  155. /**
  156. * Inits low-level API for further usage
  157. *
  158. * @return void
  159. */
  160. protected function initApi() {
  161. require_once ('api/libs/api.youtv.php');
  162. $this->api = new YouTV($this->login, $this->password, $this->dealerID);
  163. }
  164. /**
  165. * Inits subscribers database abstraction layer
  166. *
  167. * @return void
  168. */
  169. protected function initSubscribersDb() {
  170. $this->subscribersDb = new NyanORM(self::TABLE_SUBSCRIBERS);
  171. }
  172. /**
  173. * Inits tariffs database abstraction layer
  174. *
  175. * @return void
  176. */
  177. protected function initTariffsDb() {
  178. $this->tariffsDb = new NyanORM(self::TABLE_TARIFFS);
  179. }
  180. /**
  181. * Loads available subscribers from database
  182. *
  183. * @return void
  184. */
  185. protected function loadSubscribers() {
  186. $this->allSubscribers = $this->subscribersDb->getAll('login');
  187. }
  188. /**
  189. * Loads available tariffs from database
  190. *
  191. * @return void
  192. */
  193. protected function loadTariffs() {
  194. $this->allTariffs = $this->tariffsDb->getAll('serviceid');
  195. }
  196. /**
  197. * Loads available system users data
  198. *
  199. * @return void
  200. */
  201. protected function loadUserData() {
  202. $this->allUserData = zb_UserGetAllData();
  203. }
  204. /**
  205. * Registers a new user
  206. *
  207. * @param $userLogin
  208. * @return string
  209. * @throws Exception
  210. */
  211. public function userRegister($userLogin) {
  212. $result = '';
  213. $userLogin = ubRouting::filters($userLogin, 'mres');
  214. //user exists
  215. if (isset($this->allUserData[$userLogin])) {
  216. //email no empty
  217. if(!empty($this->allUserData[$userLogin]['email'])){
  218. //not registered yet
  219. if (!isset($this->allSubscribers[$userLogin])) {
  220. $userData = $this->allUserData[$userLogin];
  221. $newPassword = $userData['Password'];
  222. $userRealName = $userData['realname'];
  223. $userEmail = $userData['email'];
  224. $response = $this->api->createUser($userLogin, $userRealName, $userEmail, $newPassword);
  225. if(isset($response['data']['id'])){
  226. //log subscriber
  227. $newId = $response['data']['id'];
  228. $this->subscribersDb->data('date', curdatetime());
  229. $this->subscribersDb->data('subscriberid', $newId);
  230. $this->subscribersDb->data('login', $userLogin);
  231. $this->subscribersDb->data('active', '1');
  232. $this->subscribersDb->create();
  233. log_register('YouTV SUB REGISTER (' . $userLogin . ') AS [' . $newId . ']');
  234. }
  235. if(isset($response['errors']) and is_array($response['errors'])){
  236. foreach ($response['errors'] as $io => $each ){
  237. $result .= " <br>". $io.": " . $each[0] ;
  238. }
  239. }
  240. } else {
  241. $result .= __('Something went wrong') . ': ' . __('User duplicate') . ' - ' . $userLogin;
  242. log_register('YouTV SUB REGISTER (' . $userLogin . ') DUPLICATE FAIL');
  243. }
  244. } else {
  245. $result .= __('Something went wrong') . ': ' . __('Empty email') . ' - ' . $userLogin;
  246. log_register('YouTV SUB REGISTER (' . $userLogin . ') EMPTY EMAIL FAIL');
  247. }
  248. } else {
  249. $result .= __('Something went wrong') . ': ' . __('User not exist') . ' - ' . $userLogin;
  250. log_register('YouTV SUB REGISTER (' . $userLogin . ') NOTEXIST FAIL');
  251. }
  252. return($result);
  253. }
  254. /**
  255. * Returns subscriber remote data
  256. *
  257. * @param $userLogin
  258. * @return bool
  259. */
  260. public function getUserData($userLogin) {
  261. if (isset($this->allSubscribers[$userLogin])) {
  262. $subscriberId = $this->allSubscribers[$userLogin]['subscriberid'];
  263. $response = $this->api->getUser($subscriberId);
  264. if (isset($response['data'])) {
  265. return $response['data'];
  266. }
  267. }
  268. return false;
  269. }
  270. /**
  271. * Checks is some subscriberId associated with registered user?
  272. *
  273. * @param int $subscriberId
  274. *
  275. * @return bool
  276. */
  277. public function isValidSubscriber($subscriberId) {
  278. $subscriberId = ubRouting::filters($subscriberId, 'int');
  279. $result = false;
  280. if (!empty($this->allSubscribers)) {
  281. foreach ($this->allSubscribers as $io => $each) {
  282. if ($each['subscriberid'] == $subscriberId) {
  283. $result = true;
  284. }
  285. }
  286. }
  287. return($result);
  288. }
  289. /**
  290. * Returns existing subscriber user login by its ID
  291. *
  292. * @param int $subscriberId
  293. *
  294. * @return string/bool
  295. */
  296. public function getSubscriberLogin($subscriberId) {
  297. $result = false;
  298. if (!empty($this->allSubscribers)) {
  299. foreach ($this->allSubscribers as $io => $each) {
  300. if ($each['subscriberid'] == $subscriberId) {
  301. $result = $each['login'];
  302. }
  303. }
  304. }
  305. return($result);
  306. }
  307. /**
  308. * Returns subscripber ID by some of the users login
  309. *
  310. * @param string $userLogin
  311. *
  312. * @return int/bool
  313. */
  314. public function getSubscriberId($userLogin) {
  315. $result = false;
  316. if (isset($this->allSubscribers[$userLogin])) {
  317. $result = $this->allSubscribers[$userLogin]['subscriberid'];
  318. }
  319. return($result);
  320. }
  321. /**
  322. * Renders available subscribers JSON list
  323. *
  324. * @return void
  325. */
  326. public function renderSubsribersAjReply() {
  327. $json = new wf_JqDtHelper();
  328. if (!empty($this->allSubscribers)) {
  329. foreach ($this->allSubscribers as $userLogin => $eachSub) {
  330. if (isset($this->allUserData[$userLogin])) {
  331. $data[] = $eachSub['subscriberid'];
  332. $data[] = $eachSub['date'];
  333. $userAddress = @$this->allUserData[$userLogin]['fulladress'];
  334. $userRealName = @$this->allUserData[$userLogin]['realname'];
  335. $profileLink = wf_Link(self::URL_USERPROFILE . $userLogin, web_profile_icon());
  336. $subViewUrl = self::URL_ME . '&' . self::ROUTE_SUBVIEW . '=' . $userLogin;
  337. $actLinks = wf_Link($subViewUrl, web_edit_icon());
  338. $data[] = $profileLink . ' ' . $userAddress;
  339. $data[] = $userRealName;
  340. $data[] = $userLogin;
  341. $data[] = $actLinks;
  342. $json->addRow($data);
  343. unset($data);
  344. }
  345. }
  346. }
  347. $json->getJson();
  348. }
  349. /**
  350. * Renders existing subscribers list container
  351. *
  352. * @return string
  353. */
  354. public function renderSubscribersList() {
  355. $result = '';
  356. $columns = array('ID', 'Date', 'Address', 'Real Name', 'Login','Actions');
  357. $opts = '"order": [[ 1, "desc" ]]';
  358. $result .= wf_JqDtLoader($columns, self::URL_ME . '&' . self::ROUTE_SUBAJ . '=true', false, __('Subscriptions'), 50, $opts);
  359. return($result);
  360. }
  361. /**
  362. * Renders subscriber services
  363. *
  364. * @param array $subData
  365. *
  366. * @return string
  367. */
  368. protected function renderServices($subData) {
  369. $result = '';
  370. if (!empty($subData['subscriptions'])) {
  371. foreach ($subData['subscriptions'] as $io => $each) {
  372. // is active
  373. if(strtotime($each['expires_at']) > time()){
  374. if (isset($this->allTariffs[$each['price']])) {
  375. $tariffLabel = $this->allTariffs[$each['price']]['name'];
  376. } else {
  377. $tariffLabel = $each['id'];
  378. }
  379. $result .= $tariffLabel . ' ';
  380. }
  381. }
  382. } else {
  383. $result .= __('No tariff');
  384. }
  385. return($result);
  386. }
  387. /**
  388. * Renders basic subscriber profile
  389. *
  390. * @param string $userLogin
  391. *
  392. * @return string
  393. */
  394. public function renderSubscriber($userLogin) {
  395. $result = '';
  396. if (isset($this->allUserData[$userLogin])) {
  397. if (isset($this->allSubscribers[$userLogin])) {
  398. $subscriberId = $this->allSubscribers[$userLogin]['subscriberid'];
  399. $subData = $this->getUserData($userLogin);
  400. $userData = $this->allUserData[$userLogin];
  401. if ($subData != false) {
  402. $cells = wf_TableCell(__('Address'), '', 'row2');
  403. $cells .= wf_TableCell(wf_Link(self::URL_USERPROFILE . $userLogin, web_profile_icon() . ' ' . $userData['fulladress']));
  404. $rows = wf_TableRow($cells, 'row3');
  405. $cells = wf_TableCell(__('ID'), '30%', 'row2');
  406. $cells .= wf_TableCell($subData['id']);
  407. $rows .= wf_TableRow($cells, 'row3');
  408. $cells = wf_TableCell(__('Cash'), '', 'row2');
  409. $cells .= wf_TableCell($userData['Cash']);
  410. $rows .= wf_TableRow($cells, 'row3');
  411. $cells = wf_TableCell(__('Credit'), '', 'row2');
  412. $cells .= wf_TableCell($userData['Credit']);
  413. $rows .= wf_TableRow($cells, 'row3');
  414. $cells = wf_TableCell(__('IP'), '', 'row2');
  415. $cells .= wf_TableCell($userData['ip']);
  416. $rows .= wf_TableRow($cells, 'row3');
  417. $cells = wf_TableCell(__('Tariffs'), '', 'row2');
  418. $cells .= wf_TableCell($this->renderServices($subData));
  419. $rows .= wf_TableRow($cells, 'row3');
  420. $result .= wf_TableBody($rows, '100%', 0, '');
  421. //some user controls here
  422. $result .= wf_delimiter(0);
  423. $result .= $this->renderSubscriberControls($subscriberId, $subData);
  424. } else {
  425. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': ' . __('Empty reply received'), 'error');
  426. }
  427. } else {
  428. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': EX_SUBSCRIBER_NOT_EXISTS', 'error');
  429. }
  430. } else {
  431. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': ' . __('User not exists'), 'error');
  432. }
  433. return($result);
  434. }
  435. /**
  436. * Renders users tariff change form
  437. *
  438. * @param int $subscriberId
  439. *
  440. * @return string
  441. */
  442. protected function renderUserTariffEditForm($subscriberId) {
  443. $result = '';
  444. if (!empty($this->allTariffs)) {
  445. $mainTariffsArr = array();
  446. $additionalTariffsArr = array();
  447. $userLogin = $this->getSubscriberLogin($subscriberId);
  448. $currentMainTariff = $this->allSubscribers[$userLogin]['maintariff'];
  449. foreach ($this->allTariffs as $io => $each) {
  450. if ($each['main']) {
  451. $mainTariffsArr[$each['serviceid']] = $each['name'];
  452. } else {
  453. $additionalTariffsArr[$each['serviceid']] = $each['name'];
  454. }
  455. }
  456. if (!empty($mainTariffsArr)) {
  457. $inputs = wf_HiddenInput(self::PROUTE_TARIFFEDITSUBID, $subscriberId);
  458. $inputs .= wf_Selector(self::PROUTE_SETMAINTARIFFID, $mainTariffsArr, __('Primary') . ' ' . __('Tariff'), $currentMainTariff, true);
  459. $inputs .= wf_Submit(__('Save'));
  460. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  461. }
  462. } else {
  463. $result .= $this->messages->getStyledMessage(__('Tariffs') . ': ' . __('Not exists'), 'error');
  464. }
  465. return($result);
  466. }
  467. /**
  468. * Sets primary tariff for some subscriber
  469. *
  470. * @param int $subscriberId
  471. * @param int $tariffId
  472. *
  473. * @return void
  474. */
  475. public function setMainTariff($subscriberId, $tariffId) {
  476. $tariffId = ubRouting::filters($tariffId, 'int');
  477. $subscriberId = ubRouting::filters($subscriberId, 'int');
  478. if ($this->isValidSubscriber($subscriberId)) {
  479. $userLogin = $this->getSubscriberLogin($subscriberId);
  480. $currentTariff = $this->allSubscribers[$userLogin]['maintariff'];
  481. //deleting old service if required
  482. if ($currentTariff) {
  483. $this->api->blockUser($subscriberId);
  484. log_register('YouTV SUB (' . $userLogin . ') UNSET TARIFF [' . $currentTariff . '] AS [' . $subscriberId . ']');
  485. }
  486. //database update
  487. $this->subscribersDb->data('maintariff', $tariffId);
  488. $this->subscribersDb->data('active', 1);
  489. $this->subscribersDb->where('subscriberid', '=', $subscriberId);
  490. $this->subscribersDb->save();
  491. //push to service API
  492. $this->api->subscriptions($subscriberId, $tariffId);
  493. //put log record
  494. log_register('YouTV SUB (' . $userLogin . ') SET TARIFF [' . $tariffId . '] AS [' . $subscriberId . ']');
  495. }
  496. }
  497. /**
  498. * Returns some subscriber controls
  499. *
  500. * @param int $subscriberId
  501. * @param array $subData
  502. *
  503. * @return string
  504. */
  505. protected function renderSubscriberControls($subscriberId, $subData = array()) {
  506. $result = '';
  507. if ($this->isValidSubscriber($subscriberId)) {
  508. $result .= wf_modalAuto(wf_img('skins/icon_tariff.gif') . ' ' . __('Edit tariff'), __('Tariff'), $this->renderUserTariffEditForm($subscriberId), 'ubButton');
  509. if (!empty($subData)) {
  510. $userScheme = wf_tag('pre') . print_r($subData, true) . wf_tag('pre', true);
  511. $result .= wf_modal(wf_img('skins/brain.png') . ' ' . __('User inside'), __('User inside'), $userScheme, 'ubButton', '800', '600');
  512. }
  513. }
  514. return($result);
  515. }
  516. /**
  517. * Renders basic user registration form
  518. *
  519. * @return string
  520. */
  521. protected function renderUserRegisterForm() {
  522. $result = '';
  523. $inputs = wf_TextInput(self::PROUTE_SUBREG, __('Login'), '', false, 20);
  524. $inputs .= wf_Submit(__('Register'));
  525. $result .= wf_Form("", 'POST', $inputs, 'glamour');
  526. return($result);
  527. }
  528. /**
  529. * Renders primary module controls
  530. *
  531. * @return string
  532. */
  533. public function renderPanel() {
  534. $result = '';
  535. $result .= wf_modalAuto(web_icon_create() . ' ' . __('Users registration'), __('Users registration'), $this->renderUserRegisterForm(), 'ubButton');
  536. $result .= wf_Link(self::URL_ME . '&' . self::ROUTE_SUBLIST . '=true', wf_img('skins/ukv/users.png') . ' ' . __('Subscriptions'), false, 'ubButton') . ' ';
  537. $result .= wf_Link(self::URL_ME . '&' . self::ROUTE_TARIFFS . '=true', wf_img('skins/ukv/dollar.png') . ' ' . __('Tariffs'), false, 'ubButton') . ' ';
  538. return($result);
  539. }
  540. /**
  541. * Renders new tariff creation form
  542. *
  543. * @return string
  544. */
  545. protected function renderTariffCreateForm() {
  546. $result = '';
  547. $sup = wf_tag('sup') . '*' . wf_tag('sup', true);
  548. $inputs = wf_TextInput(self::PROUTE_CREATETARIFFID, __('Service ID') . $sup, '', true, 5, 'digits');
  549. $inputs .= wf_TextInput(self::PROUTE_CREATETARIFFNAME, __('Tariff name') . $sup, '', true, 20);
  550. $inputs .= wf_CheckInput(self::PROUTE_CREATETARIFFMAIN, __('Primary'), true, true);
  551. $inputs .= wf_TextInput(self::PROUTE_CREATETARIFFCHANS, __('Description'), '', true, 20);
  552. $inputs .= wf_TextInput(self::PROUTE_CREATETARIFFFEE, __('Fee') . $sup, '', true, 4, 'finance');
  553. $inputs .= wf_delimiter(0);
  554. $inputs .= wf_Submit(__('Create'));
  555. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  556. return($result);
  557. }
  558. /**
  559. * Creates new tariff in database
  560. *
  561. * @return void/string on error
  562. */
  563. public function createTariff() {
  564. $result = '';
  565. if (ubRouting::checkPost(array(self::PROUTE_CREATETARIFFNAME, self::PROUTE_CREATETARIFFID))) {
  566. $tariffId = ubRouting::post(self::PROUTE_CREATETARIFFID, 'int');
  567. $tariffName = ubRouting::post(self::PROUTE_CREATETARIFFNAME, 'mres');
  568. $tariffMain = (ubRouting::checkPost(self::PROUTE_CREATETARIFFMAIN)) ? 1 : 0;
  569. $tariffChans = ubRouting::post(self::PROUTE_CREATETARIFFCHANS, 'mres');
  570. $tariffFee = ubRouting::post(self::PROUTE_CREATETARIFFFEE);
  571. if ($tariffId) {
  572. if (!isset($this->allTariffs[$tariffId])) {
  573. if ($tariffName) {
  574. if (zb_checkMoney($tariffFee)) {
  575. $this->tariffsDb->data('serviceid', $tariffId);
  576. $this->tariffsDb->data('main', $tariffMain);
  577. $this->tariffsDb->data('name', $tariffName);
  578. $this->tariffsDb->data('chans', $tariffChans);
  579. $this->tariffsDb->data('fee', $tariffFee);
  580. $this->tariffsDb->create();
  581. log_register('PTV TARIFF CREATE `' . $tariffName . '` AS [' . $tariffId . '] FEE `' . $tariffFee . '`');
  582. } else {
  583. $result .= __('Wrong format of money sum');
  584. }
  585. } else {
  586. $result .= __('Wrong tariff name');
  587. }
  588. } else {
  589. $result .= __('Duplicate element ID');
  590. log_register('PTV TARIFF CREATE `' . $tariffName . '` AS [' . $tariffId . '] DUPLICATE FAIL');
  591. }
  592. } else {
  593. $result .= __('Wrong tariff id');
  594. }
  595. }
  596. return($result);
  597. }
  598. /**
  599. * Deletes existing tariff from database
  600. *
  601. * @param int $tariffId
  602. *
  603. * @return void/string on error
  604. */
  605. public function deleteTariff($tariffId) {
  606. $result = '';
  607. $tariffId = ubRouting::filters($tariffId, 'int');
  608. if (isset($this->allTariffs[$tariffId])) {
  609. if ($this->isTariffProtected($tariffId)) {
  610. $result = __('Tariff is used by some users');
  611. } else {
  612. $tariffData = $this->allTariffs[$tariffId];
  613. $tariffName = $tariffData['name'];
  614. $tariffFee = $tariffData['fee'];
  615. $this->tariffsDb->where('serviceid', '=', $tariffId);
  616. $this->tariffsDb->delete();
  617. log_register('PTV TARIFF DELETE `' . $tariffName . '` AS [' . $tariffId . '] FEE `' . $tariffFee . '`');
  618. }
  619. } else {
  620. $result .= __('Tariff not exists');
  621. }
  622. return($result);
  623. }
  624. /**
  625. * Checks is some tariff protected of usage by some user
  626. *
  627. * @param int $tariffId
  628. *
  629. * @return bool
  630. */
  631. protected function isTariffProtected($tariffId) {
  632. $result = false;
  633. if (!empty($this->allSubscribers)) {
  634. foreach ($this->allSubscribers as $io => $each) {
  635. if ($each['maintariff'] == $tariffId) {
  636. $result = true;
  637. }
  638. if (ispos($each['addtariffs'], $tariffId)) {
  639. $result = true;
  640. }
  641. }
  642. }
  643. return($result);
  644. }
  645. /**
  646. * Renders list of tariffs available for users
  647. *
  648. * @return string
  649. */
  650. public function renderTariffs() {
  651. $result = '';
  652. $result .= wf_modalAuto(web_add_icon() . ' ' . __('Create new tariff'), __('Create new tariff'), $this->renderTariffCreateForm(), 'ubButton');
  653. $result .= wf_delimiter();
  654. if (!empty($this->allTariffs)) {
  655. $cells = wf_TableCell(__('Service ID'));
  656. $cells .= wf_TableCell(__('Name'));
  657. $cells .= wf_TableCell(__('Primary'));
  658. $cells .= wf_TableCell(__('Description'));
  659. $cells .= wf_TableCell(__('Fee'));
  660. $cells .= wf_TableCell(__('Actions'));
  661. $rows = wf_TableRow($cells, 'row1');
  662. foreach ($this->allTariffs as $io => $each) {
  663. $cells = wf_TableCell($each['serviceid']);
  664. $cells .= wf_TableCell($each['name']);
  665. $cells .= wf_TableCell(web_bool_led($each['main']));
  666. $cells .= wf_TableCell($each['chans']);
  667. $cells .= wf_TableCell($each['fee']);
  668. $tariffsCancelUrl = self::URL_ME . '&' . self::ROUTE_TARIFFS . '=true';
  669. $tariffsDeleteUrl = self::URL_ME . '&' . self::ROUTE_TARDEL . '=' . $each['serviceid'];
  670. $tariffControls = wf_ConfirmDialog($tariffsDeleteUrl, web_delete_icon() . ' ' . __('Delete'), $this->messages->getDeleteAlert(), '', $tariffsCancelUrl);
  671. $cells .= wf_TableCell($tariffControls);
  672. $rows .= wf_TableRow($cells, 'row5');
  673. }
  674. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  675. } else {
  676. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'warning');
  677. }
  678. return($result);
  679. }
  680. /**
  681. * Charges some tariff fee from user account
  682. *
  683. * @param string $userLogin
  684. * @param int $tariffId
  685. *
  686. * @return void
  687. */
  688. public function chargeUserFee($userLogin, $tariffId) {
  689. if (isset($this->allUserData[$userLogin])) {
  690. $subscriberId = $this->getSubscriberId($userLogin);
  691. if (isset($this->allTariffs[$tariffId])) {
  692. $tariffFee = $this->allTariffs[$tariffId]['fee'];
  693. zb_CashAdd($userLogin, '-' . $tariffFee, 'add', 1, 'YOUTV:' . $tariffId);
  694. log_register('YouTV CHARGE TARIFF [' . $tariffId . '] FEE `' . $tariffFee . '` FOR (' . $userLogin . ') AS [' . $subscriberId . ']');
  695. } else {
  696. log_register('YouTV CHARGE FAIL NOTARIFF [' . $tariffId . '] FOR (' . $userLogin . ') AS [' . $subscriberId . ']');
  697. }
  698. } else {
  699. log_register('YouTV CHARGE FAIL NOUSER (' . $userLogin . ')');
  700. }
  701. }
  702. /**
  703. * Performs fee processing of all registered subscribers
  704. *
  705. * @return void
  706. */
  707. public function feeProcessing() {
  708. if (!empty($this->allSubscribers)) {
  709. foreach ($this->allSubscribers as $io => $eachSub) {
  710. $userLogin = $eachSub['login'];
  711. if (isset($this->allUserData[$userLogin])) {
  712. //user subscription is active now
  713. if ($eachSub['active']) {
  714. if (($this->allUserData[$userLogin]['Passive'] == 0) and ($this->allUserData[$userLogin]['Cash'] >= '-' . $this->allUserData[$userLogin]['Credit'])) {
  715. // можно снимать деньги
  716. $this->chargeUserFee($eachSub['login'], $eachSub['maintariff']);
  717. } else {
  718. // бомж, блокируем активную подписку
  719. $this->usUnsubscribe($eachSub['subscriberid'], $eachSub['maintariff']);
  720. }
  721. }
  722. } else {
  723. log_register('YouTV CHARGE (' . $userLogin . ') AS [' . $eachSub . '] FAIL MISS');
  724. }
  725. }
  726. }
  727. }
  728. /**
  729. * Renders JSON reply for some userstats frontend requests
  730. *
  731. * @param array $reply
  732. *
  733. * @return void
  734. */
  735. protected function jsonRenderReply($reply) {
  736. $reply = json_encode($reply);
  737. die($reply);
  738. }
  739. /**
  740. * Renders user subscription data for some login
  741. *
  742. * @param string $userLogin
  743. *
  744. * @return void
  745. */
  746. public function usReplyUserData($userLogin) {
  747. $reply = array();
  748. if (isset($this->allSubscribers[$userLogin])) {
  749. $reply = $this->allSubscribers[$userLogin];
  750. }
  751. $this->jsonRenderReply($reply);
  752. }
  753. /**
  754. * Renders subscriber full data
  755. *
  756. * @param string $userLogin
  757. *
  758. * @return void
  759. */
  760. public function usReplyUserFullData($userLogin) {
  761. $reply = $this->getUserData($userLogin);
  762. $this->jsonRenderReply($reply);
  763. }
  764. /**
  765. * Renders available tariffs list
  766. *
  767. * @return void
  768. */
  769. public function usReplyTariffs() {
  770. $reply = array();
  771. if (!empty($this->allTariffs)) {
  772. foreach ($this->allTariffs as $io => $each) {
  773. if ($each['main']) {
  774. $reply[$io] = $each;
  775. }
  776. }
  777. }
  778. $this->jsonRenderReply($reply);
  779. }
  780. /**
  781. * Just deactivates service for user account
  782. *
  783. * @param int $subscriberId
  784. * @param int $tariffId
  785. *
  786. * @return void
  787. */
  788. public function usUnsubscribe($subscriberId, $tariffId) {
  789. $reply = array();
  790. $userLogin = $this->getSubscriberLogin($subscriberId);
  791. $this->api->blockUser($subscriberId);
  792. $this->subscribersDb->data('active', '0');
  793. $this->subscribersDb->data('maintariff', '0');
  794. $this->subscribersDb->where('subscriberid', '=', $subscriberId);
  795. $this->subscribersDb->save();
  796. log_register('YouTV SUB (' . $userLogin . ') UNSET TARIFF [' . $tariffId . '] AS [' . $subscriberId . ']');
  797. $this->jsonRenderReply($reply);
  798. }
  799. /**
  800. * Subscribes user to some service
  801. *
  802. * @param string $subscriberId
  803. * @param int $tariffId
  804. *
  805. * @return void
  806. */
  807. public function usSubscribe($userLogin, $tariffId) {
  808. if (isset($this->allTariffs[$tariffId])) {
  809. //may be thats new user?
  810. $subscriberId = $this->getSubscriberId($userLogin);
  811. if (!$this->isValidSubscriber($subscriberId)) {
  812. $this->userRegister($userLogin);
  813. //update subscriberId
  814. $this->loadSubscribers();
  815. $subscriberId = $this->getSubscriberId($userLogin);
  816. }
  817. //just switch tariff
  818. if ($this->isValidSubscriber($subscriberId)) {
  819. $this->setMainTariff($subscriberId, $tariffId);
  820. }
  821. //charge tariff fee after
  822. $this->chargeUserFee($userLogin, $tariffId);
  823. }
  824. }
  825. }