api.trinitytv.php 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  1. <?php
  2. /**
  3. * TrinityTV low-level API implementation
  4. *
  5. * http://trinity-tv.net/
  6. * http://partners.trinity-tv.net/
  7. */
  8. class TrinityTvApi {
  9. /**
  10. * Partner ID
  11. *
  12. * @var string
  13. */
  14. protected $partnerId = '';
  15. /**
  16. * Key to generate an authorization request
  17. *
  18. * @var string
  19. */
  20. protected $salt = '';
  21. /**
  22. * API URL
  23. *
  24. * @var string
  25. */
  26. protected $urlApi = 'http://partners.trinity-tv.net/partners';
  27. /**
  28. * Debug flag
  29. *
  30. * @var bool
  31. */
  32. protected $debug = false;
  33. /**
  34. * Default debug log path
  35. */
  36. const LOG_PATH = 'exports/trinitytv.log';
  37. /**
  38. *
  39. * @param string $partnerId
  40. * @param string $salt
  41. * @param string $urlApi
  42. * @param bool $debug
  43. *
  44. * @return void
  45. */
  46. public function __construct($partnerId = '', $salt = '', $urlApi = '', $debug = false) {
  47. $this->partnerId = $partnerId;
  48. $this->salt = $salt;
  49. $this->debug = $debug;
  50. if (!empty($urlApi)) {
  51. $this->urlApi = $urlApi;
  52. }
  53. }
  54. /**
  55. * Add subscription to user
  56. *
  57. * @param int $localid
  58. * @param $subscrid
  59. * @return bool|mixed
  60. */
  61. public function createUser($localid = 0, $subscrid) {
  62. $requestid = $this->getRequestId();
  63. $hash = md5($requestid . $this->partnerId . $localid . $subscrid . $this->salt);
  64. $uri = $this->urlApi . '/user/create?requestid=' . $requestid . '&partnerid=' . $this->partnerId . '&localid=' . $localid . '&subscrid=' . $subscrid . '&hash=' . $hash;
  65. return $this->sendRequest($uri);
  66. }
  67. /**
  68. * Change User Data
  69. *
  70. * @param int $localid
  71. * @param string $lastname
  72. * @param string $firstname
  73. * @param string $middlename
  74. * @param string $address
  75. * @return bool|mixed
  76. */
  77. public function updateUser($localid = 0, $lastname = '', $firstname = '', $middlename = '', $address = '') {
  78. $requestid = $this->getRequestId();
  79. $firstname = urlencode($firstname);
  80. $lastname = urlencode($lastname);
  81. $middlename = urlencode($middlename);
  82. $address = urlencode($address);
  83. $hash = md5($requestid . $this->partnerId . $localid . $firstname . $lastname . $middlename . $address . $this->salt);
  84. $uri = $this->urlApi . '/user/updateuser?requestid=' . $requestid . '&partnerid=' . $this->partnerId . '&localid=' . $localid . '&lastname=' . $lastname . '&firstname=' . $firstname . '&middlename=' . $middlename . '&address=' . $address . '&hash=' . $hash;
  85. return $this->sendRequest($uri);
  86. }
  87. /**
  88. * Getting a list of users and their statuses.
  89. *
  90. * @return bool|mixed
  91. */
  92. public function listUsers() {
  93. $requestid = $this->getRequestId();
  94. $hash = md5($requestid . $this->partnerId . $this->salt);
  95. $uri = $this->urlApi . '/user/subscriberlist?requestid=' . $requestid . '&partnerid=' . $this->partnerId . '&hash=' . $hash;
  96. return $this->sendRequest($uri);
  97. }
  98. /**
  99. * Suspending and Restoring a Subscription
  100. *
  101. * @param int $localid
  102. * @param string $operationid
  103. * @return bool|mixed
  104. */
  105. public function subscription($localid = 0, $operationid = 'suspend') {
  106. $requestid = $this->getRequestId();
  107. $hash = md5($requestid . $this->partnerId . $localid . $operationid . $this->salt);
  108. $uri = $this->urlApi . '/user/subscription?requestid=' . $requestid . '&partnerid=' . $this->partnerId . '&localid=' . $localid . '&operationid=' . $operationid . '&hash=' . $hash;
  109. return $this->sendRequest($uri);
  110. }
  111. /**
  112. * Getting the list of subscriptions of the user.
  113. *
  114. * @param int $localid
  115. * @return bool|mixed
  116. */
  117. public function subscriptionInfo($localid = 0) {
  118. $requestid = $this->getRequestId();
  119. $hash = md5($requestid . $this->partnerId . $localid . $this->salt);
  120. $uri = $this->urlApi . '/user/subscriptioninfo?requestid=' . $requestid . '&partnerid=' . $this->partnerId . '&localid=' . $localid . '&hash=' . $hash;
  121. return $this->sendRequest($uri);
  122. }
  123. /**
  124. * Authorization MAC / UUID device
  125. *
  126. * @param int $localid
  127. * @param string $mac
  128. * @param string $uuid
  129. * @return bool|mixed
  130. */
  131. public function addMacDevice($localid = 0, $mac = '', $uuid = '') {
  132. $requestid = $this->getRequestId();
  133. // The string, mac device subscriber, 12 characters in uppercase
  134. $mac = str_replace(array(
  135. "-",
  136. ":"
  137. ), "", strtoupper($mac));
  138. $hash = md5($requestid . $this->partnerId . $localid . $mac . $this->salt);
  139. $uri = $this->urlApi . '/user/autorizemac?requestid=' . $requestid . '&partnerid=' . $this->partnerId . '&localid=' . $localid . '&mac=' . $mac . '&uuid=' . $uuid . '&hash=' . $hash;
  140. return $this->sendRequest($uri);
  141. }
  142. /**
  143. * Authorization of MAC / UUID device by code
  144. *
  145. * @param int $localid
  146. * @param string $code
  147. * @return bool|mixed
  148. */
  149. public function addCodeMacDevice($localid = 0, $code = '') {
  150. $requestid = $this->getRequestId();
  151. $hash = md5($requestid . $this->partnerId . $localid . $code . $this->salt);
  152. $uri = $this->urlApi . '/user/autorizebycode?requestid=' . $requestid . '&partnerid=' . $this->partnerId . '&localid=' . $localid . '&code=' . $code . '&hash=' . $hash;
  153. return $this->sendRequest($uri);
  154. }
  155. /**
  156. * Deauthorize MAC / UUID devices
  157. *
  158. * @param int $localid
  159. * @param string $mac
  160. * @param string $uuid
  161. * @return bool|mixed
  162. */
  163. public function deleteMacDevice($localid = 0, $mac = '', $uuid = '') {
  164. $requestid = $this->getRequestId();
  165. // The string, mac device subscriber, 12 characters in uppercase
  166. $mac = str_replace(array(
  167. "-",
  168. ":"
  169. ), "", strtoupper($mac));
  170. $hash = md5($requestid . $this->partnerId . $localid . $mac . $this->salt);
  171. $uri = $this->urlApi . '/user/deletemac?requestid=' . $requestid . '&partnerid=' . $this->partnerId . '&localid=' . $localid . '&mac=' . $mac . '&uuid=' . $uuid . '&hash=' . $hash;
  172. return $this->sendRequest($uri);
  173. }
  174. /**
  175. * Listing authorized MAC / UUID devices
  176. *
  177. * @param int $localid
  178. * @return bool|mixed
  179. */
  180. public function listDevices($localid = 0) {
  181. $requestid = $this->getRequestId();
  182. $hash = md5($requestid . $this->partnerId . $localid . $this->salt);
  183. $uri = $this->urlApi . '/user/listmac?requestid=' . $requestid . '&partnerid=' . $this->partnerId . '&localid=' . $localid . '&hash=' . $hash;
  184. return $this->sendRequest($uri);
  185. }
  186. /**
  187. * Generate Unique number
  188. *
  189. * @return mixed
  190. */
  191. private function getRequestId() {
  192. list($usec, $sec) = explode(' ', microtime());
  193. return str_replace('.', '', ((float) $sec . (float) $usec));
  194. }
  195. /**
  196. * Send request
  197. *
  198. * @param $url
  199. * @return bool|mixed
  200. */
  201. private function sendRequest($url) {
  202. if ($this->debug) {
  203. file_put_contents(self::LOG_PATH, curdatetime() . "\n", FILE_APPEND);
  204. file_put_contents(self::LOG_PATH, '>>>>>QUERY>>>>>' . "\n", FILE_APPEND);
  205. file_put_contents(self::LOG_PATH, print_r($url, true) . "\n", FILE_APPEND);
  206. }
  207. /**
  208. * Masked notices output, due 500 errors on some requests.
  209. */
  210. $response = @file_get_contents($url);
  211. if ($this->debug) {
  212. file_put_contents(self::LOG_PATH, '<<<<<RESPONSE<<<<<' . "\n", FILE_APPEND);
  213. if (!empty($response)) {
  214. file_put_contents(self::LOG_PATH, print_r(json_decode($response, true), true) . "\n", FILE_APPEND);
  215. } else {
  216. file_put_contents(self::LOG_PATH, 'EMPTY_RESPONCE_RECEIVED' . "\n", FILE_APPEND);
  217. }
  218. file_put_contents(self::LOG_PATH, '==================' . "\n", FILE_APPEND);
  219. }
  220. if (!empty($response)) {
  221. return json_decode($response);
  222. }
  223. return false;
  224. }
  225. }
  226. /**
  227. * TrinityTV OTT service implementation
  228. */
  229. class TrinityTv {
  230. /**
  231. * TrinityTV API object
  232. *
  233. * @var string
  234. */
  235. protected $api = '';
  236. /**
  237. * Contains all of available trinity tariffs as id=>data
  238. *
  239. * @var array
  240. */
  241. protected $allTariffs = array();
  242. /**
  243. * Contains available and active trinity service subscriptions as id=>data
  244. *
  245. * @var array
  246. */
  247. protected $allSubscribers = array();
  248. /**
  249. * Contains all of internet users data as login=>data
  250. *
  251. * @var array
  252. */
  253. protected $allUsers = array();
  254. /**
  255. * System message helper object placeholder
  256. *
  257. * @var object
  258. */
  259. protected $messages = '';
  260. /**
  261. * Contains default channel icon size
  262. *
  263. * @var int
  264. */
  265. protected $chanIconSize = 32;
  266. /**
  267. * Contains array of currently suspended users without base tariff
  268. *
  269. * @var array
  270. */
  271. protected $suspended = array();
  272. /**
  273. * Devices count rendering flag in subscribers list.
  274. *
  275. * @var int
  276. */
  277. protected $renderDevices = 0;
  278. /**
  279. * Contains system alter config as key=>value
  280. *
  281. * @var array
  282. */
  283. protected $altCfg = array();
  284. /**
  285. * * Default tariffs viewing URL
  286. */
  287. const URL_TARIFFS = 'tariffs=true';
  288. /**
  289. * Basic module path
  290. */
  291. const URL_ME = '?module=trinitytv';
  292. /**
  293. * Default user profile viewing URL
  294. */
  295. const URL_PROFILE = '?module=userprofile&username=';
  296. /**
  297. * Default subscriber profile viewing URL
  298. */
  299. const URL_SUBSCRIBER = '?module=trinitytv&subscriberid=';
  300. const URL_SUBS = 'subscriptions=true';
  301. const URL_AJSUBS = 'ajsubs=true';
  302. const URL_AJDEVS = 'ajdevices=true';
  303. const URL_SUBVIEW = 'subview=true';
  304. const URL_REPORTS = 'reports=true';
  305. const URL_DEVICES = 'devices=true';
  306. const TABLE_SUBS = 'trinitytv_subscribers';
  307. const TABLE_TARIFFS = 'trinitytv_tariffs';
  308. const TABLE_DEVICES = 'trinitytv_devices';
  309. const TABLE_SUSPENDS = 'trinitytv_suspend';
  310. /**
  311. * Creates new TriityTV instance
  312. */
  313. public function __construct() {
  314. $this->loadConfigs();
  315. $this->initApi();
  316. $this->initMessages();
  317. $this->loadTariffs();
  318. $this->loadUsers();
  319. $this->loadSubscribers();
  320. $this->loadSuspended();
  321. }
  322. /**
  323. * Loads required configs into protected props
  324. *
  325. * @global type $ubillingConfig
  326. *
  327. * @return void
  328. */
  329. protected function loadConfigs() {
  330. global $ubillingConfig;
  331. $this->altCfg = $ubillingConfig->getAlter();
  332. //some other options setup
  333. if (isset($this->altCfg['TRINITYTV_RDEVS'])) {
  334. $this->renderDevices = $this->altCfg['TRINITYTV_RDEVS'];
  335. }
  336. }
  337. /**
  338. * Trys to render human-readable tariff name
  339. *
  340. * @param int $tariffId
  341. *
  342. * @return string
  343. */
  344. protected function getTariffName($tariffId) {
  345. $result = '';
  346. if (isset($this->allTariffs[$tariffId])) {
  347. $result .= $this->allTariffs[$tariffId]['name'];
  348. } else {
  349. $result .= $tariffId;
  350. }
  351. return ($result);
  352. }
  353. /**
  354. * Get all devices
  355. *
  356. * @return array
  357. */
  358. private function getDevices() {
  359. $result = array();
  360. $query = "SELECT * from " . self::TABLE_DEVICES;
  361. $devices = simple_queryall($query);
  362. if (!empty($devices)) {
  363. foreach ($devices AS $device) {
  364. $result[$device['id']] = $device;
  365. }
  366. }
  367. return $result;
  368. }
  369. /**
  370. * Get subscriber devices
  371. *
  372. * @param $subscriberId
  373. * @return array
  374. */
  375. private function getSubscriberDevices($subscriberId) {
  376. $result = array();
  377. $subscriberId = mysql_real_escape_string($subscriberId);
  378. $query = "SELECT * from `" . self::TABLE_DEVICES . "` WHERE `subscriber_id` = " . $subscriberId;
  379. $devices = simple_queryall($query);
  380. if (!empty($devices)) {
  381. foreach ($devices AS $device) {
  382. $result[$device['id']] = $device;
  383. }
  384. }
  385. return $result;
  386. }
  387. /**
  388. * Get device id
  389. *
  390. * @param $subscriberId
  391. * @param $mac
  392. * @return bool
  393. */
  394. private function getDeviceId($subscriberId, $mac) {
  395. $subscriberId = mysql_real_escape_string($subscriberId);
  396. $query = "SELECT * from `" . self::TABLE_DEVICES . "` WHERE `subscriber_id` = " . $subscriberId;
  397. $devices = simple_queryall($query);
  398. if (!empty($devices)) {
  399. foreach ($devices AS $device) {
  400. if ($device['mac'] == $mac) {
  401. return $device['id'];
  402. }
  403. }
  404. }
  405. return false;
  406. }
  407. /**
  408. * Returns local subscriber ID from database
  409. *
  410. * @param string $userLogin
  411. *
  412. * @return int
  413. */
  414. public function getSubscriberId($userLogin) {
  415. $result = '';
  416. if (!empty($this->allSubscribers)) {
  417. foreach ($this->allSubscribers as $subscriber) {
  418. if ($subscriber['login'] == $userLogin) {
  419. $result = $subscriber['id'];
  420. break;
  421. }
  422. }
  423. }
  424. return ($result);
  425. }
  426. /**
  427. * Returns local subscriber login by ID from database
  428. *
  429. * @param int $subscriberId
  430. *
  431. * @return string
  432. */
  433. public function getSubscriberLogin($subscriberId) {
  434. $result = '';
  435. if (!empty($this->allSubscribers)) {
  436. if (isset($this->allSubscribers[$subscriberId])) {
  437. $result = $this->allSubscribers[$subscriberId]['login'];
  438. }
  439. }
  440. return ($result);
  441. }
  442. /**
  443. * Returns tariff local data
  444. *
  445. * @param int $tariffId
  446. *
  447. * @return array
  448. */
  449. protected function getTariffData($tariffId) {
  450. $result = array();
  451. if (!empty($this->allTariffs)) {
  452. foreach ($this->allTariffs as $tariff) {
  453. if ($tariff['id'] == $tariffId) {
  454. $result = $tariff;
  455. }
  456. }
  457. }
  458. return ($result);
  459. }
  460. /**
  461. * Returns device vendor lookup concrols + ajax container. wf_AjaxLoader required.
  462. *
  463. * @param string $mac
  464. *
  465. * @return string
  466. */
  467. protected function renderVendorLookup($mac) {
  468. $result = '';
  469. if (@$this->altCfg['MACVEN_ENABLED']) {
  470. if (!empty($mac)) {
  471. $containerName = 'DEVMCVENCNT_' . zb_rand_string(8);
  472. $lookupVendorLink = wf_AjaxLink('?module=macvendor&mac=' . $mac . '&raw=true', wf_img('skins/macven.gif', __('Device vendor')), $containerName, false, '');
  473. $lookupVendorLink .= wf_tag('span', false, '', 'id="' . $containerName . '"') . '' . wf_tag('span', true);
  474. $result .= $lookupVendorLink;
  475. }
  476. }
  477. return($result);
  478. }
  479. /**
  480. * Renders available tariffs list
  481. *
  482. * @param $subscriberId
  483. * @return string
  484. */
  485. public function renderDevices($subscriberId) {
  486. $result = '';
  487. $result .= wf_AjaxLoader();
  488. $cells = wf_TableCell(__('ID'));
  489. $cells .= wf_TableCell(__('MAC address'));
  490. $cells .= wf_TableCell(__('Date'));
  491. if (@$this->altCfg['MACVEN_ENABLED']) {
  492. $cells .= wf_TableCell(__('Manufacturer'));
  493. }
  494. $cells .= wf_TableCell(__('Actions'));
  495. $rows = wf_TableRow($cells, 'row1');
  496. // Add device
  497. $result .= wf_modalAuto(wf_img('skins/switch_models.png') . ' ' . __('Assign device') . ' ' . __('by MAC'), __('Assign device'), $this->renderDeviceAddForm($subscriberId), 'ubButton');
  498. // Add device by MAC
  499. $result .= wf_modalAuto(wf_img('skins/switch_models.png') . ' ' . __('Assign device') . ' ' . __('by code'), __('Assign device'), $this->renderDeviceByCodeAddForm($subscriberId), 'ubButton');
  500. $result .= wf_delimiter();
  501. $devices = $this->getSubscriberDevices($subscriberId);
  502. if (!empty($devices)) {
  503. foreach ($devices as $device) {
  504. $cells = wf_TableCell($device['id']);
  505. $cells .= wf_TableCell($device['mac']);
  506. $cells .= wf_TableCell($device['created_at']);
  507. if (@$this->altCfg['MACVEN_ENABLED']) {
  508. $cells .= wf_TableCell($this->renderVendorLookup($device['mac']));
  509. }
  510. $actLinks = wf_JSAlert(self::URL_ME . '&' . self::URL_SUBSCRIBER . $subscriberId . '&deletedeviceid=' . $device['id'], web_delete_icon(), $this->messages->getDeleteAlert());
  511. $cells .= wf_TableCell($actLinks);
  512. $rows .= wf_TableRow($cells, 'row3');
  513. }
  514. }
  515. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  516. return ($result);
  517. }
  518. /**
  519. * Renders available tariffs list
  520. *
  521. * @return string
  522. */
  523. public function renderTariffs() {
  524. $result = '';
  525. $cells = wf_TableCell(__('ID'));
  526. $cells .= wf_TableCell(__('Tariff name'));
  527. $cells .= wf_TableCell(__('Fee'));
  528. $cells .= wf_TableCell(__('Service ID'));
  529. $cells .= wf_TableCell(__('Actions'));
  530. $rows = wf_TableRow($cells, 'row1');
  531. // Кнопка создать новый тариф
  532. $result .= wf_modalAuto(wf_img('skins/ukv/add.png') . ' ' . __('Create new tariff'), __('Create new tariff'), $this->renderTariffCreateForm(), 'ubButton');
  533. $result .= "<br><br> ";
  534. if (!empty($this->allTariffs)) {
  535. foreach ($this->allTariffs as $tariff) {
  536. $cells = wf_TableCell($tariff['id']);
  537. $cells .= wf_TableCell($tariff['name']);
  538. $cells .= wf_TableCell($tariff['fee']);
  539. $cells .= wf_TableCell($tariff['serviceid']);
  540. $actLinks = wf_JSAlert(self::URL_ME . '&' . self::URL_TARIFFS . '&deletetariffid=' . $tariff['id'], web_delete_icon(), $this->messages->getDeleteAlert());
  541. $actLinks .= wf_modalAuto(web_edit_icon(), __('Edit tariff'), $this->tariffEditForm($tariff['id']));
  542. $cells .= wf_TableCell($actLinks);
  543. $rows .= wf_TableRow($cells, 'row5');
  544. }
  545. }
  546. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  547. return ($result);
  548. }
  549. /**
  550. * Renders tariff creation form
  551. *
  552. * @return string
  553. */
  554. public function renderTariffCreateForm() {
  555. $result = '';
  556. $inputs = wf_TextInput('newtariffname', __('Tariff name'), '', true, '20');
  557. $inputs .= wf_TextInput('newtariffdesc', __('Description'), '', true, '20');
  558. $inputs .= wf_TextInput('newtarifffee', __('Fee'), '', true, '5');
  559. $inputs .= wf_TextInput('newtariffserviceid', __('Service ID'), '', true, '10');
  560. $inputs .= '<hr>';
  561. $inputs .= wf_Submit(__('Create'));
  562. $result = wf_Form('', 'POST', $inputs, 'glamour __StreetEditForm');
  563. return ($result);
  564. }
  565. /**
  566. * Returns tariff editing form
  567. *
  568. * @param int $tariffId
  569. *
  570. * @return string
  571. */
  572. protected function tariffEditForm($tariffId) {
  573. $result = '';
  574. $inputs = wf_HiddenInput('edittariffid', $tariffId);
  575. $inputs .= wf_TextInput('edittariffname', __('Tariff name'), $this->allTariffs[$tariffId]['name'], true, '20');
  576. $inputs .= wf_TextInput('edittariffdesc', __('Description'), $this->allTariffs[$tariffId]['description'], true, '20');
  577. $inputs .= wf_TextInput('edittarifffee', __('Fee'), $this->allTariffs[$tariffId]['fee'], true, '5');
  578. $inputs .= wf_TextInput('edittariffserviceid', __('Service ID'), $this->allTariffs[$tariffId]['serviceid'], true, '10');
  579. $inputs .= '<hr>';
  580. $inputs .= wf_Submit(__('Save'));
  581. $result = wf_Form('', 'POST', $inputs, 'glamour');
  582. return ($result);
  583. }
  584. /**
  585. * Catches tariff editing form data
  586. *
  587. * @return void/string on error
  588. */
  589. public function updateTariff() {
  590. $result = '';
  591. if (wf_CheckPost(array(
  592. 'edittariffid',
  593. 'edittariffname',
  594. 'edittariffdesc',
  595. 'edittariffserviceid'
  596. ))) {
  597. $tariffId = vf($_POST['edittariffid'], 3);
  598. if (isset($this->allTariffs[$tariffId])) {
  599. $where = " WHERE `id`='" . $tariffId . "';";
  600. simple_update_field(self::TABLE_TARIFFS, 'name', $_POST['edittariffname'], $where);
  601. simple_update_field(self::TABLE_TARIFFS, 'description', $_POST['edittariffdesc'], $where);
  602. simple_update_field(self::TABLE_TARIFFS, 'fee', $_POST['edittarifffee'], $where);
  603. simple_update_field(self::TABLE_TARIFFS, 'serviceid', $_POST['edittariffserviceid'], $where);
  604. log_register('TRINITY TARIFF EDIT [' . $tariffId . '] `' . $_POST['edittariffname'] . '` FEE `' . $_POST['edittarifffee'] . '`');
  605. }
  606. } else {
  607. $result = $this->messages->getStyledMessage(__('No all of required fields is filled'), 'error');
  608. }
  609. return ($result);
  610. }
  611. /**
  612. * Creates new tariff in database
  613. *
  614. * @return void/string on error
  615. */
  616. public function createTariff() {
  617. $result = '';
  618. if (wf_CheckPost(array(
  619. 'newtariffname',
  620. 'newtariffdesc',
  621. 'newtariffserviceid'
  622. ))) {
  623. $nameF = mysql_real_escape_string($_POST['newtariffname']);
  624. $feeF = mysql_real_escape_string($_POST['newtarifffee']);
  625. $desc = mysql_real_escape_string($_POST['newtariffdesc']);
  626. $serviceidF = mysql_real_escape_string($_POST['newtariffserviceid']);
  627. if (zb_checkMoney($feeF)) {
  628. $query = "INSERT INTO `" . self::TABLE_TARIFFS . "` (`id`,`name`,`description`,`fee`,`serviceid`) VALUES ";
  629. $query .= "(NULL,'" . $nameF . "','" . $desc . "','" . $feeF . "','" . $serviceidF . "')";
  630. nr_query($query);
  631. $newId = simple_get_lastid(self::TABLE_TARIFFS);
  632. log_register('TRINITY TARIFF CREATE [' . $newId . '] `' . $_POST['newtariffname'] . '` FEE `' . $_POST['newtarifffee'] . '`');
  633. } else {
  634. $result = $this->messages->getStyledMessage(__('Wrong format of a sum of money to pay'), 'error');
  635. }
  636. } else {
  637. $result = $this->messages->getStyledMessage(__('No all of required fields is filled'), 'error');
  638. }
  639. return ($result);
  640. }
  641. /**
  642. * Checks is tariff used by some users
  643. *
  644. * @param int $tariffid
  645. *
  646. * @return bool
  647. */
  648. protected function tariffProtected($tariffid) {
  649. $result = false;
  650. if (!empty($this->allSubscribers)) {
  651. foreach ($this->allSubscribers as $subscriber) {
  652. if ($subscriber['tariffid'] == $tariffid) {
  653. $result = true;
  654. break;
  655. }
  656. }
  657. }
  658. return ($result);
  659. }
  660. /**
  661. * Deletes existing tariff from database
  662. *
  663. * @param int $tariffId
  664. *
  665. * @return void/string
  666. */
  667. public function deleteTariff($tariffId) {
  668. $tariffId = vf($tariffId, 3);
  669. $result = '';
  670. if (isset($this->allTariffs[$tariffId])) {
  671. if (!$this->tariffProtected($tariffId)) {
  672. $query = "DELETE from `" . self::TABLE_TARIFFS . "` WHERE `id`='" . $tariffId . "';";
  673. nr_query($query);
  674. log_register('TRINITYTV TARIFF DELETE [' . $tariffId . ']');
  675. } else {
  676. $result = $this->messages->getStyledMessage(__('Tariff is used by some users'), 'error');
  677. }
  678. } else {
  679. $result = $this->messages->getStyledMessage(__('Not existing item'), 'error');
  680. }
  681. return ($result);
  682. }
  683. /**
  684. * Inits API object for further usage
  685. */
  686. protected function initApi() {
  687. $partnerId = '';
  688. $salt = '';
  689. if (!empty($this->altCfg['TRINITYTV_PARTNER_ID'])) {
  690. $partnerId = $this->altCfg['TRINITYTV_PARTNER_ID'];
  691. }
  692. if (!empty($this->altCfg['TRINITYTV_SALT'])) {
  693. $salt = $this->altCfg['TRINITYTV_SALT'];
  694. }
  695. if (isset($this->altCfg['TRINITYTV_DEBUG'])) {
  696. $debug = $this->altCfg['TRINITYTV_DEBUG'];
  697. } else {
  698. $debug = false;
  699. }
  700. $this->api = new TrinityTvApi($partnerId, $salt, '', $debug);
  701. }
  702. /**
  703. * Inits system message helper
  704. *
  705. * @return void
  706. */
  707. protected function initMessages() {
  708. $this->messages = new UbillingMessageHelper();
  709. }
  710. /**
  711. * Loads existing tariffs from database
  712. *
  713. * @return void
  714. */
  715. protected function loadTariffs() {
  716. $query = "SELECT * from " . self::TABLE_TARIFFS;
  717. $tariffs = simple_queryall($query);
  718. if (!empty($tariffs)) {
  719. foreach ($tariffs as $tariff) {
  720. $this->allTariffs[$tariff['id']] = $tariff;
  721. }
  722. }
  723. }
  724. /**
  725. * Loads existing suspended users
  726. *
  727. * @return void
  728. */
  729. protected function loadSuspended() {
  730. $query = "SELECT * from " . self::TABLE_SUSPENDS;
  731. $suspends = simple_queryall($query);
  732. if (!empty($suspends)) {
  733. foreach ($suspends as $suspend) {
  734. $this->suspended[$suspend['login']] = $suspend['id'];
  735. }
  736. }
  737. }
  738. /**
  739. * Loads existing subscribers data
  740. *
  741. * @return void
  742. */
  743. protected function loadSubscribers() {
  744. $query = "SELECT * from " . self::TABLE_SUBS;
  745. $subscribers = simple_queryall($query);
  746. if (!empty($subscribers)) {
  747. foreach ($subscribers as $subscriber) {
  748. $this->allSubscribers[$subscriber['id']] = $subscriber;
  749. }
  750. }
  751. }
  752. /**
  753. * Loads internet users data into protected property for further usage
  754. *
  755. * @return void
  756. */
  757. protected function loadUsers() {
  758. $this->allUsers = zb_UserGetAllData();
  759. }
  760. /**
  761. * Renders form to manual tariff changing
  762. *
  763. * @param int $subscriberId
  764. *
  765. * @return string
  766. */
  767. protected function renderManualTariffForm($subscriberId) {
  768. $subscriberId = vf($subscriberId, 3);
  769. $result = '';
  770. $subcribersData = @$this->allSubscribers[$subscriberId];
  771. $baseTariffs = array();
  772. if (!empty($this->allTariffs)) {
  773. foreach ($this->allTariffs as $tariff) {
  774. $baseTariffs[$tariff['id']] = $tariff['name'];
  775. }
  776. }
  777. $inputs = '';
  778. $inputs .= wf_Selector('changebasetariff', $baseTariffs, __('Tariff'), $subcribersData['tariffid'], true);
  779. $inputs .= wf_CheckInput('dontchargefeenow', __('Dont charge fee now'), true, true);
  780. $inputs .= wf_tag('br');
  781. $inputs .= wf_Submit(__('Save'));
  782. $result .= wf_Form(self::URL_SUBSCRIBER . $subscriberId, 'POST', $inputs, 'glamour');
  783. return ($result);
  784. }
  785. /**
  786. * Performs editing of user tariffs
  787. *
  788. * @param int $subscriberId
  789. * @param int $tariffId
  790. * @param bool $chargeFee
  791. *
  792. * @return void
  793. */
  794. public function changeTariffs($subscriberId, $tariffId, $chargeFee = true) {
  795. $tariffId = vf($tariffId, 3);
  796. $subscriberId = vf($subscriberId, 3);
  797. // Get the tariff properties
  798. $tariff = $this->getTariffData($tariffId);
  799. if (isset($this->allSubscribers[$subscriberId])) {
  800. $userLogin = $this->allSubscribers[$subscriberId]['login'];
  801. $currentTariffId = $this->allSubscribers[$subscriberId]['tariffid'];
  802. // Change the tariff if needed
  803. if ($currentTariffId != $tariffId) {
  804. // Create a subscription on the Trinity
  805. $response = $this->api->createUser($subscriberId, $tariff['serviceid']);
  806. if (isset($response->result) AND $response->result == 'success') {
  807. simple_update_field(self::TABLE_SUBS, 'tariffid', $tariffId, "WHERE `id`='" . $subscriberId . "'");
  808. log_register('TRINITYTV SET TARIFF [' . $tariffId . '] (' . $userLogin . ') AS [' . $subscriberId . ']');
  809. }
  810. }
  811. //do something awful with user balance
  812. if ($chargeFee) {
  813. // Calculating new tariff fee
  814. $tariffFee = $tariff['fee'];
  815. $currentDayOfMonth = date("d");
  816. $currentMonthDayCount = date("t");
  817. $tariffFeeDaily = $this->getDaylyFee($tariffFee);
  818. $tariffFee = ($currentMonthDayCount - $currentDayOfMonth) * $tariffFeeDaily;
  819. // Charging fee to the end of month
  820. zb_CashAdd($userLogin, '-' . $tariffFee, 'add', 1, 'TRINITYTV:' . $tariffId);
  821. log_register('TRINITYTV FEE (' . $userLogin . ') - ' . $tariffFee);
  822. } else {
  823. //just log this change as zero charge
  824. zb_CashAdd($userLogin, '-0', 'add', 1, 'TRINITYTV:' . $tariffId);
  825. log_register('TRINITYTV FEE (' . $userLogin . ') - 0');
  826. }
  827. // Trying activate user if he is not active now.
  828. if ($this->allSubscribers[$subscriberId]['active'] != 1) {
  829. simple_update_field(self::TABLE_SUBS, 'active', '1', "WHERE `id`='" . $subscriberId . "'");
  830. log_register('TRINITYTV RESURRECT USER (' . $userLogin . ') AS [' . $subscriberId . ']');
  831. }
  832. }
  833. }
  834. /**
  835. * Renders manual device assign form
  836. *
  837. * @return string
  838. */
  839. protected function renderDeviceAddForm($subscriberId) {
  840. $result = '';
  841. $inputs = wf_HiddenInput('device', 'true');
  842. $inputs .= wf_HiddenInput('subscriberid', $subscriberId);
  843. $userlogin = $this->getSubscriberLogin($subscriberId);
  844. $inputs .= wf_HiddenInput('userlogin', $userlogin);
  845. $inputs .= wf_TextInput('mac', __('MAC'), '', true, 20, 'mac');
  846. $inputs .= wf_Submit(__('Assign'));
  847. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  848. return ($result);
  849. }
  850. /**
  851. * Renders manual device assign form
  852. *
  853. * @return string
  854. */
  855. protected function renderDeviceByCodeAddForm($subscriberId) {
  856. $result = '';
  857. $inputs = wf_HiddenInput('manualassigndevice', 'true');
  858. $inputs .= wf_HiddenInput('subscriberid', $subscriberId);
  859. $userlogin = $this->getSubscriberLogin($subscriberId);
  860. $inputs .= wf_HiddenInput('userlogin', $userlogin);
  861. $inputs .= wf_TextInput('code', __('Code'), '', true, 20);
  862. $inputs .= wf_Submit(__('Assign'));
  863. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  864. return ($result);
  865. }
  866. /**
  867. * Check exists device by MAC
  868. * @param $mac
  869. * @return bool
  870. */
  871. public function existsDevice($mac) {
  872. $mac = strtoupper($mac);
  873. $query = "SELECT * from `" . self::TABLE_DEVICES . "` WHERE `mac` ='" . $mac . "'";
  874. $devices = simple_queryall($query);
  875. if (!empty($devices)) {
  876. return true;
  877. }
  878. return false;
  879. }
  880. /**
  881. * Assigns some device by code to some subscriber
  882. *
  883. * @param $userLogin
  884. * @param $code
  885. * @return string
  886. */
  887. public function addDeviceByCode($userLogin, $code) {
  888. $result = '';
  889. $subscriberId = $this->getSubscriberId($userLogin);
  890. $subscriberId = vf($subscriberId, 3); //int
  891. $code = vf(strtoupper($code)); //alphanumeric
  892. if (isset($this->allSubscribers[$subscriberId])) {
  893. $response = $this->api->addCodeMacDevice($subscriberId, $code);
  894. if (isset($response->result) AND $response->result == 'success') {
  895. @$mac = vf(strtoupper($response->mac)); //alphanumeric
  896. $query = "INSERT INTO `" . self::TABLE_DEVICES . "` (`login`, `subscriber_id`, `mac`, `created_at`) VALUES ";
  897. $query .= "('" . $this->allSubscribers[$subscriberId]['login'] . "', '" . $subscriberId . "','" . $mac . "', NOW() )";
  898. nr_query($query);
  899. $userLogin = $this->getSubscriberLogin($subscriberId);
  900. log_register('TRINITYTV DEVICE ADD `' . $mac . '` FOR (' . $userLogin . ') AS [' . $subscriberId . ']');
  901. } else {
  902. $result = __('Strange exeption') . ': ' . @$response->result;
  903. }
  904. } else {
  905. $result .= __('Something went wrong') . ': ' . __('User not exists');
  906. }
  907. return ($result);
  908. }
  909. /**
  910. * Assigns some device uniq to some subscriber
  911. *
  912. * @param $userLogin
  913. * @param $mac
  914. * @return string
  915. */
  916. public function addDevice($userLogin, $mac) {
  917. $result = '';
  918. $subscriberId = $this->getSubscriberId($userLogin);
  919. $subscriberId = vf($subscriberId, 3); //int
  920. $mac = vf(strtoupper($mac)); //alphanumeric
  921. if (isset($this->allSubscribers[$subscriberId])) {
  922. $existsDevice = $this->existsDevice($mac);
  923. if (!$existsDevice) {
  924. $response = $this->api->addMacDevice($subscriberId, $mac);
  925. if (isset($response->result) AND $response->result == 'success') {
  926. $query = "INSERT INTO `" . self::TABLE_DEVICES . "` (`login`, `subscriber_id`, `mac`, `created_at`) VALUES ";
  927. $query .= "('" . $this->allSubscribers[$subscriberId]['login'] . "', '" . $subscriberId . "','" . $mac . "', NOW() )";
  928. nr_query($query);
  929. $userLogin = $this->getSubscriberLogin($subscriberId);
  930. log_register('TRINITYTV DEVICE ADD `' . $mac . '` FOR (' . $userLogin . ') AS [' . $subscriberId . ']');
  931. } else {
  932. $result .= __('Strange exeption') . ': ' . @$response->result;
  933. }
  934. } else {
  935. $result .= __('Something went wrong') . ': ' . __('Device exists');
  936. }
  937. } else {
  938. $result .= __('Something went wrong') . ': ' . __('User not exists');
  939. }
  940. return ($result);
  941. }
  942. /**
  943. * Renders profile controls
  944. *
  945. * @return string
  946. */
  947. protected function renderProfileControls($subscriberId) {
  948. $subscriberId = vf($subscriberId, 3);
  949. $result = wf_tag('br');
  950. $result .= wf_Link(self::URL_ME . '&subscriberid=' . $subscriberId . '&blockuser=true', web_bool_led(0) . ' ' . __('Block user'), false, 'ubButton');
  951. $result .= wf_Link(self::URL_ME . '&subscriberid=' . $subscriberId . '&unblockuser=true', web_bool_led(1) . ' ' . __('Unblock user'), false, 'ubButton');
  952. $result .= wf_modalAuto(web_edit_icon() . ' ' . __('Edit tariff'), __('Edit tariff'), $this->renderManualTariffForm($subscriberId), 'ubButton');
  953. return ($result);
  954. }
  955. /**
  956. * Sets user local and remote profile as active or not
  957. *
  958. * @param int $subscriberId
  959. * @param bool $state
  960. *
  961. * @return void
  962. */
  963. public function setSubscriberActive($subscriberId, $state) {
  964. $subscriberId = vf($subscriberId, 3);
  965. if (isset($this->allSubscribers[$subscriberId])) {
  966. $userLogin = $this->allSubscribers[$subscriberId]['login'];
  967. $where = "WHERE `id`='" . $subscriberId . "'";
  968. if ($state) {
  969. // Разблокируем
  970. $this->api->subscription($subscriberId, 'resume');
  971. simple_update_field(self::TABLE_SUBS, 'active', '1', $where);
  972. log_register('TRINITYTV UNBLOCK USER (' . $userLogin . ') AS [' . $subscriberId . ']');
  973. $this->suspendUser($userLogin, false);
  974. } else {
  975. // Блокируем
  976. $this->api->subscription($subscriberId, 'suspend');
  977. simple_update_field(self::TABLE_SUBS, 'active', '0', $where);
  978. log_register('TRINITYTV BLOCK USER (' . $userLogin . ') AS [' . $subscriberId . ']');
  979. $this->suspendUser($userLogin, true);
  980. }
  981. }
  982. }
  983. /**
  984. * Sets user as suspended or not to preventing his automatic ressurection
  985. *
  986. * @param string $userLogin
  987. * @param bool $state
  988. *
  989. * @return void
  990. */
  991. protected function suspendUser($userLogin, $state) {
  992. $login_f = mysql_real_escape_string($userLogin);
  993. $subscriberId = $this->getSubscriberId($userLogin);
  994. if ($state) {
  995. $query = "INSERT INTO " . self::TABLE_SUSPENDS . " (`id`,`login`) VALUES (NULL,'" . $login_f . "');";
  996. nr_query($query);
  997. log_register('TRINITYTV SUSPEND USER (' . $userLogin . ') AS [' . $subscriberId . ']');
  998. } else {
  999. $query = "DELETE FROM " . self::TABLE_SUSPENDS . " WHERE `login`='" . $login_f . "'";
  1000. nr_query($query);
  1001. log_register('TRINITYTV UNSUSPEND USER (' . $userLogin . ') AS [' . $subscriberId . ']');
  1002. }
  1003. }
  1004. /**
  1005. * Renders some user profile info
  1006. *
  1007. * @param int $subscriberId
  1008. *
  1009. * @return string
  1010. */
  1011. public function renderUserInfo($subscriberId) {
  1012. $subscriberId = vf($subscriberId, 3);
  1013. $result = '';
  1014. $subscriber = @$this->allSubscribers[$subscriberId];
  1015. if (!empty($subscriber)) {
  1016. $remoteServiceData = $this->api->subscriptionInfo($subscriberId);
  1017. $result .= wf_tag('b') . __('Local profile') . wf_tag('b', true) . wf_tag('br');
  1018. $rows = '';
  1019. $cells = wf_TableCell(__('ID'), '', 'row2');
  1020. $cells .= wf_TableCell($subscriber['id']);
  1021. $rows .= wf_TableRow($cells, 'row3');
  1022. $cells = wf_TableCell(__('Real Name'), '', 'row2');
  1023. $cells .= wf_TableCell($this->allUsers[$subscriber['login']]['realname']);
  1024. $rows .= wf_TableRow($cells, 'row3');
  1025. $cells = wf_TableCell(__('Login'), '', 'row2');
  1026. $cells .= wf_TableCell($subscriber['login']);
  1027. $rows .= wf_TableRow($cells, 'row3');
  1028. $cells = wf_TableCell(__('Full address'), '', 'row2');
  1029. $userAddress = @$this->allUsers[$subscriber['login']]['fulladress'];
  1030. $userLink = wf_Link(self::URL_PROFILE . $subscriber['login'], web_profile_icon() . ' ' . $userAddress);
  1031. $cells .= wf_TableCell($userLink);
  1032. $rows .= wf_TableRow($cells, 'row3');
  1033. $cells = wf_TableCell(__('Contract') . ' ' . __('Trinity'), '', 'row2');
  1034. $cells .= wf_TableCell($subscriber['contracttrinity']);
  1035. $rows .= wf_TableRow($cells, 'row3');
  1036. $cells = wf_TableCell(__('Tariff'), '', 'row2');
  1037. $cells .= wf_TableCell($this->getTariffName($subscriber['tariffid']));
  1038. $rows .= wf_TableRow($cells, 'row3');
  1039. $cells = wf_TableCell(__('Date'), '', 'row2');
  1040. $cells .= wf_TableCell($this->getTariffName($subscriber['actdate']));
  1041. $rows .= wf_TableRow($cells, 'row3');
  1042. $remoteServiceStatus = $remoteServiceData->subscriptions->subscrstatus;
  1043. $cells = wf_TableCell(__('Status') . ' ' . __('local'), '', 'row2');
  1044. $cells .= wf_TableCell(web_bool_led($subscriber['active']));
  1045. $rows .= wf_TableRow($cells, 'row3');
  1046. $cells = wf_TableCell(__('Status') . ' ' . __('Trinity'), '', 'row2');
  1047. $cells .= wf_TableCell($remoteServiceStatus);
  1048. $rows .= wf_TableRow($cells, 'row3');
  1049. $result .= wf_TableBody($rows, '100%', 0);
  1050. }
  1051. $result .= $this->renderProfileControls($subscriberId);
  1052. return ($result);
  1053. }
  1054. /**
  1055. * Renders default module controls
  1056. *
  1057. * @return string
  1058. */
  1059. public function renderPanel() {
  1060. $result = '';
  1061. $result .= wf_Link(self::URL_ME . '&' . self::URL_SUBS, wf_img('skins/ukv/users.png') . ' ' . __('Subscriptions'), false, 'ubButton') . ' ';
  1062. $result .= wf_Link(self::URL_ME . '&' . self::URL_TARIFFS, wf_img('skins/ukv/dollar.png') . ' ' . __('Tariffs'), false, 'ubButton') . ' ';
  1063. $result .= wf_Link(self::URL_ME . '&' . self::URL_DEVICES, wf_img('skins/switch_models.png') . ' ' . __('Devices'), false, 'ubButton') . ' ';
  1064. $result .= wf_Link(self::URL_ME . '&' . self::URL_REPORTS, wf_img('skins/ukv/report.png') . ' ' . __('Reports'), false, 'ubButton') . ' ';
  1065. return ($result);
  1066. }
  1067. /**
  1068. * Renders new subscriber registration form
  1069. *
  1070. * @return string
  1071. */
  1072. protected function renderUserRegisterForm() {
  1073. $baseTariffs = array();
  1074. foreach ($this->allTariffs as $tariff) {
  1075. $baseTariffs[$tariff['id']] = $tariff['name'];
  1076. }
  1077. $result = '';
  1078. $loginPreset = (wf_CheckGet(array('username'))) ? $_GET['username'] : '';
  1079. $inputs = wf_HiddenInput('manualregister', 'true');
  1080. $inputs .= wf_TextInput('manualregisterlogin', __('Login'), $loginPreset, true, '15');
  1081. $inputs .= wf_Selector('manualregistertariff', $baseTariffs, __('Tariff'), '', true);
  1082. $inputs .= wf_Submit(__('Create'));
  1083. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  1084. return ($result);
  1085. }
  1086. /**
  1087. * Renders tariff editing form
  1088. *
  1089. * @param int $tariffId
  1090. *
  1091. * @return string
  1092. */
  1093. protected function renderTariffEditForm($tariffId) {
  1094. $tariffId = vf($tariffId, 3);
  1095. $result = '';
  1096. if (isset($this->allTariffs[$tariffId])) {
  1097. $tariffData = $this->allTariffs[$tariffId];
  1098. if (!empty($tariffData)) {
  1099. $tariffsTypes = array(
  1100. 'base' => __('Base'),
  1101. 'bundle' => __('Bundle'),
  1102. 'promo' => __('Promo')
  1103. );
  1104. $inputs = wf_HiddenInput('edittariffid', $tariffId);
  1105. $inputs .= wf_TextInput('edittariffname', __('Tariff name'), $tariffData['tariffname'], true, 25);
  1106. $inputs .= wf_Selector('edittarifftype', $tariffsTypes, __('Type'), $tariffData['type'], true);
  1107. $inputs .= wf_TextInput('edittarifffee', __('Fee'), $tariffData['fee'], true, 3, 'finance');
  1108. $inputs .= wf_Submit(__('Save'));
  1109. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  1110. }
  1111. }
  1112. return ($result);
  1113. }
  1114. /**
  1115. * Renders list of available tariffs
  1116. *
  1117. * @return string
  1118. */
  1119. public function renderTariffsList() {
  1120. $result = '';
  1121. if (!empty($this->allTariffs)) {
  1122. $cells = wf_TableCell(__('ID'));
  1123. $cells .= wf_TableCell(__('Tariff') . ' ' . __('Code'));
  1124. $cells .= wf_TableCell(__('Tariff name'));
  1125. $cells .= wf_TableCell(__('Type'));
  1126. $cells .= wf_TableCell(__('Fee'));
  1127. $cells .= wf_TableCell(__('Actions'));
  1128. $rows = wf_TableRow($cells, 'row1');
  1129. foreach ($this->allTariffs as $io => $each) {
  1130. $cells = wf_TableCell($each['id']);
  1131. $cells .= wf_TableCell($each['tariffid']);
  1132. $cells .= wf_TableCell($each['tariffname']);
  1133. $cells .= wf_TableCell(__($each['type']));
  1134. $cells .= wf_TableCell($each['fee']);
  1135. $actLinks = wf_JSAlert(self::URL_ME . '&tariffs=true&deleteid=' . $each['id'], web_delete_icon(), $this->messages->getDeleteAlert()) . ' ';
  1136. $actLinks .= wf_modalAuto(web_edit_icon(), __('Edit'), $this->renderTariffEditForm($each['id'])) . ' ';
  1137. $cells .= wf_TableCell($actLinks);
  1138. $rows .= wf_TableRow($cells, 'row5');
  1139. }
  1140. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  1141. } else {
  1142. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'warning');
  1143. }
  1144. return ($result);
  1145. }
  1146. /**
  1147. * Deletes device assigned to some subscriberid
  1148. *
  1149. * @param string $userLogin
  1150. * @param string $mac
  1151. *
  1152. * @return void
  1153. */
  1154. public function deleteDevice($userLogin, $mac) {
  1155. $result = '';
  1156. $subscriberId = $this->getSubscriberId($userLogin);
  1157. $deviceId = $this->getDeviceId($subscriberId, $mac);
  1158. $deviceId = vf($deviceId, 3);
  1159. $allDevices = $this->getDevices();
  1160. if (isset($allDevices[$deviceId])) {
  1161. // Delete a subscription on the Trinity
  1162. $response = $this->api->deleteMacDevice($allDevices[$deviceId]['subscriber_id'], $allDevices[$deviceId]['mac']);
  1163. $query = "DELETE from `" . self::TABLE_DEVICES . "` WHERE `id`='" . $deviceId . "';";
  1164. nr_query($query);
  1165. log_register('TRINITYTV DEVICE DELETE `' . $allDevices[$deviceId]['mac'] . '` FOR (' . $userLogin . ')');
  1166. if (isset($response->result) AND $response->result == 'success') {
  1167. } else {
  1168. $result = __('Something went wrong') . ": Trinity response " . @$response->result;
  1169. }
  1170. } else {
  1171. $result = __('Not existing item');
  1172. }
  1173. return ($result);
  1174. }
  1175. /**
  1176. * Deletes device assigned to some subscriberid
  1177. *
  1178. * @param string $deviceId
  1179. *
  1180. * @return void
  1181. */
  1182. public function deleteDeviceById($deviceId) {
  1183. $result = '';
  1184. $deviceId = vf($deviceId, 3);
  1185. $allDevices = $this->getDevices();
  1186. if (isset($allDevices[$deviceId])) {
  1187. $deviceData = $allDevices[$deviceId];
  1188. // Delete a subscription on the Trinity
  1189. $response = $this->api->deleteMacDevice($allDevices[$deviceId]['subscriber_id'], $allDevices[$deviceId]['mac']);
  1190. $query = "DELETE from `" . self::TABLE_DEVICES . "` WHERE `id`='" . $deviceId . "';";
  1191. nr_query($query);
  1192. log_register('TRINITYTV DEVICE DELETE `' . $allDevices[$deviceId]['mac'] . '` FOR (' . $deviceData['login'] . ')');
  1193. if (isset($response->result) AND $response->result == 'success') {
  1194. } else {
  1195. $result = __('Something went wrong') . ": Trinity response " . @$response->result;
  1196. }
  1197. } else {
  1198. $result = __('Not existing item');
  1199. }
  1200. return ($result);
  1201. }
  1202. /**
  1203. * Deletes some device by its ID if it assigned to specified login (for remoteapi callback)
  1204. *
  1205. * @param int $deviceId
  1206. * @param string $userLogin
  1207. *
  1208. * @return void/string on error
  1209. */
  1210. public function deleteDeviceByIdProtected($deviceId, $userLogin) {
  1211. $result = '';
  1212. $deviceId = ubRouting::filters($deviceId, 'int');
  1213. $allDevices = $this->getDevices();
  1214. //device exists?
  1215. if (isset($allDevices[$deviceId])) {
  1216. $deviceData = $allDevices[$deviceId];
  1217. //have correct assign for user that requested deletion?
  1218. if ($deviceData['login'] == $userLogin) {
  1219. $result .= $this->deleteDeviceById($deviceId);
  1220. } else {
  1221. //masking actual devices quantity and assigns from user
  1222. $result .= __('No such device');
  1223. }
  1224. } else {
  1225. $result .= __('No such device');
  1226. }
  1227. return($result);
  1228. }
  1229. /**
  1230. * Deletes subscription
  1231. *
  1232. * @param string $login
  1233. *
  1234. * @return string
  1235. */
  1236. public function deleteSubscribtion($login) {
  1237. $result = '';
  1238. if (isset($this->allUsers[$login])) {
  1239. $subscriberId = $this->getSubscriberId($login);
  1240. if (!empty($subscriberId)) {
  1241. $this->setSubscriberActive($subscriberId, false);
  1242. }
  1243. } else {
  1244. $result = __('Something went wrong') . ': ' . __('User not exist') . ' - ' . $login;
  1245. log_register('TRINITYTV FAIL SUBSCRIBER REGISTER (' . $login . ') NOLOGIN');
  1246. }
  1247. return ($result);
  1248. }
  1249. /**
  1250. * Creates new user profile
  1251. *
  1252. * @param $login
  1253. * @param $tariffId
  1254. * @return string
  1255. */
  1256. public function createSubscribtion($login, $tariffId) {
  1257. $tariffId = vf($tariffId, 3);
  1258. $login_f = mysql_real_escape_string($login);
  1259. $curdate = curdatetime();
  1260. $result = '';
  1261. if (isset($this->allUsers[$login])) {
  1262. if (isset($this->allTariffs[$tariffId])) {
  1263. $subscriberId = $this->getSubscriberId($login);
  1264. //not existing subscriber
  1265. if (empty($subscriberId)) {
  1266. //getting new tariff data
  1267. $tariff = $this->getTariffData($tariffId);
  1268. //and tariff exists
  1269. if (!empty($tariff)) {
  1270. // Create Subscriber In Ubilling
  1271. $query = "INSERT INTO `" . self::TABLE_SUBS . "` (`login`,`tariffid`,`actdate`,`active`) VALUES ";
  1272. $query .= "('" . $login_f . "','" . $tariffId . "','" . $curdate . "','1');";
  1273. nr_query($query);
  1274. $subscriberID = simple_get_lastid(self::TABLE_SUBS);
  1275. // Create a subscription on the Trinity
  1276. $response = $this->api->createUser($subscriberID, $tariff['serviceid']);
  1277. if (isset($response->result) AND $response->result == 'success') {
  1278. $contractID = $response->contracttrinity;
  1279. //Push contracttrinity to DB
  1280. simple_update_field(self::TABLE_SUBS, 'contracttrinity', $contractID, 'WHERE `id`=' . $subscriberID);
  1281. log_register('TRINITYTV SUBSCRIBER REGISTER (' . $login . ') AS [' . $subscriberID . ']');
  1282. $this->loadSubscribers();
  1283. }
  1284. // charge fee to the end of month
  1285. $tariffFee = $tariff['fee'];
  1286. $currentDayOfMonth = date("d");
  1287. $currentMonthDayCount = date("t");
  1288. $tariffFeeDaily = $this->getDaylyFee($tariffFee);
  1289. $tariffFee = ($currentMonthDayCount - $currentDayOfMonth) * $tariffFeeDaily;
  1290. zb_CashAdd($login, '-' . $tariffFee, 'add', 1, 'TRINITYTV:' . $tariffId);
  1291. log_register('TRINITYTV FEE (' . $login . ') -' . $tariffFee);
  1292. } else {
  1293. $result .= 'Wrong tariff';
  1294. }
  1295. } else {
  1296. // Change tariff AND activate
  1297. $this->changeTariffs($subscriberId, $tariffId);
  1298. }
  1299. } else {
  1300. $result = 'Wrong tariff';
  1301. }
  1302. } else {
  1303. $result .= __('Something went wrong') . ': ' . __('User not exist') . ' - ' . $login;
  1304. log_register('TRINITYTV FAIL SUBSCRIBER REGISTER (' . $login . ') NOLOGIN');
  1305. }
  1306. return ($result);
  1307. }
  1308. /**
  1309. * Returns valid dayly fee for some tariff/month
  1310. *
  1311. * @param float $tariffFee
  1312. *
  1313. * @return float
  1314. */
  1315. protected function getDaylyFee($tariffFee) {
  1316. $monthDays = date("t");
  1317. $result = round(($tariffFee / $monthDays), 2);
  1318. return ($result);
  1319. }
  1320. /**
  1321. * Charges fee for some tariff
  1322. *
  1323. * @param string $userLogin
  1324. * @param int $tariffId
  1325. *
  1326. * @return void
  1327. */
  1328. protected function chargeFee($userLogin, $tariffId) {
  1329. $tariffData = $this->getTariffData($tariffId);
  1330. $subscriberID = $this->getSubscriberId($userLogin);
  1331. $tariffFee = $tariffData['fee'];
  1332. zb_CashAdd($userLogin, '-' . $tariffFee, 'add', 1, 'TRINITYTV:' . $tariffId);
  1333. log_register('TRINITYTV CHARGE TARIFF [' . $tariffId . '] FEE `' . $tariffFee . '` FOR (' . $userLogin . ') AS [' . $subscriberID . ']');
  1334. }
  1335. /**
  1336. * Renders default subscriptions report
  1337. *
  1338. * @return string
  1339. */
  1340. public function renderSubscribtionsReportMonthly() {
  1341. $result = '';
  1342. $selectedMonth = (wf_CheckPost(array('monthsel'))) ? $_POST['monthsel'] : date("m");
  1343. $selectedYear = (wf_CheckPost(array('yearsel'))) ? $_POST['yearsel'] : date("Y");
  1344. $inputs = wf_YearSelectorPreset('yearsel', __('Year'), false, $selectedYear) . ' ';
  1345. $inputs .= wf_MonthSelector('monthsel', __('Month'), $selectedMonth, false) . ' ';
  1346. $inputs .= wf_Submit(__('Show'));
  1347. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  1348. $curYear = (wf_CheckPost(array('yearsel'))) ? vf($_POST['yearsel'], 3) : curyear();
  1349. $curMonth = (wf_CheckPost(array('monthsel'))) ? vf($_POST['monthsel'], 3) : date("m");
  1350. $query = "SELECT * from `payments` WHERE `date` LIKE '" . $curYear . "-" . $curMonth . "%' AND `note` LIKE 'TRINITYTV:%';";
  1351. $payments = simple_queryall($query);
  1352. $tmpArr = array();
  1353. if (!empty($payments)) {
  1354. foreach ($payments as $payment) {
  1355. $tariffId = explode(':', $payment['note']);
  1356. $tariffId = $tariffId[1];
  1357. if (isset($tmpArr[$tariffId])) {
  1358. $tmpArr[$tariffId]['summ'] = $tmpArr[$tariffId]['summ'] + abs($payment['summ']);
  1359. $tmpArr[$tariffId]['count'] ++;
  1360. } else {
  1361. $tmpArr[$tariffId]['summ'] = abs($payment['summ']);
  1362. $tmpArr[$tariffId]['count'] = 1;
  1363. }
  1364. }
  1365. }
  1366. if (!empty($tmpArr)) {
  1367. $cells = wf_TableCell(__('Tariff'));
  1368. $cells .= wf_TableCell(__('Fee'));
  1369. $cells .= wf_TableCell(__('Users'));
  1370. $cells .= wf_TableCell(__('Total payments'));
  1371. $rows = wf_TableRow($cells, 'row1');
  1372. $totalUsers = 0;
  1373. $totalSumm = 0;
  1374. foreach ($tmpArr as $io => $each) {
  1375. $totalUsers = $totalUsers + $each['count'];
  1376. $totalSumm = $totalSumm + $each['summ'];
  1377. $cells = wf_TableCell(@$this->allTariffs[$io]['name']);
  1378. $cells .= wf_TableCell(@$this->allTariffs[$io]['fee']);
  1379. $cells .= wf_TableCell($each['count']);
  1380. $cells .= wf_TableCell($each['summ']);
  1381. $rows .= wf_TableRow($cells, 'row3');
  1382. }
  1383. $cells = wf_TableCell(wf_tag('b') . __('Total') . wf_tag('b', true));
  1384. $cells .= wf_TableCell('');
  1385. $cells .= wf_TableCell($totalUsers);
  1386. $cells .= wf_TableCell($totalSumm);
  1387. $rows .= wf_TableRow($cells, 'row2');
  1388. $result .= wf_TableBody($rows, '100%', 0, '');
  1389. } else {
  1390. $result .= $this->messages->getStyledMessage(__('Nothing found'), 'info');
  1391. }
  1392. return ($result);
  1393. }
  1394. /**
  1395. * Returns data container for active subscriptions
  1396. *
  1397. * @return string
  1398. */
  1399. public function renderSubscribtions() {
  1400. $result = '';
  1401. // Кнопка создать подписку
  1402. $result .= wf_modalAuto(wf_img('skins/ukv/add.png') . ' ' . __('Users registration'), __('Registration'), $this->renderUserRegisterForm(), 'ubButton');
  1403. $result .= wf_delimiter();
  1404. if ($this->renderDevices) {
  1405. $columns = array(
  1406. __('ID'),
  1407. __('Login'),
  1408. __('Real Name'),
  1409. __('Full address'),
  1410. __('Cash'),
  1411. __('Current tariff'),
  1412. __('Date'),
  1413. __('Devices'),
  1414. __('Active'),
  1415. __('Actions')
  1416. );
  1417. } else {
  1418. $columns = array(
  1419. __('ID'),
  1420. __('Login'),
  1421. __('Real Name'),
  1422. __('Full address'),
  1423. __('Cash'),
  1424. __('Current tariff'),
  1425. __('Date'),
  1426. __('Active'),
  1427. __('Actions')
  1428. );
  1429. }
  1430. $orderOpts = '"order": [[ 0, "desc" ]]';
  1431. $result .= wf_JqDtLoader($columns, self::URL_ME . '&' . self::URL_SUBS . '&' . self::URL_AJSUBS, false, __('Subscriptions'), '100', $orderOpts);
  1432. return ($result);
  1433. }
  1434. /**
  1435. * Returns array of devices assigned for subscribers as subscriberId=>devcount
  1436. *
  1437. * @return array
  1438. */
  1439. protected function getUserDevicesCount() {
  1440. $result = array();
  1441. $allDevices = $this->getDevices();
  1442. if (!empty($allDevices)) {
  1443. foreach ($allDevices as $io => $each) {
  1444. if (isset($result[$each['subscriber_id']])) {
  1445. $result[$each['subscriber_id']] ++;
  1446. } else {
  1447. $result[$each['subscriber_id']] = 1;
  1448. }
  1449. }
  1450. }
  1451. return($result);
  1452. }
  1453. /**
  1454. * Renders ajax data subscriptions
  1455. *
  1456. * @return void
  1457. */
  1458. public function subscribtionsListAjax() {
  1459. $json = new wf_JqDtHelper();
  1460. if (!empty($this->allSubscribers)) {
  1461. if ($this->renderDevices) {
  1462. $devCounters = $this->getUserDevicesCount();
  1463. }
  1464. foreach ($this->allSubscribers as $subscriber) {
  1465. $userAddress = @$this->allUsers[$subscriber['login']]['fulladress'];
  1466. $userLink = wf_Link(self::URL_PROFILE . $subscriber['login'], web_profile_icon() . ' ' . $userAddress);
  1467. $actLinks = wf_Link(self::URL_ME . '&subscriberid=' . $subscriber['id'], web_edit_icon());
  1468. $data[] = $subscriber['id'];
  1469. $data[] = $subscriber['login'];
  1470. $data[] = @$this->allUsers[$subscriber['login']]['realname'];
  1471. $data[] = $userLink;
  1472. $data[] = @$this->allUsers[$subscriber['login']]['Cash'];
  1473. $data[] = $this->getTariffName($subscriber['tariffid']);
  1474. $data[] = $subscriber['actdate'];
  1475. if ($this->renderDevices) {
  1476. $devicesCount = 0;
  1477. if (isset($devCounters[$subscriber['id']])) {
  1478. $devicesCount = $devCounters[$subscriber['id']];
  1479. } else {
  1480. $devicesCount = 0;
  1481. }
  1482. $data[] = $devicesCount;
  1483. }
  1484. $data[] = web_bool_led($subscriber['active'], true);
  1485. $data[] = $actLinks;
  1486. $json->addRow($data);
  1487. unset($data);
  1488. }
  1489. }
  1490. $json->getJson();
  1491. }
  1492. /**
  1493. * Renders devices report container
  1494. *
  1495. * @return string
  1496. */
  1497. public function renderDevicesList() {
  1498. $result = '';
  1499. $columns = array('ID', 'MAC', 'Date', 'Real Name', 'Full address', 'Subscriptions');
  1500. $opts = '"order": [[ 0, "desc" ]]';
  1501. $result .= wf_JqDtLoader($columns, self::URL_ME . '&' . self::URL_DEVICES . '&' . self::URL_AJDEVS, false, __('Devices'), 100, $opts);
  1502. return($result);
  1503. }
  1504. /**
  1505. * Returns JSON data with available devices info
  1506. *
  1507. * @return void
  1508. */
  1509. public function devicesListAjax() {
  1510. $json = new wf_JqDtHelper();
  1511. $allDevices = $this->getDevices();
  1512. if (!empty($allDevices)) {
  1513. foreach ($allDevices as $io => $each) {
  1514. $data[] = $each['id'];
  1515. $data[] = $each['mac'];
  1516. $data[] = $each['created_at'];
  1517. $data[] = @$this->allUsers[$each['login']]['realname'];
  1518. $userLink = wf_Link(self::URL_PROFILE . $each['login'], web_profile_icon() . ' ' . @$this->allUsers[$each['login']]['fulladress']);
  1519. $data[] = $userLink;
  1520. $subLink = wf_Link(self::URL_SUBSCRIBER . $each['subscriber_id'], web_edit_icon());
  1521. $data[] = $subLink;
  1522. $json->addRow($data);
  1523. unset($data);
  1524. }
  1525. }
  1526. $json->getJson();
  1527. }
  1528. /**
  1529. * Charges all users tariffs fee, disables it when users go down
  1530. *
  1531. * @return void
  1532. */
  1533. public function subscriptionFeeProcessing() {
  1534. if (!empty($this->allSubscribers)) {
  1535. foreach ($this->allSubscribers as $subscriber) {
  1536. if ($subscriber['active']) {
  1537. if (isset($this->allUsers[$subscriber['login']])) {
  1538. if (!$this->allUsers[$subscriber['login']]['Passive']) {
  1539. if (!empty($subscriber['tariffid'])) {
  1540. $this->chargeFee($subscriber['login'], $subscriber['tariffid']);
  1541. }
  1542. }
  1543. }
  1544. }
  1545. }
  1546. //checking for debtors/freezed users and disabling it
  1547. $this->loadUsers();
  1548. foreach ($this->allSubscribers as $subscriber) {
  1549. if ($subscriber['active']) {
  1550. if (isset($this->allUsers[$subscriber['login']])) {
  1551. $userData = $this->allUsers[$subscriber['login']];
  1552. if ($userData['Passive']) {
  1553. //user is frozen by some reason - need to disable him
  1554. $this->api->subscription($subscriber['id'], 'suspend');
  1555. simple_update_field(self::TABLE_SUBS, 'active', '0', "WHERE `id`='" . $subscriber['id'] . "'");
  1556. log_register('TRINITYTV BLOCK FROZEN USER (' . $subscriber['login'] . ') AS [' . $subscriber['id'] . ']');
  1557. }
  1558. //if user have debt after charging fee - we need to block him too
  1559. if ($userData['Cash'] < '-' . $userData['Credit']) {
  1560. $this->api->subscription($subscriber['id'], 'suspend');
  1561. simple_update_field(self::TABLE_SUBS, 'active', '0', "WHERE `id`='" . $subscriber['id'] . "'");
  1562. log_register('TRINITYTV BLOCK DEBTOR USER (' . $subscriber['login'] . ') AS [' . $subscriber['id'] . ']');
  1563. }
  1564. }
  1565. }
  1566. }
  1567. }
  1568. }
  1569. /**
  1570. * Resurrects some users if their was disabled by inactivity
  1571. *
  1572. * @return void
  1573. */
  1574. public function resurrectAllSubscribers() {
  1575. if (!empty($this->allSubscribers)) {
  1576. foreach ($this->allSubscribers as $subscriber) {
  1577. if (!$subscriber['active']) {
  1578. if (isset($this->allUsers[$subscriber['login']])) {
  1579. $userData = $this->allUsers[$subscriber['login']];
  1580. if (($userData['Passive'] == 0) AND ( $userData['Cash'] >= '-' . $userData['Credit'])) {
  1581. if (!empty($subscriber['tariffid'])) {
  1582. //check is user resurrection suspended?
  1583. if (!isset($this->suspended[$subscriber['login']])) {
  1584. //unblock this user
  1585. $this->api->subscription($subscriber['id'], 'resume');
  1586. simple_update_field(self::TABLE_SUBS, 'active', '1', "WHERE `id`='" . $subscriber['id'] . "'");
  1587. log_register('TRINITYTV RESURRECT USER (' . $subscriber['login'] . ') AS [' . $subscriber['id'] . ']');
  1588. }
  1589. }
  1590. }
  1591. }
  1592. }
  1593. }
  1594. }
  1595. }
  1596. }
  1597. ?>