api.omegatv.php 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668
  1. <?php
  2. /**
  3. * OmegaTV OTT service implementation
  4. */
  5. class OmegaTV {
  6. /**
  7. * HlsTV object placeholder for further usage
  8. *
  9. * @var object
  10. */
  11. protected $hls = '';
  12. /**
  13. * Contains system alter config as key=>value
  14. *
  15. * @var array
  16. */
  17. protected $altCfg = array();
  18. /**
  19. * Contains all of available omega tariffs as id=>data
  20. *
  21. * @var array
  22. */
  23. protected $allTariffs = array();
  24. /**
  25. * Contains all tariff names as tariffid=>name
  26. *
  27. * @var array
  28. */
  29. protected $tariffNames = array();
  30. /**
  31. * Contains available user profiles as customerid=>data
  32. *
  33. * @var array
  34. */
  35. protected $allUsers = array();
  36. /**
  37. * Contains available users data as login=>data
  38. *
  39. * @var array
  40. */
  41. protected $allUserData = array();
  42. /**
  43. * Contains local queue for deffered actions
  44. *
  45. * @var array
  46. */
  47. protected $queue = array();
  48. /**
  49. * System message helper object placeholder
  50. *
  51. * @var object
  52. */
  53. protected $messages = '';
  54. /**
  55. * Contains default channel icon size
  56. *
  57. * @var int
  58. */
  59. protected $chanIconSize = 32;
  60. /**
  61. * Is tariffs unsub delayed in queue or not flag
  62. *
  63. * @var bool
  64. */
  65. protected $unsubDelay = false;
  66. /**
  67. * Contains array of currently suspended users without base tariff
  68. *
  69. * @var array
  70. */
  71. protected $suspended = array();
  72. /**
  73. * Contains bundled internet tariffs names as name=>someshit. No fee charging for them. Lol.
  74. *
  75. * @var array
  76. */
  77. protected $bundledTariffs = array();
  78. /**
  79. * Basic module path
  80. */
  81. const URL_ME = '?module=omegatv';
  82. /**
  83. * Default user profile viewing URL
  84. */
  85. const URL_PROFILE = '?module=userprofile&username=';
  86. /**
  87. * Default subscriber profile viewing URL
  88. */
  89. const URL_SUBSCRIBER = '?module=omegatv&customerprofile=';
  90. /**
  91. * Creates new OmegaTV instance
  92. */
  93. public function __construct() {
  94. $this->initHls();
  95. $this->initMessages();
  96. $this->loadAlter();
  97. $this->loadTariffs();
  98. $this->loadUserData();
  99. $this->loadUserProfiles();
  100. $this->loadQueue();
  101. $this->loadSuspended();
  102. $this->loadBundleTariffs();
  103. }
  104. /**
  105. * Loads system alter config into protected property.
  106. *
  107. * @global object $ubillingConfig
  108. *
  109. * @return void
  110. */
  111. protected function loadAlter() {
  112. global $ubillingConfig;
  113. $this->altCfg = $ubillingConfig->getAlter();
  114. }
  115. /**
  116. * Loads bundle tariffs list from config option into protected prop.
  117. *
  118. * @return void
  119. */
  120. protected function loadBundleTariffs() {
  121. //A-A-A-A-A-A-AA!!!!!111 Skybetik eto pizdets!
  122. if (isset($this->altCfg['OMEGATV_TARIFFSBUNDLE'])) {
  123. $bundleTariffsList = array();
  124. $bundleTariffsTmp = explode(',', $this->altCfg['OMEGATV_TARIFFSBUNDLE']);
  125. if (!empty($bundleTariffsTmp)) {
  126. foreach ($bundleTariffsTmp as $optionIndex => $eachBundleTariffName) {
  127. $cleanTariffName = trim($eachBundleTariffName);
  128. $this->bundledTariffs[$cleanTariffName] = $eachBundleTariffName;
  129. }
  130. }
  131. }
  132. }
  133. /**
  134. * Inits HLS object for further usage
  135. *
  136. * @return void
  137. */
  138. protected function initHls() {
  139. $this->hls = new HlsTV();
  140. }
  141. /**
  142. * Inits system message helper
  143. *
  144. * @return void
  145. */
  146. protected function initMessages() {
  147. $this->messages = new UbillingMessageHelper();
  148. }
  149. /**
  150. * Loads existing tariffs from database
  151. *
  152. * @return void
  153. */
  154. protected function loadTariffs() {
  155. $query = "SELECT * from `om_tariffs`";
  156. $all = simple_queryall($query);
  157. if (!empty($all)) {
  158. foreach ($all as $io => $each) {
  159. $this->allTariffs[$each['id']] = $each;
  160. $this->tariffNames[$each['tariffid']] = $each['tariffname'];
  161. }
  162. }
  163. }
  164. /**
  165. * Loads existing queue records for some actions
  166. *
  167. * @return void
  168. */
  169. protected function loadQueue() {
  170. $query = "SELECT * from `om_queue`";
  171. $all = simple_queryall($query);
  172. if (!empty($all)) {
  173. foreach ($all as $io => $each) {
  174. $this->queue[$each['id']] = $each;
  175. }
  176. }
  177. }
  178. /**
  179. * Loads existing suspended users
  180. *
  181. * @return void
  182. */
  183. protected function loadSuspended() {
  184. $query = "SELECT * from `om_suspend`";
  185. $all = simple_queryall($query);
  186. if (!empty($all)) {
  187. foreach ($all as $io => $each) {
  188. $this->suspended[$each['login']] = $each['id'];
  189. }
  190. }
  191. }
  192. /**
  193. * Loads existing users profiles
  194. *
  195. * @return void
  196. */
  197. protected function loadUserProfiles() {
  198. $query = "SELECT * from `om_users`";
  199. $all = simple_queryall($query);
  200. if (!empty($all)) {
  201. foreach ($all as $io => $each) {
  202. $this->allUsers[$each['customerid']] = $each;
  203. }
  204. }
  205. }
  206. /**
  207. * Loads internet users data into protected property for further usage
  208. *
  209. * @return void
  210. */
  211. protected function loadUserData() {
  212. $this->allUserData = zb_UserGetAllData();
  213. }
  214. /**
  215. * Renders available tariffs list
  216. *
  217. * @param string $list - tariff list to render base/bundle/promo
  218. * @param bool $withIds - render tariff IDs or not?
  219. * @param bool $withChannels - render channels preview or not?
  220. *
  221. * @return string
  222. */
  223. public function renderTariffsRemote($list, $withIds = true, $withChannels = true) {
  224. $result = '';
  225. switch ($list) {
  226. case 'base':
  227. $allTariffs = $this->hls->getTariffsBase();
  228. break;
  229. case 'bundle':
  230. $allTariffs = $this->hls->getTariffsBundle();
  231. break;
  232. case 'promo':
  233. $allTariffs = $this->hls->getTariffsPromo();
  234. break;
  235. }
  236. if (!empty($allTariffs)) {
  237. if (isset($allTariffs['result'])) {
  238. if ($list != 'promo') {
  239. $allTariffs = $allTariffs['result'];
  240. } else {
  241. $allTariffs = $allTariffs['result']['promo_limited'];
  242. }
  243. if (!empty($allTariffs)) {
  244. foreach ($allTariffs as $io => $each) {
  245. $tariffTitle = ($withIds) ? $each['tariff_id'] . ': ' . $each['tariff_name'] : $each['tariff_name'];
  246. $result .= wf_tag('h3') . $tariffTitle . wf_tag('h3', true);
  247. if ($withChannels) {
  248. if (!empty($each['hls_channels'])) {
  249. $cells = wf_TableCell('');
  250. $cells .= wf_TableCell(__('Channels'));
  251. $cells .= wf_TableCell(__('Category'));
  252. $rows = wf_TableRow($cells, 'row1');
  253. foreach ($each['hls_channels'] as $chanId => $eachChannel) {
  254. $cells = wf_TableCell(wf_img_sized($eachChannel['logo'], $eachChannel['name'], $this->chanIconSize), $this->chanIconSize + 10);
  255. $cells .= wf_TableCell($eachChannel['name']);
  256. $cells .= wf_TableCell($eachChannel['ganre']);
  257. $rows .= wf_TableRow($cells, 'row3');
  258. }
  259. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  260. }
  261. }
  262. }
  263. }
  264. }
  265. }
  266. return ($result);
  267. }
  268. /**
  269. * Trys to render human-readable tariff name
  270. *
  271. * @param int $tariffId
  272. *
  273. * @return string
  274. */
  275. protected function getTariffName($tariffId) {
  276. $result = '';
  277. if (isset($this->tariffNames[$tariffId])) {
  278. $result .= $this->tariffNames[$tariffId];
  279. } else {
  280. $result .= $tariffId;
  281. }
  282. return ($result);
  283. }
  284. /**
  285. * Returns current user devices info as JSON
  286. *
  287. * @param string $userLogin
  288. *
  289. * @return string
  290. */
  291. public function getUserDevicesData($userLogin) {
  292. $result = '';
  293. $customerId = $this->getLocalCustomerId($userLogin);
  294. if (!empty($customerId)) {
  295. $userInfo = $this->hls->getUserInfo($customerId);
  296. if (isset($userInfo['result'])) {
  297. $userInfo = $userInfo['result'];
  298. if (isset($userInfo['devices'])) {
  299. $result .= json_encode($userInfo['devices']);
  300. }
  301. }
  302. }
  303. return ($result);
  304. }
  305. /**
  306. * Returns current user playlists info as JSON
  307. *
  308. * @param string $userLogin
  309. *
  310. * @return string
  311. */
  312. public function getUserPlaylistsData($userLogin) {
  313. $result = '';
  314. $customerId = $this->getLocalCustomerId($userLogin);
  315. if (!empty($customerId)) {
  316. $userInfo = $this->hls->getUserInfo($customerId);
  317. if (isset($userInfo['result'])) {
  318. $userInfo = $userInfo['result'];
  319. if (isset($userInfo['playlists'])) {
  320. $result .= json_encode($userInfo['playlists']);
  321. }
  322. }
  323. }
  324. return ($result);
  325. }
  326. /**
  327. * Deletes some device from user
  328. *
  329. * @param string $userLogin
  330. * @param string $uniq
  331. *
  332. * @return void
  333. */
  334. public function deleteUserDevice($userLogin, $uniq) {
  335. $customerId = $this->getLocalCustomerId($userLogin);
  336. $uniq = trim($uniq);
  337. if (!empty($customerId)) {
  338. $userInfo = $this->hls->getUserInfo($customerId);
  339. if (isset($userInfo['result'])) {
  340. //checking for user device ownership
  341. $userInfo = $userInfo['result'];
  342. if (isset($userInfo['devices'])) {
  343. if (!empty($userInfo['devices'])) {
  344. foreach ($userInfo['devices'] as $io => $each) {
  345. if ($each['uniq'] == $uniq) {
  346. $this->deleteDevice($customerId, $uniq);
  347. log_register('OMEGATV DEVICE DELETE `' . $uniq . '` FOR (' . $userLogin . ') AS [' . $customerId . ']');
  348. }
  349. }
  350. }
  351. }
  352. }
  353. }
  354. }
  355. /**
  356. * Deletes some playlists assigned from user
  357. *
  358. * @param string $userLogin
  359. * @param string $uniq
  360. *
  361. * @return void
  362. */
  363. public function deleteUserPlaylist($userLogin, $uniq) {
  364. $customerId = $this->getLocalCustomerId($userLogin);
  365. $uniq = trim($uniq);
  366. if (!empty($customerId)) {
  367. $userInfo = $this->hls->getUserInfo($customerId);
  368. if (isset($userInfo['result'])) {
  369. //checking for user playlist ownership
  370. $userInfo = $userInfo['result'];
  371. if (isset($userInfo['playlists'])) {
  372. if (!empty($userInfo['playlists'])) {
  373. foreach ($userInfo['playlists'] as $io => $each) {
  374. if ($each['uniq'] == $uniq) {
  375. $this->deletePlaylist($customerId, $uniq);
  376. log_register('OMEGATV PLAYLIST DELETE `' . $uniq . '` FOR (' . $userLogin . ') AS [' . $customerId . ']');
  377. }
  378. }
  379. }
  380. }
  381. }
  382. }
  383. }
  384. /**
  385. * Creates new playlist and assigns it to existing user
  386. *
  387. * @param string $userLogin
  388. *
  389. * @return void/string on error
  390. */
  391. public function assignUserPlaylist($userLogin) {
  392. $result = '';
  393. $customerId = $this->getLocalCustomerId($userLogin);
  394. if (!empty($customerId)) {
  395. if (isset($this->allUsers[$customerId])) {
  396. $assignResult = $this->hls->addPlayList($customerId);
  397. if (isset($assignResult['error'])) {
  398. $result .= __('Strange exeption') . ': ' . $assignResult['error']['code'] . ' - ' . $assignResult['error']['msg'];
  399. } else {
  400. $uniq = $assignResult['result']['uniq'];
  401. $userLogin = $this->getLocalCustomerLogin($customerId);
  402. log_register('OMEGATV PLAYLIST ASSIGN `' . $uniq . '` FOR (' . $userLogin . ') AS [' . $customerId . ']');
  403. }
  404. } else {
  405. $result .= __('Something went wrong') . ': ' . __('User not exists');
  406. }
  407. }
  408. return ($result);
  409. }
  410. /**
  411. * Renders form to manual tariff changing
  412. *
  413. * @param int $customerId
  414. *
  415. * @return string
  416. */
  417. protected function renderManualTariffForm($customerId) {
  418. $customerId = vf($customerId, 3);
  419. $result = '';
  420. $userData = @$this->allUsers[$customerId];
  421. $currentBundleTariffs = $this->extractBundle($customerId);
  422. if (!empty($this->allTariffs)) {
  423. $inputs = '';
  424. $baseTariffs = array();
  425. $bundleTariffs = array('' => '-');
  426. foreach ($this->allTariffs as $io => $each) {
  427. if ($each['type'] == 'base') {
  428. $baseTariffs[$each['tariffid']] = $each['tariffname'] . ' (' . __($each['type']) . ')';
  429. }
  430. if ($each['type'] == 'bundle') {
  431. if (!isset($currentBundleTariffs[$each['tariffid']])) {
  432. $bundleTariffs[$each['tariffid']] = $each['tariffname'] . ' (' . __($each['type']) . ')';
  433. }
  434. }
  435. }
  436. $inputs .= wf_Selector('changebasetariff', $baseTariffs, __('Base tariff'), $userData['basetariffid'], true);
  437. $inputs .= wf_Selector('addbundletariff', $bundleTariffs, __('Add bundle tariff'), '', true);
  438. $inputs .= wf_CheckInput('deleteallbundle', __('Delete all bundle tariffs'), true, false);
  439. $inputs .= wf_tag('br');
  440. $inputs .= wf_Submit(__('Save'));
  441. $result .= wf_Form(self::URL_SUBSCRIBER . $customerId, 'POST', $inputs, 'glamour');
  442. }
  443. return ($result);
  444. }
  445. /**
  446. * Performs editing of user tariffs
  447. *
  448. * @param int $customerId
  449. *
  450. * @return void
  451. */
  452. public function changeUserTariffs($customerId) {
  453. $customerId = vf($customerId, 3);
  454. if (wf_CheckPost(array('changebasetariff'))) {
  455. $newBase = vf($_POST['changebasetariff'], 3);
  456. if (isset($this->allUsers[$customerId])) {
  457. $userLogin = $this->allUsers[$customerId]['login'];
  458. $userCurrentBase = $this->allUsers[$customerId]['basetariffid'];
  459. $userCurrentBundle = $this->extractBundle($customerId);
  460. if ($userCurrentBase != $newBase) {
  461. $newTariffs = array('base' => $newBase, 'bundle' => $userCurrentBundle);
  462. $this->hls->setUserTariff($customerId, $newTariffs);
  463. simple_update_field('om_users', 'basetariffid', $newBase, "WHERE `customerid`='" . $customerId . "'");
  464. log_register('OMEGATV SET TARIFF [' . $newBase . '] BASE FOR (' . $userLogin . ') AS [' . $customerId . ']');
  465. }
  466. if (wf_CheckPost(array('addbundletariff'))) {
  467. $newBundle = vf($_POST['addbundletariff'], 3);
  468. if (!isset($userCurrentBundle[$newBundle])) {
  469. $userCurrentBundle[$newBundle] = $newBundle;
  470. $newTariffs = array('base' => $newBase, 'bundle' => $userCurrentBundle);
  471. $this->hls->setUserTariff($customerId, $newTariffs);
  472. $saveBundle = trim(serialize($userCurrentBundle));
  473. simple_update_field('om_users', 'bundletariffs', $saveBundle, "WHERE `customerid`='" . $customerId . "'");
  474. log_register('OMEGATV SET TARIFF [' . $newBundle . '] BUNDLE FOR (' . $userLogin . ') AS [' . $customerId . ']');
  475. }
  476. }
  477. if (wf_CheckPost(array('deleteallbundle'))) {
  478. $userCurrentBundle = array();
  479. $newTariffs = array('base' => $newBase, 'bundle' => $userCurrentBundle);
  480. $this->hls->setUserTariff($customerId, $newTariffs);
  481. $saveBundle = trim(serialize($userCurrentBundle));
  482. simple_update_field('om_users', 'bundletariffs', $saveBundle, "WHERE `customerid`='" . $customerId . "'");
  483. log_register('OMEGATV FLUSH BUNDLE FOR (' . $userLogin . ') AS [' . $customerId . ']');
  484. }
  485. }
  486. }
  487. }
  488. /**
  489. * Renders manual device assign form
  490. *
  491. * @return string
  492. */
  493. protected function renderDeviceAddForm($customerId) {
  494. $result = '';
  495. $inputs = wf_HiddenInput('manualassigndevice', 'true');
  496. $inputs .= wf_HiddenInput('manualassigndevicecustomerid', $customerId);
  497. $inputs .= wf_TextInput('manualassigndeviceuniq', __('Uniq'), '', true, 20, 'alphanumeric');
  498. $inputs .= wf_CheckInput('manualassignnewplaylist', __('Just create new playlist'), true, false);
  499. $inputs .= wf_Submit(__('Assign'));
  500. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  501. return ($result);
  502. }
  503. /**
  504. * Assigns some device uniq to some customer
  505. *
  506. * @return void/string on error
  507. */
  508. public function assignDeviceManual() {
  509. $result = '';
  510. if (wf_CheckPost(array('manualassigndevice', 'manualassigndevicecustomerid', 'manualassigndeviceuniq'))) {
  511. $customerId = vf($_POST['manualassigndevicecustomerid'], 3); //int
  512. $uniq = vf($_POST['manualassigndeviceuniq']); //alphanumeric
  513. if (isset($this->allUsers[$customerId])) {
  514. $assignResult = $this->hls->addDevice($customerId, $uniq);
  515. if (isset($assignResult['error'])) {
  516. $result .= __('Strange exeption') . ': ' . $assignResult['error']['code'] . ' - ' . $assignResult['error']['msg'];
  517. } else {
  518. $userLogin = $this->getLocalCustomerLogin($customerId);
  519. log_register('OMEGATV DEVICE ASSIGN `' . $uniq . '` FOR (' . $userLogin . ') AS [' . $customerId . ']');
  520. }
  521. } else {
  522. $result .= __('Something went wrong') . ': ' . __('User not exists');
  523. }
  524. }
  525. return ($result);
  526. }
  527. /**
  528. * Assigns new playlist for some existing user
  529. *
  530. * @return void/string on error
  531. */
  532. public function assignPlaylistManual() {
  533. $result = '';
  534. if (wf_CheckPost(array('manualassigndevicecustomerid', 'manualassigndevice', 'manualassignnewplaylist'))) {
  535. $customerId = vf($_POST['manualassigndevicecustomerid'], 3); //int
  536. if (isset($this->allUsers[$customerId])) {
  537. $assignResult = $this->hls->addPlayList($customerId);
  538. if (isset($assignResult['error'])) {
  539. $result .= __('Strange exeption') . ': ' . $assignResult['error']['code'] . ' - ' . $assignResult['error']['msg'];
  540. } else {
  541. $uniq = $assignResult['result']['uniq'];
  542. $userLogin = $this->getLocalCustomerLogin($customerId);
  543. log_register('OMEGATV PLAYLIST ASSIGN `' . $uniq . '` FOR (' . $userLogin . ') AS [' . $customerId . ']');
  544. }
  545. } else {
  546. $result .= __('Something went wrong') . ': ' . __('User not exists');
  547. }
  548. }
  549. return ($result);
  550. }
  551. /**
  552. * Renders profile controls
  553. *
  554. * @return string
  555. */
  556. protected function renderProfileControls($customerId) {
  557. $customerId = vf($customerId, 3);
  558. $result = wf_tag('br');
  559. $result .= wf_Link(self::URL_ME . '&customerprofile=' . $customerId . '&blockuser=true', web_bool_led(0) . ' ' . __('Block user'), false, 'ubButton');
  560. $result .= wf_Link(self::URL_ME . '&customerprofile=' . $customerId . '&unblockuser=true', web_bool_led(1) . ' ' . __('Unblock user'), false, 'ubButton');
  561. $result .= wf_modalAuto(web_edit_icon() . ' ' . __('Edit tariff'), __('Edit tariff'), $this->renderManualTariffForm($customerId), 'ubButton');
  562. $result .= wf_modalAuto(wf_img('skins/switch_models.png') . ' ' . __('Assign device'), __('Assign device'), $this->renderDeviceAddForm($customerId), 'ubButton');
  563. return ($result);
  564. }
  565. /**
  566. * Sets user local and remote profile as active or not
  567. *
  568. * @param int $customerId
  569. * @param bool $state
  570. *
  571. * @return void
  572. */
  573. public function setCustomerActive($customerId, $state) {
  574. $customerId = vf($customerId, 3);
  575. if (isset($this->allUsers[$customerId])) {
  576. $userLogin = $this->allUsers[$customerId]['login'];
  577. $where = "WHERE `customerid`='" . $customerId . "'";
  578. if ($state) {
  579. $this->hls->setUserActivate($customerId);
  580. simple_update_field('om_users', 'active', '1', $where);
  581. log_register('OMEGATV UNBLOCK USER (' . $userLogin . ') AS [' . $customerId . ']');
  582. $this->suspendUser($userLogin, false);
  583. } else {
  584. $this->hls->setUserBlock($customerId);
  585. simple_update_field('om_users', 'active', '0', $where);
  586. log_register('OMEGATV BLOCK USER (' . $userLogin . ') AS [' . $customerId . ']');
  587. $this->suspendUser($userLogin, true);
  588. }
  589. }
  590. }
  591. /**
  592. * Renders some user profile info
  593. *
  594. * @param int $customerId
  595. *
  596. * @return string
  597. */
  598. public function renderUserInfo($customerId) {
  599. $customerId = vf($customerId, 3);
  600. $result = '';
  601. $result .= wf_AjaxLoader();
  602. $userInfo = $this->hls->getUserInfo($customerId);
  603. $localUserInfo = @$this->allUsers[$customerId];
  604. if (isset($userInfo['result'])) {
  605. $userInfo = $userInfo['result'];
  606. $cells = wf_TableCell(__('Full address'), '', 'row2');
  607. $userAddress = @$this->allUserData[$localUserInfo['login']]['fulladress'];
  608. $userLink = wf_Link(self::URL_PROFILE . $localUserInfo['login'], web_profile_icon() . ' ' . $userAddress);
  609. $cells .= wf_TableCell($userLink);
  610. $rows = wf_TableRow($cells, 'row3');
  611. $cells = wf_TableCell(__('ID'), '', 'row2');
  612. $cells .= wf_TableCell($userInfo['id']);
  613. $rows .= wf_TableRow($cells, 'row3');
  614. $cells = wf_TableCell(__('Date'), '', 'row2');
  615. $cells .= wf_TableCell($localUserInfo['actdate']);
  616. $rows .= wf_TableRow($cells, 'row3');
  617. if (!empty($userInfo['tariff'])) {
  618. foreach ($userInfo['tariff'] as $io => $each) {
  619. $cells = wf_TableCell(__('Tariffs') . ' ' . __($io), '', 'row2');
  620. $tariffsList = '';
  621. if (!empty($each)) {
  622. foreach ($each as $ia => $tariffId) {
  623. $tariffsList .= $this->getTariffName($tariffId) . ' ';
  624. }
  625. }
  626. $cells .= wf_TableCell($tariffsList);
  627. $rows .= wf_TableRow($cells, 'row3');
  628. }
  629. }
  630. $cells = wf_TableCell(__('Status'), '', 'row2');
  631. $cells .= wf_TableCell(web_bool_led($userInfo['status']));
  632. $rows .= wf_TableRow($cells, 'row3');
  633. $cells = wf_TableCell(__('Preview'), '', 'row2');
  634. $cells .= wf_TableCell(wf_Link($userInfo['web_url'], __('View online'), false, '', 'TARGET="_BLANK"'));
  635. $rows .= wf_TableRow($cells, 'row3');
  636. if (!empty($userInfo['devices'])) {
  637. foreach ($userInfo['devices'] as $io => $each) {
  638. $cells = wf_TableCell(__('Device') . ' ' . $io, '', 'row2');
  639. $deviceLabel = __('Uniq') . ': ' . $each['uniq'] . ' ' . __('Date') . ': ' . date("Y-m-d H:i:s", $each['activation_data']) . ' ' . __('Model') . ': ' . $each['model'];
  640. $deviceControls = wf_JSAlert(self::URL_ME . '&subscriptions=true&customerid=' . $customerId . '&deletedevice=' . $each['uniq'], web_delete_icon(), $this->messages->getDeleteAlert());
  641. $cells .= wf_TableCell($deviceControls . ' ' . $deviceLabel);
  642. $rows .= wf_TableRow($cells, 'row3');
  643. }
  644. }
  645. if (!empty($userInfo['playlists'])) {
  646. foreach ($userInfo['playlists'] as $io => $each) {
  647. $cells = wf_TableCell(__('Playlist') . ' ' . $io, '', 'row2');
  648. $actDate = ($each['activation_data']) ? date("Y-m-d H:i:s", $each['activation_data']) : __('Inactive');
  649. $playlistLabel = __('Uniq') . ': ' . $each['uniq'] . ' ' . __('Date') . ': ' . $actDate . ' ' . wf_Link($each['url'], __('Download'));
  650. $playlistControls = wf_JSAlert(self::URL_ME . '&subscriptions=true&customerid=' . $customerId . '&deleteplaylist=' . $each['uniq'], web_delete_icon(), $this->messages->getDeleteAlert());
  651. $cells .= wf_TableCell($playlistControls . ' ' . $playlistLabel);
  652. $rows .= wf_TableRow($cells, 'row3');
  653. }
  654. }
  655. $cells = wf_TableCell(__('Device activation'), '', 'row2');
  656. $getCodeLink = $this->ajDevCodeLink($customerId, __('Get code'));
  657. $cells .= wf_TableCell(wf_AjaxContainer('deviceactivationcodecontainer', '', $getCodeLink));
  658. $rows .= wf_TableRow($cells, 'row3');
  659. $result .= wf_TableBody($rows, '100%', 0);
  660. }
  661. if (!empty($localUserInfo)) {
  662. $result .= wf_tag('b') . __('Local profile') . wf_tag('b', true) . wf_tag('br');
  663. $rows = '';
  664. $cells = wf_TableCell(__('Tariff') . ' ' . __('base'), '', 'row2');
  665. $cells .= wf_TableCell($this->getTariffName($localUserInfo['basetariffid']));
  666. $rows .= wf_TableRow($cells, 'row3');
  667. $bundleTariffs = $this->extractBundle($customerId);
  668. $bundleTariffsList = '';
  669. if (!empty($bundleTariffs)) {
  670. foreach ($bundleTariffs as $io => $each) {
  671. $bundleTariffsList .= $this->getTariffName($io) . ' ';
  672. }
  673. }
  674. $cells = wf_TableCell(__('Tariffs') . ' ' . __('bundle'), '', 'row2');
  675. $cells .= wf_TableCell($bundleTariffsList);
  676. $rows .= wf_TableRow($cells, 'row3');
  677. $cells = wf_TableCell(__('Status'), '', 'row2');
  678. $cells .= wf_TableCell(web_bool_led($localUserInfo['active']));
  679. $rows .= wf_TableRow($cells, 'row3');
  680. $cells = wf_TableCell(__('Suspended'), '', 'row2');
  681. $suspFlag = (isset($this->suspended[$localUserInfo['login']])) ? true : false;
  682. $cells .= wf_TableCell(web_bool_led($suspFlag));
  683. $rows .= wf_TableRow($cells, 'row3');
  684. $result .= wf_TableBody($rows, '100%', 0);
  685. }
  686. $result .= $this->renderProfileControls($customerId);
  687. return($result);
  688. }
  689. /**
  690. * Renders list of all devices with some controls
  691. *
  692. * @return string
  693. */
  694. public function renderDevicesList() {
  695. $result = '';
  696. $allDevices = $this->hls->getDeviceList();
  697. if (isset($allDevices['result'])) {
  698. if (!empty($allDevices['result'])) {
  699. $allDevices = $allDevices['result'];
  700. $cells = wf_TableCell(__('Uniq'));
  701. $cells .= wf_TableCell(__('Model'));
  702. $cells .= wf_TableCell(__('Registration'));
  703. $cells .= wf_TableCell(__('Activation'));
  704. $cells .= wf_TableCell(__('User'));
  705. $cells .= wf_TableCell(__('Actions'));
  706. $rows = wf_TableRow($cells, 'row1');
  707. foreach ($allDevices as $io => $each) {
  708. $cells = wf_TableCell($each['uniq']);
  709. $cells .= wf_TableCell($each['model']);
  710. $cells .= wf_TableCell(date("Y-m-d H:i:s", $each['registration_date']));
  711. $cells .= wf_TableCell(date("Y-m-d H:i:s", $each['activation_date']));
  712. $userLogin = $this->getLocalCustomerLogin($each['customer_id']);
  713. $userAddress = @$this->allUserData[$userLogin]['fulladress'];
  714. $userLink = wf_Link(self::URL_SUBSCRIBER . $each['customer_id'], web_profile_icon() . ' ' . $userAddress);
  715. $cells .= wf_TableCell($userLink);
  716. $actLinks = wf_JSAlert(self::URL_ME . '&devices=true&customerid=' . $each['customer_id'] . '&deletedevice=' . $each['uniq'], web_delete_icon(), $this->messages->getDeleteAlert());
  717. $cells .= wf_TableCell($actLinks);
  718. $rows .= wf_TableRow($cells, 'row5');
  719. }
  720. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  721. } else {
  722. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'warning');
  723. }
  724. } else {
  725. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': EX_NOREPLY', 'warning');
  726. }
  727. return ($result);
  728. }
  729. /**
  730. * Extract existing customer available tariffs
  731. *
  732. * @param int $customerId
  733. *
  734. * @return array
  735. */
  736. protected function extractBundle($customerId) {
  737. $result = array();
  738. if (isset($this->allUsers[$customerId])) {
  739. $localUserInfo = $this->allUsers[$customerId];
  740. $bundleTariffs = $localUserInfo['bundletariffs'];
  741. if (!empty($bundleTariffs)) {
  742. $result = unserialize($bundleTariffs);
  743. }
  744. }
  745. return ($result);
  746. }
  747. /**
  748. * Returns device activation code ajax link
  749. *
  750. * @param int $customerId
  751. * @param string $label
  752. *
  753. * @return string
  754. */
  755. protected function ajDevCodeLink($customerId, $label) {
  756. $result = wf_AjaxLink(self::URL_ME . '&subscriptions=true&getdevicecode=' . $customerId, $label, 'deviceactivationcodecontainer');
  757. return ($result);
  758. }
  759. /**
  760. * Renders default module controls
  761. *
  762. * @return string
  763. */
  764. public function renderPanel() {
  765. $result = '';
  766. $result .= wf_Link(self::URL_ME . '&subscriptions=true', wf_img('skins/ukv/users.png') . ' ' . __('Subscriptions'), false, 'ubButton') . ' ';
  767. $result .= wf_modalAuto(wf_img('skins/ukv/add.png') . ' ' . __('Users registration'), __('Registration'), $this->renderUserRegisterForm(), 'ubButton');
  768. $result .= wf_Link(self::URL_ME . '&tariffs=true', wf_img('skins/ukv/dollar.png') . ' ' . __('Tariffs'), false, 'ubButton') . ' ';
  769. $result .= wf_Link(self::URL_ME . '&devices=true', wf_img('skins/switch_models.png') . ' ' . __('Devices'), false, 'ubButton') . ' ';
  770. // $result .= wf_Link(self::URL_ME . '&reports=true', wf_img('skins/ukv/report.png') . ' ' . __('Reports'), false, 'ubButton') . ' ';
  771. return($result);
  772. }
  773. /**
  774. * Renders new customer registration form
  775. *
  776. * @return string
  777. */
  778. protected function renderUserRegisterForm() {
  779. $result = '';
  780. $loginPreset = (wf_CheckGet(array('username'))) ? $_GET['username'] : '';
  781. $inputs = wf_HiddenInput('manualregister', 'true');
  782. $inputs .= wf_TextInput('manualregisterlogin', __('Login'), $loginPreset, true, '15');
  783. $inputs .= wf_Submit(__('Create'));
  784. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  785. return ($result);
  786. }
  787. /**
  788. * Creates new user profile for existing internet user
  789. *
  790. * @param string $userLogin
  791. *
  792. * @return void/strin on error
  793. */
  794. public function registerUserManual($userLogin) {
  795. $result = '';
  796. if (!empty($userLogin)) {
  797. if (isset($this->allUserData[$userLogin])) {
  798. $userLocalProfile = $this->getLocalCustomerId($userLogin);
  799. if (empty($userLocalProfile)) {
  800. $this->createUserProfile($userLogin);
  801. } else {
  802. $result .= __('Something went wrong') . ': ' . __('Duplicate login') . ' - ' . $userLogin;
  803. log_register('OMEGATV FAIL CUSTOMER REGISTER (' . $userLogin . ') DUPLICATE');
  804. }
  805. } else {
  806. $result .= __('Something went wrong') . ': ' . __('User not exist') . ' - ' . $userLogin;
  807. log_register('OMEGATV FAIL CUSTOMER REGISTER (' . $userLogin . ') NOLOGIN');
  808. }
  809. }
  810. return ($result);
  811. }
  812. /**
  813. * Renders channels preview controls panel
  814. *
  815. * @return string
  816. */
  817. public function renderChanControls() {
  818. $result = wf_Link(self::URL_ME . '&tariffs=true&chanlist=base', web_icon_search() . ' ' . __('Base'), false, 'ubButton');
  819. $result .= wf_Link(self::URL_ME . '&tariffs=true&chanlist=bundle', web_icon_search() . ' ' . __('Bundle'), false, 'ubButton');
  820. $result .= wf_Link(self::URL_ME . '&tariffs=true&chanlist=promo', web_icon_search() . ' ' . __('Promo'), false, 'ubButton');
  821. return ($result);
  822. }
  823. /**
  824. * Returns array of available remote tariffs as tariffid=>name
  825. *
  826. * @return array
  827. */
  828. protected function getTariffsRemote() {
  829. $result = array();
  830. $baseTariffs = $this->hls->getTariffsBase();
  831. $bundleTariffs = $this->hls->getTariffsBundle();
  832. $promoTariffs = $this->hls->getTariffsPromo();
  833. if (isset($baseTariffs['result'])) {
  834. foreach ($baseTariffs['result'] as $io => $each) {
  835. $result[$each['tariff_id']] = $each['tariff_name'] . ' (' . __('base') . ')';
  836. }
  837. }
  838. if (isset($bundleTariffs['result'])) {
  839. foreach ($bundleTariffs['result'] as $io => $each) {
  840. $result[$each['tariff_id']] = $each['tariff_name'] . ' (' . __('bundle') . ')';
  841. }
  842. }
  843. if (isset($promoTariffs['result'])) {
  844. if (isset($promoTariffs['result']['promo_limited'])) {
  845. foreach ($promoTariffs['result']['promo_limited'] as $io => $each) {
  846. $result[$each['tariff_id']] = $each['tariff_name'] . ' (' . __('promo limited') . ')';
  847. }
  848. }
  849. if (isset($promoTariffs['result']['promo'])) {
  850. foreach ($promoTariffs['result']['promo'] as $io => $each) {
  851. $result[$each['tariff_id']] = $each['tariff_name'] . ' (' . __('promo') . ')';
  852. }
  853. }
  854. }
  855. return($result);
  856. }
  857. /**
  858. * Renders tariff creation form
  859. *
  860. * @return string
  861. */
  862. public function renderTariffCreateForm() {
  863. $result = '';
  864. $remoteTariffs = $this->getTariffsRemote();
  865. $tmpArr = array();
  866. if (!empty($remoteTariffs)) {
  867. foreach ($remoteTariffs as $io => $each) {
  868. //excluding already registered tariffs. Commented due skybetik request.
  869. // if (!isset($this->tariffNames[$io])) {
  870. $tmpArr[$io] = $io . ' - ' . $each;
  871. //}
  872. }
  873. }
  874. if (!empty($tmpArr)) {
  875. $tariffsTypes = array(
  876. 'base' => __('Base'),
  877. 'bundle' => __('Bundle'),
  878. 'promo' => __('Promo')
  879. );
  880. $inputs = wf_Selector('newtariffid', $tmpArr, __('ID'), '', true);
  881. $inputs .= wf_TextInput('newtariffname', __('Tariff name'), '', true, 25);
  882. $inputs .= wf_Selector('newtarifftype', $tariffsTypes, __('Type'), '', true);
  883. $inputs .= wf_TextInput('newtarifffee', __('Fee'), '0', true, 3, 'finance');
  884. $inputs .= wf_Submit(__('Create'));
  885. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  886. } else {
  887. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'info');
  888. }
  889. return($result);
  890. }
  891. /**
  892. * Renders tariff editing form
  893. *
  894. * @param int $tariffId
  895. *
  896. * @return string
  897. */
  898. protected function renderTariffEditForm($tariffId) {
  899. $tariffId = vf($tariffId, 3);
  900. $result = '';
  901. if (isset($this->allTariffs[$tariffId])) {
  902. $tariffData = $this->allTariffs[$tariffId];
  903. if (!empty($tariffData)) {
  904. $tariffsTypes = array(
  905. 'base' => __('Base'),
  906. 'bundle' => __('Bundle'),
  907. 'promo' => __('Promo')
  908. );
  909. $inputs = wf_HiddenInput('edittariffid', $tariffId);
  910. $inputs .= wf_TextInput('edittariffname', __('Tariff name'), $tariffData['tariffname'], true, 25);
  911. $inputs .= wf_Selector('edittarifftype', $tariffsTypes, __('Type'), $tariffData['type'], true);
  912. $inputs .= wf_TextInput('edittarifffee', __('Fee'), $tariffData['fee'], true, 3, 'finance');
  913. $inputs .= wf_Submit(__('Save'));
  914. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  915. }
  916. }
  917. return($result);
  918. }
  919. /**
  920. * Catches tariff editing request and edits it in database if something changed
  921. *
  922. * @return void
  923. */
  924. public function catchTariffSave() {
  925. if (wf_CheckPost(array('edittariffid', 'edittariffname', 'edittarifftype'))) {
  926. $tariffId = vf($_POST['edittariffid'], 3);
  927. $where = "WHERE `id`='" . $tariffId . "';";
  928. if (isset($this->allTariffs[$tariffId])) {
  929. $tariffCurrentData = $this->allTariffs[$tariffId];
  930. if ($_POST['edittariffname'] != $tariffCurrentData['tariffname']) {
  931. simple_update_field('om_tariffs', 'tariffname', $_POST['edittariffname'], $where);
  932. log_register('OMEGATV TARIFF EDIT [' . $tariffCurrentData['tariffid'] . '] AS [' . $tariffId . '] NAME `' . $_POST['edittariffname'] . '`');
  933. }
  934. if ($_POST['edittarifftype'] != $tariffCurrentData['type']) {
  935. simple_update_field('om_tariffs', 'type', $_POST['edittarifftype'], $where);
  936. log_register('OMEGATV TARIFF EDIT [' . $tariffCurrentData['tariffid'] . '] AS [' . $tariffId . '] TYPE `' . $_POST['edittarifftype'] . '`');
  937. }
  938. if ($_POST['edittarifffee'] != $tariffCurrentData['fee']) {
  939. simple_update_field('om_tariffs', 'fee', $_POST['edittarifffee'], $where);
  940. log_register('OMEGATV TARIFF EDIT [' . $tariffCurrentData['tariffid'] . '] AS [' . $tariffId . '] FEE `' . $_POST['edittarifffee'] . '`');
  941. }
  942. }
  943. }
  944. }
  945. /**
  946. * Creates new tariff in database
  947. *
  948. * @return void
  949. */
  950. public function createTariff() {
  951. if (wf_CheckPost(array('newtariffid', 'newtariffname', 'newtarifftype'))) {
  952. $tariffid_f = vf($_POST['newtariffid'], 3);
  953. $name_f = mysql_real_escape_string($_POST['newtariffname']);
  954. $type_f = vf($_POST['newtarifftype']);
  955. $fee = $_POST['newtarifffee'];
  956. $fee_f = mysql_real_escape_string($fee);
  957. $query = "INSERT INTO `om_tariffs` (`id`,`tariffid`,`tariffname`,`type`,`fee`) VALUES ";
  958. $query .= "(NULL,'" . $tariffid_f . "','" . $name_f . "','" . $type_f . "','" . $fee_f . "');";
  959. nr_query($query);
  960. $newId = simple_get_lastid('om_tariffs');
  961. log_register('OMEGATV TARIFF CREATE [' . $tariffid_f . '] AS [' . $newId . '] TYPE `' . $type_f . '` FEE `' . $fee . '`');
  962. }
  963. }
  964. /**
  965. * Renders list of available tariffs
  966. *
  967. * @return string
  968. */
  969. public function renderTariffsList() {
  970. $result = '';
  971. if (!empty($this->allTariffs)) {
  972. $cells = wf_TableCell(__('ID'));
  973. $cells .= wf_TableCell(__('Tariff') . ' ' . __('Code'));
  974. $cells .= wf_TableCell(__('Tariff name'));
  975. $cells .= wf_TableCell(__('Type'));
  976. $cells .= wf_TableCell(__('Fee'));
  977. $cells .= wf_TableCell(__('Actions'));
  978. $rows = wf_TableRow($cells, 'row1');
  979. foreach ($this->allTariffs as $io => $each) {
  980. $cells = wf_TableCell($each['id']);
  981. $cells .= wf_TableCell($each['tariffid']);
  982. $cells .= wf_TableCell($each['tariffname']);
  983. $cells .= wf_TableCell(__($each['type']));
  984. $cells .= wf_TableCell($each['fee']);
  985. $actLinks = wf_JSAlert(self::URL_ME . '&tariffs=true&deleteid=' . $each['id'], web_delete_icon(), $this->messages->getDeleteAlert()) . ' ';
  986. $actLinks .= wf_modalAuto(web_edit_icon(), __('Edit'), $this->renderTariffEditForm($each['id'])) . ' ';
  987. $cells .= wf_TableCell($actLinks);
  988. $rows .= wf_TableRow($cells, 'row5');
  989. }
  990. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  991. } else {
  992. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'warning');
  993. }
  994. return($result);
  995. }
  996. /**
  997. * Checks is tariff used by some customers
  998. *
  999. * @param int $id
  1000. *
  1001. * @return bool
  1002. */
  1003. public function isTariffProtected($id) {
  1004. $id = vf($id, 3);
  1005. $result = false;
  1006. if (isset($this->allTariffs[$id])) {
  1007. $tariffCode = $this->allTariffs[$id]['tariffid'];
  1008. if (!empty($this->allUsers)) {
  1009. foreach ($this->allUsers as $io => $each) {
  1010. if ($each['basetariffid'] == $tariffCode) {
  1011. $result = true;
  1012. }
  1013. $userBundleTariffs = $this->extractBundle($io);
  1014. if (isset($userBundleTariffs[$tariffCode])) {
  1015. $result = true;
  1016. }
  1017. }
  1018. }
  1019. }
  1020. return ($result);
  1021. }
  1022. /**
  1023. * Deletes some tariff from database
  1024. *
  1025. * @param int $id
  1026. *
  1027. * @return void
  1028. */
  1029. public function deleteTariff($id) {
  1030. $id = vf($id, 3);
  1031. if (isset($this->allTariffs[$id])) {
  1032. $query = "DELETE from `om_tariffs` WHERE `id`='" . $id . "';";
  1033. nr_query($query);
  1034. log_register('OMEGATV TARIFF DELETE [' . $id . ']');
  1035. }
  1036. }
  1037. /**
  1038. * Returns user login transformed to some numeric hash
  1039. *
  1040. * @param string $login
  1041. *
  1042. * @return int
  1043. */
  1044. public function generateCustormerId($login) {
  1045. $result = '';
  1046. if (!empty($login)) {
  1047. $result = crc32($login);
  1048. }
  1049. return($result);
  1050. }
  1051. /**
  1052. * Returns web-player URL
  1053. *
  1054. * @param int $customerId
  1055. *
  1056. * @return string
  1057. */
  1058. public function generateWebURL($customerId) {
  1059. $result = '';
  1060. $userInfo = $this->hls->getUserInfo($customerId);
  1061. if (isset($userInfo['result'])) {
  1062. $userInfo = $userInfo['result'];
  1063. if (!empty($userInfo)) {
  1064. $result .= $userInfo['web_url'];
  1065. }
  1066. }
  1067. return ($result);
  1068. }
  1069. /**
  1070. * Returns web URL by some user login
  1071. *
  1072. * @param string $userLogin
  1073. *
  1074. * @return string
  1075. */
  1076. public function getWebUrlByLogin($userLogin) {
  1077. $result = '';
  1078. $customerId = $this->getLocalCustomerId($userLogin);
  1079. if (!empty($customerId)) {
  1080. //already existing user
  1081. $result .= $this->generateWebURL($customerId);
  1082. } else {
  1083. //first usage
  1084. $this->createUserProfile($userLogin);
  1085. $customerId = $this->getLocalCustomerId($userLogin);
  1086. $result .= $this->generateWebURL($customerId);
  1087. }
  1088. return ($result);
  1089. }
  1090. /**
  1091. * Returns local customer ID from database
  1092. *
  1093. * @param string $userLogin
  1094. *
  1095. * @return int
  1096. */
  1097. public function getLocalCustomerId($userLogin) {
  1098. $result = '';
  1099. if (!empty($this->allUsers)) {
  1100. foreach ($this->allUsers as $io => $each) {
  1101. if ($each['login'] == $userLogin) {
  1102. $result = $each['customerid'];
  1103. break;
  1104. }
  1105. }
  1106. }
  1107. return ($result);
  1108. }
  1109. /**
  1110. * Returns local customer login by ID from database
  1111. *
  1112. * @param int $customerId
  1113. *
  1114. * @return string
  1115. */
  1116. public function getLocalCustomerLogin($customerId) {
  1117. $result = '';
  1118. if (!empty($this->allUsers)) {
  1119. if (isset($this->allUsers[$customerId])) {
  1120. $result = $this->allUsers[$customerId]['login'];
  1121. }
  1122. }
  1123. return ($result);
  1124. }
  1125. /**
  1126. * Returns new device activation code
  1127. *
  1128. * @param int $customerId
  1129. *
  1130. * @return string
  1131. */
  1132. public function generateDeviceCode($customerId) {
  1133. $result = '';
  1134. $codeData = $this->hls->getDeviceCode($customerId);
  1135. if (isset($codeData['result'])) {
  1136. if (isset($codeData['result']['code'])) {
  1137. $result .= $codeData['result']['code'] . ' ' . $this->ajDevCodeLink($customerId, __('Renew'));
  1138. }
  1139. }
  1140. return ($result);
  1141. }
  1142. /**
  1143. * Returns new device activation code by user login
  1144. *
  1145. * @param string $userLogin
  1146. *
  1147. * @return string
  1148. */
  1149. public function getDeviceCodeByLogin($userLogin) {
  1150. $result = '';
  1151. $customerId = $this->getLocalCustomerId($userLogin);
  1152. if (!empty($customerId)) {
  1153. $codeData = $this->hls->getDeviceCode($customerId);
  1154. if (isset($codeData['result'])) {
  1155. if (isset($codeData['result']['code'])) {
  1156. $result .= $codeData['result']['code'];
  1157. }
  1158. }
  1159. }
  1160. return ($result);
  1161. }
  1162. /**
  1163. * Deletes device assigned to some customerid
  1164. *
  1165. * @param int $customerId
  1166. * @param string $deviceId
  1167. *
  1168. * @return void
  1169. */
  1170. public function deleteDevice($customerId, $deviceId) {
  1171. $this->hls->deleteDevice($customerId, $deviceId);
  1172. }
  1173. /**
  1174. * Deletes playlist assigned to some customerid
  1175. *
  1176. * @param int $customerId
  1177. * @param string $playlistId
  1178. *
  1179. * @return void
  1180. */
  1181. public function deletePlaylist($customerId, $playlistId) {
  1182. $this->hls->deletePlayList($customerId, $playlistId);
  1183. }
  1184. /**
  1185. * Creates new user profile
  1186. *
  1187. * @param string $userLogin
  1188. *
  1189. * @return void
  1190. */
  1191. protected function createUserProfile($userLogin) {
  1192. $customerId = $this->generateCustormerId($userLogin);
  1193. $login_f = mysql_real_escape_string($userLogin);
  1194. $curdate = curdatetime();
  1195. $query = "INSERT INTO `om_users` (`id`,`login`,`customerid`,`basetariffid`,`bundletariffs`,`active`,`actdate`) VALUES ";
  1196. $query .= "(NULL,'" . $login_f . "','" . $customerId . "',NULL,NULL,'0','" . $curdate . "');";
  1197. nr_query($query);
  1198. log_register('OMEGATV CUSTOMER REGISTER (' . $userLogin . ') AS [' . $customerId . ']');
  1199. $this->loadUserProfiles();
  1200. }
  1201. /**
  1202. * Returns tariff local data
  1203. *
  1204. * @param int $tariffId
  1205. *
  1206. * @return array
  1207. */
  1208. protected function getTariffData($tariffId) {
  1209. $result = array();
  1210. if (!empty($this->allTariffs)) {
  1211. foreach ($this->allTariffs as $io => $each) {
  1212. if ($each['tariffid'] == $tariffId) {
  1213. $result = $each;
  1214. }
  1215. }
  1216. }
  1217. return ($result);
  1218. }
  1219. /**
  1220. * Creates some subscription if it possible
  1221. *
  1222. * @param string $userLogin
  1223. * @param int $tariffId
  1224. *
  1225. * @return void/string on error
  1226. */
  1227. public function createSubscription($userLogin, $tariffId) {
  1228. $result = '';
  1229. $tariffId = vf($tariffId, 3);
  1230. if (isset($this->tariffNames[$tariffId])) {
  1231. if (isset($this->allUserData[$userLogin])) {
  1232. $customerId = $this->getLocalCustomerId($userLogin);
  1233. if (!empty($customerId)) {
  1234. $subscriberData = $this->allUsers[$customerId];
  1235. $tariffData = $this->getTariffData($tariffId);
  1236. if (!empty($tariffData)) {
  1237. //base tariff subscription
  1238. if ($tariffData['type'] == 'base') {
  1239. if (empty($subscriberData['basetariffid'])) {
  1240. $setTariffList = array('base' => $tariffId);
  1241. $this->hls->setUserTariff($customerId, $setTariffList);
  1242. simple_update_field('om_users', 'basetariffid', $tariffId, "WHERE `customerid`='" . $customerId . "'");
  1243. $this->hls->setUserActivate($customerId);
  1244. simple_update_field('om_users', 'active', '1', "WHERE `customerid`='" . $customerId . "'");
  1245. log_register('OMEGATV SUBSCRIBE TARIFF [' . $tariffId . '] BASE FOR (' . $userLogin . ') AS [' . $customerId . ']');
  1246. //delete user from suspend queue
  1247. $this->suspendUser($userLogin, false);
  1248. } else {
  1249. $result .= 'Only one base tariff allowed';
  1250. }
  1251. }
  1252. //bundle tariffs subscription
  1253. if ($tariffData['type'] == 'bundle') {
  1254. if (!empty($subscriberData['basetariffid'])) {
  1255. $bundleTariffsCurrent = $this->extractBundle($customerId);
  1256. if (!isset($bundleTariffsCurrent[$tariffId])) {
  1257. $bundleTariffsCurrent[$tariffId] = $tariffId;
  1258. $setTariffList = array('base' => $subscriberData['basetariffid'], 'bundle' => $bundleTariffsCurrent);
  1259. $this->hls->setUserTariff($customerId, $setTariffList);
  1260. $storeBundleTariffs = serialize($bundleTariffsCurrent);
  1261. simple_update_field('om_users', 'bundletariffs', $storeBundleTariffs, "WHERE `customerid`='" . $customerId . "'");
  1262. log_register('OMEGATV SUBSCRIBE TARIFF [' . $tariffId . '] BUNDLE FOR (' . $userLogin . ') AS [' . $customerId . ']');
  1263. } else {
  1264. $result .= 'Tariff already subscribed';
  1265. }
  1266. } else {
  1267. $result .= 'Available only in addition to base tariff';
  1268. }
  1269. }
  1270. } else {
  1271. $result .= 'Local tariff not exists';
  1272. }
  1273. } else {
  1274. $result .= 'Subscriber profile not found';
  1275. }
  1276. } else {
  1277. $result .= 'User login not found';
  1278. }
  1279. } else {
  1280. $result .= 'Wrong tariff';
  1281. }
  1282. return ($result);
  1283. }
  1284. /**
  1285. * Charges fee for some tariff
  1286. *
  1287. * @param string $userLogin
  1288. * @param int $tariffId
  1289. *
  1290. * @return void
  1291. */
  1292. protected function chargeFee($userLogin, $tariffId) {
  1293. $tariffData = $this->getTariffData($tariffId);
  1294. $customerId = $this->getLocalCustomerId($userLogin);
  1295. $tariffFee = $tariffData['fee'];
  1296. zb_CashAdd($userLogin, '-' . $tariffFee, 'add', 1, 'OMEGATV:' . $tariffId);
  1297. log_register('OMEGATV CHARGE TARIFF [' . $tariffId . '] FEE `' . $tariffFee . '` FOR (' . $userLogin . ') AS [' . $customerId . ']');
  1298. }
  1299. /**
  1300. * Sets user as suspended or not to preventing his automatic ressurection
  1301. *
  1302. * @param string $userLogin
  1303. * @param bool $state
  1304. *
  1305. * @return void
  1306. */
  1307. protected function suspendUser($userLogin, $state) {
  1308. $login_f = mysql_real_escape_string($userLogin);
  1309. $customerId = $this->getLocalCustomerId($userLogin);
  1310. if ($state) {
  1311. $query = "INSERT INTO `om_suspend` (`id`,`login`) VALUES (NULL,'" . $login_f . "');";
  1312. nr_query($query);
  1313. log_register('OMEGATV SUSPEND USER (' . $userLogin . ') AS [' . $customerId . ']');
  1314. } else {
  1315. $query = "DELETE FROM `om_suspend` WHERE `login`='" . $login_f . "'";
  1316. nr_query($query);
  1317. log_register('OMEGATV UNSUSPEND USER (' . $userLogin . ') AS [' . $customerId . ']');
  1318. }
  1319. }
  1320. /**
  1321. * Deletes or pushes queue for some subscription if it possible
  1322. *
  1323. * @param string $userLogin
  1324. * @param int $tariffId
  1325. *
  1326. * @return void/string on error
  1327. */
  1328. public function deleteSubscription($userLogin, $tariffId) {
  1329. $result = '';
  1330. $tariffId = vf($tariffId, 3);
  1331. if (isset($this->tariffNames[$tariffId])) {
  1332. if (isset($this->allUserData[$userLogin])) {
  1333. $customerId = $this->getLocalCustomerId($userLogin);
  1334. if (!empty($customerId)) {
  1335. $subscriberData = $this->allUsers[$customerId];
  1336. $tariffData = $this->getTariffData($tariffId);
  1337. if (!empty($tariffData)) {
  1338. //base tariff unsubscription
  1339. if ($tariffData['type'] == 'base') {
  1340. if (!empty($subscriberData['basetariffid'])) {
  1341. if ($subscriberData['basetariffid'] == $tariffId) {
  1342. //unsubscription right now. Base tariff kills additional tariffs too.
  1343. if (!$this->unsubDelay) {
  1344. //charging fee for all tariffs
  1345. $baseTariffFee = $tariffData['fee'];
  1346. $this->chargeFee($userLogin, $tariffId);
  1347. $bundleTariffs = $this->extractBundle($customerId);
  1348. if (!empty($bundleTariffs)) {
  1349. foreach ($bundleTariffs as $io => $each) {
  1350. $this->chargeFee($userLogin, $io);
  1351. }
  1352. }
  1353. //setting user down
  1354. $this->hls->setUserBlock($customerId);
  1355. simple_update_field('om_users', 'active', '0', "WHERE `customerid`='" . $customerId . "'");
  1356. //dropping local tariffs
  1357. simple_update_field('om_users', 'basetariffid', '', "WHERE `customerid`='" . $customerId . "'");
  1358. simple_update_field('om_users', 'bundletariffs', '', "WHERE `customerid`='" . $customerId . "'");
  1359. log_register('OMEGATV UNSUBSCRIBE TARIFF [' . $tariffId . '] BASE FOR (' . $userLogin . ') AS [' . $customerId . ']');
  1360. //suspending user to prevent his automatic ressurection
  1361. $this->suspendUser($userLogin, true);
  1362. } else {
  1363. //TODO: push unsub to queue
  1364. }
  1365. } else {
  1366. $result .= 'This tariff is not assigned for you';
  1367. }
  1368. } else {
  1369. $result .= 'You have not assigned base tariff';
  1370. }
  1371. }
  1372. //bundle tariffs unsubscription
  1373. if ($tariffData['type'] == 'bundle') {
  1374. $bundleTariffsCurrent = $this->extractBundle($customerId);
  1375. //unsubscription right now.
  1376. if (!$this->unsubDelay) {
  1377. if (isset($bundleTariffsCurrent[$tariffId])) {
  1378. unset($bundleTariffsCurrent[$tariffId]);
  1379. $setTariffList = array('base' => $subscriberData['basetariffid'], 'bundle' => $bundleTariffsCurrent);
  1380. //charging fee for this bundle tariff
  1381. $this->chargeFee($userLogin, $tariffId);
  1382. $this->hls->setUserTariff($customerId, $setTariffList);
  1383. $storeBundleTariffs = serialize($bundleTariffsCurrent);
  1384. simple_update_field('om_users', 'bundletariffs', $storeBundleTariffs, "WHERE `customerid`='" . $customerId . "'");
  1385. log_register('OMEGATV UNSUBSCRIBE TARIFF [' . $tariffId . '] BUNDLE FOR (' . $userLogin . ') AS [' . $customerId . ']');
  1386. } else {
  1387. $result .= 'This tariff is not assigned for you';
  1388. }
  1389. } else {
  1390. //TODO: push bundle unsub to queue
  1391. }
  1392. }
  1393. } else {
  1394. $result .= 'Local tariff not exists';
  1395. }
  1396. } else {
  1397. $result .= 'Subscriber profile not found';
  1398. }
  1399. } else {
  1400. $result .= 'User login not found';
  1401. }
  1402. } else {
  1403. $result .= 'Wrong tariff';
  1404. }
  1405. return ($result);
  1406. }
  1407. /**
  1408. * Renders available subscriptions container list with some controls
  1409. *
  1410. * @return string
  1411. */
  1412. public function renderUserListContainer() {
  1413. $result = '';
  1414. $columns = array('ID', 'Full address', 'Real Name', 'Cash', 'Base tariff', 'Bundle tariffs', 'Date', 'Active', 'Customer ID', 'Actions');
  1415. $result .= wf_JqDtLoader($columns, self::URL_ME . '&subscriptions=true&ajuserlist=true', false, __('Users'));
  1416. return ($result);
  1417. }
  1418. /**
  1419. * Renders JSON data for ajax user list container content
  1420. *
  1421. * @return void
  1422. */
  1423. public function ajUserList() {
  1424. $result = '';
  1425. $json = new wf_JqDtHelper();
  1426. if (!empty($this->allUsers)) {
  1427. foreach ($this->allUsers as $io => $each) {
  1428. $userAddress = @$this->allUserData[$each['login']]['fulladress'];
  1429. $userLink = wf_Link(self::URL_PROFILE . $each['login'], web_profile_icon() . ' ' . $userAddress);
  1430. $data[] = $each['id'];
  1431. $data[] = $userLink;
  1432. $data[] = @$this->allUserData[$each['login']]['realname'];
  1433. $data[] = @$this->allUserData[$each['login']]['Cash'];
  1434. $data[] = $this->getTariffName($each['basetariffid']);
  1435. $bundleList = '';
  1436. if (!empty($each['bundletariffs'])) {
  1437. $allBundle = unserialize($each['bundletariffs']);
  1438. if (!empty($allBundle)) {
  1439. foreach ($allBundle as $bundleTariffId => $eachbundleData) {
  1440. $bundleList .= $this->getTariffName($bundleTariffId) . ' ';
  1441. }
  1442. }
  1443. }
  1444. $data[] = $bundleList;
  1445. $data[] = $each['actdate'];
  1446. $data[] = web_bool_led($each['active'], true);
  1447. $data[] = $each['customerid'];
  1448. $actLinks = wf_Link(self::URL_ME . '&customerprofile=' . $each['customerid'], web_edit_icon());
  1449. $actLinks .= wf_Link('https://admin.hls.tv/customers/operator#page=1&search=' . $each['customerid'], web_icon_search());
  1450. $data[] = $actLinks;
  1451. $json->addRow($data);
  1452. unset($data);
  1453. }
  1454. }
  1455. $json->getJson();
  1456. }
  1457. /**
  1458. * Charges all users tariffs fee, disables it when users go down
  1459. *
  1460. * @return void
  1461. */
  1462. public function chargeAllUsersFee() {
  1463. if (!empty($this->allUsers)) {
  1464. foreach ($this->allUsers as $io => $each) {
  1465. if ($each['active']) {
  1466. if (isset($this->allUserData[$each['login']])) {
  1467. if (!$this->allUserData[$each['login']]['Passive']) {
  1468. $userInternetTariff = $this->allUserData[$each['login']]['Tariff'];
  1469. //user is not on internet bundled tariff
  1470. if (!isset($this->bundledTariffs[$userInternetTariff])) {
  1471. if (!empty($each['basetariffid'])) {
  1472. $this->chargeFee($each['login'], $each['basetariffid']);
  1473. $userBundleTariffs = $this->extractBundle($each['customerid']);
  1474. if (!empty($userBundleTariffs)) {
  1475. foreach ($userBundleTariffs as $eachBundleId => $eachBundleTariff) {
  1476. $this->chargeFee($each['login'], $eachBundleId);
  1477. }
  1478. }
  1479. }
  1480. } else {
  1481. log_register('OMEGATV CHARGE SKIP INETBUNDLE FOR (' . $each['login'] . ') AS [' . $each['id'] . ']');
  1482. }
  1483. //end of internet bundle check
  1484. }
  1485. }
  1486. }
  1487. }
  1488. //checking for debtors/freezed users and disabling it
  1489. $this->loadUserData();
  1490. foreach ($this->allUsers as $io => $each) {
  1491. if ($each['active']) {
  1492. if (isset($this->allUserData[$each['login']])) {
  1493. $userData = $this->allUserData[$each['login']];
  1494. if ($userData['Passive']) {
  1495. //user is frozen by some reason - need to disable him
  1496. $this->hls->setUserBlock($each['customerid']);
  1497. simple_update_field('om_users', 'active', '0', "WHERE `customerid`='" . $each['customerid'] . "'");
  1498. log_register('OMEGATV BLOCK FROZEN USER (' . $each['login'] . ') AS [' . $each['customerid'] . ']');
  1499. }
  1500. //if user have debt after charging fee - we need to block him too
  1501. if ($userData['Cash'] < '-' . $userData['Credit']) {
  1502. $this->hls->setUserBlock($each['customerid']);
  1503. simple_update_field('om_users', 'active', '0', "WHERE `customerid`='" . $each['customerid'] . "'");
  1504. log_register('OMEGATV BLOCK DEBTOR USER (' . $each['login'] . ') AS [' . $each['customerid'] . ']');
  1505. }
  1506. }
  1507. }
  1508. }
  1509. }
  1510. }
  1511. /**
  1512. * Resurrects some users if their was disabled by inactivity
  1513. *
  1514. * @return void
  1515. */
  1516. public function resurrectAllUsers() {
  1517. if (!empty($this->allUsers)) {
  1518. foreach ($this->allUsers as $io => $each) {
  1519. if (!$each['active']) {
  1520. if (isset($this->allUserData[$each['login']])) {
  1521. $userData = $this->allUserData[$each['login']];
  1522. if (($userData['Passive'] == 0) AND ( $userData['Cash'] >= '-' . $userData['Credit'])) {
  1523. if (!empty($each['basetariffid'])) {
  1524. //check is user resurrection suspended?
  1525. if (!isset($this->suspended[$each['login']])) {
  1526. //unblock this user
  1527. $this->hls->setUserActivate($each['customerid']);
  1528. simple_update_field('om_users', 'active', '1', "WHERE `customerid`='" . $each['customerid'] . "'");
  1529. log_register('OMEGATV RESURRECT USER (' . $each['login'] . ') AS [' . $each['customerid'] . ']');
  1530. }
  1531. }
  1532. }
  1533. }
  1534. }
  1535. }
  1536. }
  1537. }
  1538. }
  1539. ?>