api.xmlagent.php 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. <?php
  2. /**
  3. * Ubilling REST API implementation class
  4. */
  5. class XMLAgent {
  6. /**
  7. * Defines the REST API responses format: XML or JSON
  8. * XML is a default
  9. *
  10. * @var string
  11. */
  12. protected $outputFormat = 'xml';
  13. /**
  14. * Placeholder for Ubilling UserStats config instance
  15. *
  16. * @var null
  17. */
  18. protected $usConfig = null;
  19. /**
  20. * Placeholder for PAYMENTS_ENABLED "userstats.ini" option
  21. *
  22. * @var int
  23. */
  24. protected $uscfgPaymentsON = 0;
  25. /**
  26. * Placeholder for AN_ENABLED "userstats.ini" option
  27. *
  28. * @var int
  29. */
  30. protected $uscfgAnnouncementsON = 0;
  31. /**
  32. * Placeholder for TICKETING_ENABLED "userstats.ini" option
  33. *
  34. * @var int
  35. */
  36. protected $uscfgTicketingON = 0;
  37. /**
  38. * Placeholder for UBA_XML_ADDRESS_STRUCT "userstats.ini" option
  39. *
  40. * @var int
  41. */
  42. protected $uscfgAddressStructON = 0;
  43. /**
  44. * Placeholder for ONLINELEFT_COUNT "userstats.ini" option
  45. *
  46. * @var int
  47. */
  48. protected $uscfgOnlineLeftCountON = 0;
  49. /**
  50. * Placeholder for OPENPAYZ_ENABLED "userstats.ini" option
  51. *
  52. * @var int
  53. */
  54. protected $uscfgOpenPayzON = 0;
  55. /**
  56. * Placeholder for OPENPAYZ_REALID "userstats.ini" option
  57. *
  58. * @var int
  59. */
  60. protected $uscfgOpenPayzRealIDON = 0;
  61. /**
  62. * Placeholder for OPENPAYZ_URL "userstats.ini" option
  63. *
  64. * @var int
  65. */
  66. protected $uscfgOpenPayzURL = '../openpayz/backend/';
  67. /**
  68. * Placeholder for OPENPAYZ_PAYSYS "userstats.ini" option
  69. *
  70. * @var int
  71. */
  72. protected $uscfgOpenPayzPaySys = '';
  73. /**
  74. * Placeholder for currency "userstats.ini" option
  75. *
  76. * @var int
  77. */
  78. protected $uscfgCurrency = 'UAH';
  79. /**
  80. * Placeholder for TC_ENABLED "userstats.ini" option
  81. *
  82. * @var int
  83. */
  84. protected $uscfgTariffCahngeEnabled = 0;
  85. /**
  86. * Placeholder for TC_EXTENDED_MATRIX "userstats.ini" option
  87. *
  88. * @var int
  89. */
  90. protected $uscfgTariffCahngeMatrix = 0;
  91. /**
  92. * Placeholder for TC_TARIFFSALLOWED "userstats.ini" option
  93. *
  94. * @var int
  95. */
  96. protected $uscfgTariffCahngeAllowedTo = '';
  97. /**
  98. * Placeholder for TC_TARIFFENABLEDFROM "userstats.ini" option
  99. *
  100. * @var int
  101. */
  102. protected $uscfgTariffCahngeAllowedFrom = '';
  103. /**
  104. * Placeholder for the whole "opayz.ini" config contents
  105. *
  106. * @var int
  107. */
  108. protected $usOpayzCfg = array();
  109. /**
  110. * Placeholder for the whole "tariffmatrix.ini" config contents
  111. */
  112. protected $usTariffMatrixCfg = array();
  113. /**
  114. * Placeholder for XMLAGENT_DEBUG_ON "userstats.ini" option
  115. *
  116. * @var bool
  117. */
  118. protected $debug = false;
  119. /**
  120. * Placeholder for XMLAGENT_DEBUG_DEEP_ON "userstats.ini" option
  121. *
  122. * @var bool
  123. */
  124. protected $debugDeep = false;
  125. /**
  126. * Placeholder for XMLAGENT_EXTENDED_AUTH_ON "userstats.ini" option
  127. *
  128. * @var bool
  129. */
  130. protected $extendedAuthON = false;
  131. /**
  132. * Contains auth key (MD5 hash of the current UB instance serial) from the incoming request
  133. *
  134. * @var string
  135. */
  136. protected $extendedAuthKey = '';
  137. const TICKET_TYPE_SUPPORT = 'support_request';
  138. const TICKET_TYPE_SIGNUP = 'signup_request';
  139. const DEBUG_FILE_PATH = 'exports/xmlagent.debug';
  140. const TARIFF_MATRIX_CONFIG_PATH = 'config/tariffmatrix.ini';
  141. public function __construct($user_login = '') {
  142. $this->loadConfig();
  143. $this->loadOptions();
  144. $this->outputFormat = (ubRouting::checkGet('json') ? 'json' : 'xml');
  145. $this->extendedAuthKey = (ubRouting::checkGet('uberkey') ? ubRouting::get('uberkey') : '');
  146. $this->router($user_login);
  147. }
  148. /**
  149. * Get the UserStatsConfig instance
  150. *
  151. * @return void
  152. */
  153. protected function loadConfig() {
  154. $this->usConfig = new UserStatsConfig();
  155. }
  156. /**
  157. * Essential options loader
  158. *
  159. * @return void
  160. */
  161. protected function loadOptions() {
  162. $this->usOpayzCfg = $this->usConfig->getOpayzCfg();
  163. $this->usTariffMatrixCfg = $this->usConfig->getTariffMatrixCfg();
  164. $this->uscfgPaymentsON = $this->usConfig->getUstasParam('PAYMENTS_ENABLED', 0);
  165. $this->uscfgAnnouncementsON = $this->usConfig->getUstasParam('AN_ENABLED', 0);
  166. $this->uscfgTicketingON = $this->usConfig->getUstasParam('TICKETING_ENABLED', 0);
  167. $this->uscfgAddressStructON = $this->usConfig->getUstasParam('UBA_XML_ADDRESS_STRUCT', 0);
  168. $this->uscfgOnlineLeftCountON = $this->usConfig->getUstasParam('ONLINELEFT_COUNT', 0);
  169. $this->uscfgOpenPayzON = $this->usConfig->getUstasParam('OPENPAYZ_ENABLED', 0);
  170. $this->uscfgOpenPayzRealIDON = $this->usConfig->getUstasParam('OPENPAYZ_REALID', 0);
  171. $this->uscfgOpenPayzURL = $this->usConfig->getUstasParam('OPENPAYZ_URL', '../openpayz/backend/');
  172. $this->uscfgOpenPayzPaySys = $this->usConfig->getUstasParam('OPENPAYZ_PAYSYS', 0);
  173. $this->uscfgCurrency = $this->usConfig->getUstasParam('currency', 'UAH');
  174. $this->uscfgTariffCahngeEnabled = $this->usConfig->getUstasParam('TC_ENABLED', 0);
  175. $this->uscfgTariffCahngeMatrix = $this->usConfig->getUstasParam('TC_EXTENDED_MATRIX', 0);
  176. $this->uscfgTariffCahngeAllowedTo = $this->usConfig->getUstasParam('TC_TARIFFSALLOWED', '');
  177. $this->uscfgTariffCahngeAllowedFrom = $this->usConfig->getUstasParam('TC_TARIFFENABLEDFROM', '');
  178. $this->debug = $this->usConfig->getUstasParam('XMLAGENT_DEBUG_ON', false);
  179. $this->debugDeep = $this->usConfig->getUstasParam('XMLAGENT_DEBUG_DEEP_ON', false);
  180. $this->extendedAuthON = $this->usConfig->getUstasParam('XMLAGENT_EXTENDED_AUTH_ON', false);
  181. }
  182. /**
  183. * Retrieves the UB serial from DB
  184. *
  185. * @return mixed|string
  186. */
  187. protected function getUBSerial() {
  188. $result = '';
  189. $dbUbstats = new NyanORM('ubstats');
  190. $dbUbstats->where('key', '=', 'ubid');
  191. $result = $dbUbstats->getAll('key');
  192. $result = empty($result) ? '' : $result['ubid']['value'];
  193. return($result);
  194. }
  195. /**
  196. * Chooses the destination according to GET params
  197. *
  198. * @param $user_login
  199. *
  200. * @return void
  201. */
  202. public function router($user_login) {
  203. $extenAuthSuccessful = true;
  204. $outputFormat = $this->outputFormat;
  205. if ($this->extendedAuthON) {
  206. if (empty($this->extendedAuthKey)) {
  207. $extenAuthSuccessful = false;
  208. } else {
  209. $ubKey = $this->getUBSerial();
  210. $extenAuthSuccessful = (md5($ubKey) === $this->extendedAuthKey);
  211. }
  212. }
  213. if (!$extenAuthSuccessful) {
  214. $this->renderResponse(array(array('reason' => 'wrong_uberauth')), 'error', '', $outputFormat);
  215. }
  216. $resultToRender = array();
  217. $mainSection = 'data';
  218. $subSection = '';
  219. $messages = false;
  220. $restapiMethod = '';
  221. $debugData = '';
  222. $getUserData = !(ubRouting::checkGet(array(
  223. 'payments',
  224. 'announcements',
  225. 'tickets',
  226. 'opayz',
  227. 'agentassigned',
  228. 'tariffvservices',
  229. 'activetariffsvservices',
  230. 'tarifftoswitchallowed',
  231. 'feecharges',
  232. 'ticketcreate'
  233. ),
  234. true, true)
  235. );
  236. if (!empty($user_login)) {
  237. if ($getUserData) {
  238. $mainSection = 'userdata';
  239. $restapiMethod = 'getuserdata';
  240. $resultToRender = $this->getUserData($user_login);
  241. } else {
  242. if (ubRouting::checkGet('payments') and $this->uscfgPaymentsON) {
  243. $subSection = 'payment';
  244. $resultToRender = $this->getUserPayments($user_login);
  245. }
  246. if (ubRouting::checkGet('tickets') and $this->uscfgTicketingON) {
  247. $subSection = 'ticket';
  248. $resultToRender = $this->getUserTickets($user_login);
  249. }
  250. if (ubRouting::checkGet('opayz') and $this->uscfgOpenPayzON) {
  251. $subSection = 'paysys';
  252. $resultToRender = $this->getUserOpenPayz($user_login);
  253. }
  254. if (ubRouting::checkGet('agentassigned')) {
  255. $subSection = 'agentdata';
  256. $resultToRender = $this->getUserContrAgent($user_login);
  257. }
  258. if (ubRouting::checkGet('tariffvservices')) {
  259. $subSection = 'tariffvservices';
  260. $resultToRender = $this->getUserTariffAndVservices($user_login);
  261. }
  262. if (ubRouting::checkGet('tarifftoswitchallowed')) {
  263. $subSection = 'tarifftoswitchallowed';
  264. $resultToRender = $this->getTariffsToSwitchAllowed($user_login);
  265. }
  266. if (ubRouting::checkGet('feecharges')) {
  267. $subSection = 'feecharge';
  268. $date_from = ubRouting::checkGet('datefrom') ? ubRouting::get('datefrom') : '';
  269. $date_to = ubRouting::checkGet('dateto') ? ubRouting::get('dateto') : '';
  270. $resultToRender = $this->getUserFeeCharges($user_login, $date_from, $date_to);
  271. }
  272. if (ubRouting::checkGet(array('ticketcreate', 'tickettext', 'tickettype'))
  273. and ubRouting::get('tickettype') == self::TICKET_TYPE_SUPPORT
  274. ) {
  275. $text = base64_decode(ubRouting::get('tickettext'));
  276. $replyID = ubRouting::checkGet('reply_id') ? ubRouting::get('reply_id') : 0;
  277. $debugData = empty($replyID) ? 'replyID: ' . $replyID . ' ' . $text : $text;
  278. $restapiMethod = 'supportticketcreate';
  279. $resultToRender = $this->createSupportTicket($user_login, $text, $replyID);
  280. }
  281. }
  282. }
  283. if (ubRouting::checkGet('announcements') and $this->uscfgAnnouncementsON) {
  284. $messages = true;
  285. $restapiMethod = 'announcements';
  286. $resultToRender = $this->getAnnouncements();
  287. }
  288. if (ubRouting::checkGet('activetariffsvservices')) {
  289. $subSection = 'activetariffsvservices';
  290. $resultToRender = $this->getAllTariffsVservices();
  291. }
  292. if (ubRouting::checkGet(array('ticketcreate', 'tickettype'))
  293. and ubRouting::get('tickettype') == self::TICKET_TYPE_SIGNUP
  294. ) {
  295. $requestJSON = file_get_contents("php://input");
  296. $debugData = $requestJSON;
  297. $restapiMethod = 'signupticketcreate';
  298. $resultToRender = $this->createSignUpRequest($requestJSON);
  299. }
  300. $restapiMethod = (empty($restapiMethod) and !empty($subSection)) ? $subSection : $restapiMethod;
  301. $this->debugLog($restapiMethod, $debugData);
  302. $this->renderResponse($resultToRender, $mainSection, $subSection, $outputFormat, $messages);
  303. }
  304. /**
  305. * Renders some data as XML/JSON
  306. *
  307. * @param array $data data array for rendering
  308. * @param string $mainSection all output data parent element tag name
  309. * @param string $subSection parent tag for each data qunique element tag name
  310. * @param string $format output format: xml or json
  311. * @param bool $messages is data contain announcements data for render
  312. *
  313. * @return void
  314. */
  315. public static function renderResponse($data, $mainSection = '', $subSection = '', $format = 'xml', $messages = false) {
  316. $result = '';
  317. //XML legacy output
  318. if ($format == 'xml') {
  319. $result .= '<?xml version="1.0" encoding="utf-8"?>' . PHP_EOL;
  320. if (!empty($mainSection)) {
  321. $result .= '<' . $mainSection . '>' . PHP_EOL;
  322. }
  323. if (!empty($data)) {
  324. foreach ($data as $index => $record) {
  325. if (!empty($subSection)) {
  326. $result .= '<' . $subSection . '>' . PHP_EOL;
  327. }
  328. //normal data output
  329. if (!$messages) {
  330. foreach ($record as $tag => $value) {
  331. $result .= "\t" . '<' . $tag . '>' . $value . '</' . $tag . '>' . PHP_EOL;
  332. }
  333. } else {
  334. //announcements data output
  335. $result .= '<message unic="' . $record['unic'] . '" title="' . $record['title'] . '">' . $record['text'] . '</message>' . PHP_EOL;
  336. }
  337. if (!empty($subSection)) {
  338. $result .= '</' . $subSection . '>' . PHP_EOL;
  339. }
  340. }
  341. }
  342. if (!empty($mainSection)) {
  343. $result .= '</' . $mainSection . '>' . PHP_EOL;
  344. }
  345. }
  346. //JSON data output
  347. if ($format == 'json') {
  348. $jsonData = array();
  349. $rcount = 0;
  350. if (!empty($data)) {
  351. foreach ($data as $index => $record) {
  352. if (!empty($record)) {
  353. foreach ($record as $tag => $value) {
  354. if (!empty($subSection) OR $messages) {
  355. $jsonData[$rcount][$tag] = $value;
  356. } else {
  357. $jsonData[$tag] = $value;
  358. }
  359. }
  360. }
  361. $rcount++;
  362. }
  363. }
  364. $result .= json_encode($jsonData);
  365. }
  366. //pushing result to client
  367. $contentType = 'text';
  368. if ($format == 'json') {
  369. $contentType = 'application/json';
  370. }
  371. header('Last-Modified: ' . gmdate('r'));
  372. header('Content-Type: ' . $contentType . '; charset=UTF-8');
  373. header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
  374. header("Pragma: no-cache");
  375. header('Access-Control-Allow-Origin: *');
  376. die($result);
  377. }
  378. /**
  379. * Data collector for "payments" request
  380. *
  381. * @param $login
  382. *
  383. * @return array
  384. */
  385. protected function getUserPayments($login) {
  386. $payments = array();
  387. $allpayments = zbs_CashGetUserPayments($login);
  388. if (!empty($allpayments)) {
  389. foreach ($allpayments as $io => $eachpayment) {
  390. $payments[$eachpayment['id']]['date'] = $eachpayment['date'];
  391. $payments[$eachpayment['id']]['summ'] = $eachpayment['summ'];
  392. $payments[$eachpayment['id']]['balance'] = $eachpayment['balance'];
  393. }
  394. }
  395. return ($payments);
  396. }
  397. /**
  398. * Data collector for "announcements" request
  399. *
  400. * @return array
  401. */
  402. protected function getAnnouncements() {
  403. $annArr = array();
  404. $annTable = new NyanORM('zbsannouncements');
  405. $annTable->where('public', '=', '1');
  406. $annTable->orderBy('id', 'DESC');
  407. $allAnnouncements = $annTable->getAll();
  408. if (!empty($allAnnouncements)) {
  409. foreach ($allAnnouncements as $ian => $eachAnnouncement) {
  410. $annText = strip_tags($eachAnnouncement['text']);
  411. $allTitle = strip_tags($eachAnnouncement['title']);
  412. $annArr[] = array(
  413. 'unic' => $eachAnnouncement['id'],
  414. 'title' => $allTitle,
  415. 'text' => $annText
  416. );
  417. }
  418. }
  419. return ($annArr);
  420. }
  421. /**
  422. * Data collector for "tickets" request
  423. *
  424. * @param $login
  425. *
  426. * @return array
  427. */
  428. protected function getUserTickets($login) {
  429. $ticketsArr = array();
  430. $myTickets = array();
  431. $ticketsTable = new NyanORM('ticketing');
  432. $ticketsTable->orderBy('date', 'DESC');
  433. $allTickets = $ticketsTable->getAll();
  434. if (!empty($allTickets)) {
  435. foreach ($allTickets as $io => $each) {
  436. if ($each['from'] == $login or $each['to'] == $login or isset($myTickets[$each['replyid']])) {
  437. $myTickets[$each['id']] = $each['id'];
  438. $ticketsArr[$each['id']]['id'] = $each['id'];
  439. $ticketsArr[$each['id']]['date'] = $each['date'];
  440. $ticketsArr[$each['id']]['from'] = $each['from'];
  441. $ticketsArr[$each['id']]['to'] = $each['to'];
  442. $ticketsArr[$each['id']]['replyid'] = $each['replyid'];
  443. $ticketsArr[$each['id']]['status'] = $each['status'];
  444. $ticketsArr[$each['id']]['text'] = $each['text'];
  445. }
  446. }
  447. }
  448. return ($ticketsArr);
  449. }
  450. /**
  451. * Data collector for "opayz" request
  452. *
  453. * @param $login
  454. *
  455. * @return array
  456. */
  457. protected function getUserOpenPayz($login) {
  458. $opayzArr = array();
  459. $paySys = explode(",", $this->uscfgOpenPayzPaySys);
  460. $payDesc = (empty($this->usOpayzCfg) ? array() : $this->usOpayzCfg);
  461. $opayzPaymentid = 0;
  462. if ($this->uscfgOpenPayzRealIDON) {
  463. $opayzPaymentid = zbs_PaymentIDGet($login);
  464. } else {
  465. $userdata = zbs_UserGetStargazerData($login);
  466. $opayzPaymentid = ip2int($userdata['IP']);
  467. }
  468. if (!empty($paySys)) {
  469. if (!empty($opayzPaymentid)) {
  470. foreach ($paySys as $io => $eachpaysys) {
  471. if (isset($payDesc[$eachpaysys])) {
  472. $paysys_desc = $payDesc[$eachpaysys];
  473. } else {
  474. $paysys_desc = '';
  475. }
  476. $paymentUrl = $this->uscfgOpenPayzURL . $eachpaysys . '/?customer_id=' . $opayzPaymentid;
  477. $opayzArr[$eachpaysys]['name'] = $eachpaysys;
  478. $opayzArr[$eachpaysys]['url'] = $paymentUrl;
  479. $opayzArr[$eachpaysys]['description'] = $paysys_desc;
  480. }
  481. }
  482. }
  483. return ($opayzArr);
  484. }
  485. /**
  486. * Data collector for "agentassigned" request
  487. *
  488. * @param $login
  489. *
  490. * @return array
  491. */
  492. protected function getUserContrAgent($login) {
  493. $allAddress = zbs_AddressGetFulladdresslist();
  494. $userAddress = empty($allAddress) ? array() : $allAddress[$login];
  495. $agentData = zbs_AgentAssignedGetDataFast($login, $userAddress);
  496. $agentArray = empty($agentData) ? array() : array('agentdata' => $agentData);
  497. return ($agentArray);
  498. }
  499. /**
  500. * Data collector for "userdata" request
  501. *
  502. * @param $login
  503. *
  504. * @return array
  505. */
  506. protected function getUserData($login) {
  507. $userdata = zbs_UserGetStargazerData($login);
  508. $alladdress = zbs_AddressGetFulladdresslist();
  509. if ($this->uscfgAddressStructON) {
  510. $alladdressStruct = zbs_AddressGetFulladdresslistStruct($login);
  511. } else {
  512. $alladdressStruct = array();
  513. }
  514. $allrealnames = zbs_UserGetAllRealnames();
  515. $contract = zbs_UserGetContract($login);
  516. $contractDate = zbs_UserGetContractDate($login);
  517. $email = zbs_UserGetEmail($login);
  518. $mobile = zbs_UserGetMobile($login);
  519. $phone = zbs_UserGetPhone($login);
  520. $apiVer = '1';
  521. $passive = $userdata['Passive'];
  522. $down = $userdata['Down'];
  523. //payment id handling
  524. if ($this->uscfgOpenPayzON) {
  525. if ($this->uscfgOpenPayzRealIDON) {
  526. $paymentid = zbs_PaymentIDGet($login);
  527. } else {
  528. $paymentid = ip2int($userdata['IP']);
  529. }
  530. } else {
  531. $paymentid = 0;
  532. }
  533. if ($userdata['CreditExpire'] != 0) {
  534. $credexpire = date("d-m-Y", $userdata['CreditExpire']);
  535. } else {
  536. $credexpire = 'No';
  537. }
  538. if ($userdata['TariffChange']) {
  539. $tariffNm = $userdata['TariffChange'];
  540. } else {
  541. $tariffNm = 'No';
  542. }
  543. $traffdown = 0;
  544. $traffup = 0;
  545. $traffdgb = 0;
  546. $traffugb = 0;
  547. for ($i = 0; $i <= 9; $i++) {
  548. $traffdown = $traffdown + $userdata['D' . $i];
  549. $traffup = $traffup + $userdata['U' . $i];
  550. }
  551. $traffdgb = round($traffdown / 1073741824);
  552. $traffugb = round($traffup / 1073741824);
  553. if ($traffdgb == 0) {
  554. $traffdgb = 1;
  555. }
  556. if ($traffugb == 0) {
  557. $traffugb = 1;
  558. }
  559. // pasive state check
  560. if ($passive) {
  561. $passive_state = 'frozen';
  562. } else {
  563. $passive_state = 'active';
  564. }
  565. //down state check
  566. if ($down) {
  567. $down_state = ' + disabled';
  568. } else {
  569. $down_state = '';
  570. }
  571. // START OF ONLINELEFT COUNTING <<
  572. if ($this->uscfgOnlineLeftCountON) {
  573. // DEFINE VARS:
  574. $userBalance = $userdata['Cash'];
  575. if ($userBalance >= 0) {
  576. $balanceExpire = zbs_GetOnlineLeftCount($login, $userBalance, $userdata['Tariff'], true);
  577. } else {
  578. $balanceExpire = 'debt';
  579. }
  580. } else {
  581. $balanceExpire = 'No';
  582. }
  583. // >> END OF ONLINELEFT COUNTING
  584. $reqResult = array();
  585. $reqResult[] = array('address' => @$alladdress[$login]);
  586. if ($this->uscfgAddressStructON) {
  587. if (!empty($alladdressStruct)) {
  588. foreach ($alladdressStruct[$login] as $field => $value) {
  589. $reqResult[] = array($field => $value);
  590. }
  591. }
  592. }
  593. $tariffData = zbs_UserGetTariffData($userdata['Tariff']);
  594. $tariffPeriod = isset($tariffData['period']) ? $tariffData['period'] : 'month';
  595. $vservicesPeriodON = $this->usConfig->getUstasParam('VSERVICES_CONSIDER_PERIODS', 0);
  596. $includeVServices = $this->usConfig->getUstasParam('ONLINELEFT_CONSIDER_VSERVICES', 0);
  597. $totalVsrvPrice = ($vservicesPeriodON) ? zbs_vservicesGetUserPricePeriod($login, $tariffPeriod) : zbs_vservicesGetUserPrice($login);
  598. $payedTillDate = (($balanceExpire !== 'No' and $balanceExpire !== 'debt' and is_numeric($balanceExpire))
  599. ? date("d.m.Y", time() + ($balanceExpire * 24 * 60 * 60)) : 'none');
  600. $reqResult[] = array('realname' => @$allrealnames[$login]);
  601. $reqResult[] = array('login' => $login);
  602. $reqResult[] = array('cash' => @round($userdata['Cash'], 2));
  603. $reqResult[] = array('ip' => @$userdata['IP']);
  604. $reqResult[] = array('phone' => $phone);
  605. $reqResult[] = array('mobile' => $mobile);
  606. $reqResult[] = array('email' => $email);
  607. $reqResult[] = array('credit' => @$userdata['Credit']);
  608. $reqResult[] = array('creditexpire' => $credexpire);
  609. $reqResult[] = array('payid' => strval($paymentid));
  610. $reqResult[] = array('contract' => $contract);
  611. $reqResult[] = array('contractdate' => $contractDate);
  612. $reqResult[] = array('tariff' => $userdata['Tariff']);
  613. $reqResult[] = array('tariffalias' => __($userdata['Tariff']));
  614. $reqResult[] = array('tariffnm' => $tariffNm);
  615. $reqResult[] = array('traffdownload' => zbs_convert_size($traffdown));
  616. $reqResult[] = array('traffupload' => zbs_convert_size($traffup));
  617. $reqResult[] = array('trafftotal' => zbs_convert_size($traffdown + $traffup));
  618. $reqResult[] = array('accountstate' => $passive_state . $down_state);
  619. $reqResult[] = array('accountexpire' => $balanceExpire);
  620. $reqResult[] = array('payedtilldate' => $payedTillDate);
  621. $reqResult[] = array('payedtillvsrvincluded' => $includeVServices);
  622. $reqResult[] = array('vservicescost' => $totalVsrvPrice);
  623. $reqResult[] = array('currency' => $this->uscfgCurrency);
  624. $reqResult[] = array('version' => $apiVer);
  625. return ($reqResult);
  626. }
  627. /**
  628. * Data collector for "tariffvservices" request
  629. *
  630. * @param $login
  631. *
  632. * @return array
  633. */
  634. protected function getUserTariffAndVservices($login) {
  635. $tariffvsrvs = array();
  636. $userTariff = zbs_UserGetTariff($login);
  637. $tariffData = (!empty($userTariff) ? zbs_UserGetTariffData($userTariff) : array());
  638. if (!empty($tariffData)) {
  639. $vsrvsData = zbs_vservicesGetUsersAll($login, true, true);
  640. $tariffvsrvs[$tariffData['name']]['tariffname'] = $tariffData['name'];
  641. $tariffvsrvs[$tariffData['name']]['tariffprice'] = $tariffData['Fee'];
  642. $tariffvsrvs[$tariffData['name']]['tariffdaysperiod'] = $tariffData['period'];
  643. if (!empty($vsrvsData)) {
  644. $vsrvsData = $vsrvsData[$login];
  645. foreach ($vsrvsData as $eachID => $eachSrv) {
  646. $tariffvsrvs[$eachID]['vsrvname'] = $eachSrv['vsrvname'];
  647. $tariffvsrvs[$eachID]['vsrvprice'] = $eachSrv['price'];
  648. $tariffvsrvs[$eachID]['vsrvdaysperiod'] = $eachSrv['daysperiod'];
  649. }
  650. }
  651. }
  652. return ($tariffvsrvs);
  653. }
  654. /**
  655. * Data collector for "activetariffsvservices" request
  656. *
  657. * @return array
  658. */
  659. protected function getAllTariffsVservices() {
  660. $alltariffsvservices = array();
  661. $tariffsNoLousy = zbs_GetTariffsDataAll(true);
  662. $vservicesNoArchived = zbs_getVservicesAllWithNames(true);
  663. if (!empty($tariffsNoLousy)) {
  664. foreach ($tariffsNoLousy as $eachName => $eachRec) {
  665. $alltariffsvservices[$eachName]['tariffname'] = $eachRec['name'];
  666. $alltariffsvservices[$eachName]['tariffprice'] = $eachRec['Fee'];
  667. $alltariffsvservices[$eachName]['tariffdaysperiod'] = $eachRec['period'];
  668. }
  669. }
  670. if (!empty($vservicesNoArchived)) {
  671. foreach ($vservicesNoArchived as $eachID => $eachSrv) {
  672. $alltariffsvservices[$eachID]['vsrvname'] = $eachSrv['vsrvname'];
  673. $alltariffsvservices[$eachID]['vsrvprice'] = $eachSrv['price'];
  674. $alltariffsvservices[$eachID]['vsrvdaysperiod'] = $eachSrv['charge_period_days'];
  675. }
  676. }
  677. return ($alltariffsvservices);
  678. }
  679. /**
  680. * Returns tariff list the user is allowed to switch to
  681. *
  682. * @param $login
  683. *
  684. * @return array
  685. */
  686. protected function getTariffsToSwitchAllowed($login) {
  687. $userTariff = zbs_UserGetTariff($login);
  688. $tariffsAllowedTo = array();
  689. $result = array();
  690. if (!empty($userTariff)) {
  691. if ($this->uscfgTariffCahngeMatrix) {
  692. $tariffsAllowedTo = $this->getTariffMatrixAllowedTo($userTariff);
  693. } else {
  694. $tariffsAllowedFrom = explode(',', $this->uscfgTariffCahngeAllowedFrom);
  695. if (!empty($tariffsAllowedFrom) and in_array($userTariff, $tariffsAllowedFrom)) {
  696. $tariffsAllowedTo = explode(',', $this->uscfgTariffCahngeAllowedTo);
  697. }
  698. }
  699. }
  700. if (!empty($tariffsAllowedTo)) {
  701. foreach ($tariffsAllowedTo as $io => $item) {
  702. $result[$io]['tariff'] = $item;
  703. }
  704. }
  705. return ($result);
  706. }
  707. /**
  708. * Data collector for "feecharges" request
  709. *
  710. * @param $login
  711. *
  712. * @return array
  713. */
  714. protected function getUserFeeCharges($login, $date_from = '', $date_to = '') {
  715. $feeCharges = array();
  716. $vservicesLabeled = zbs_VservicesGetAllNamesLabeled();
  717. $tmpFees = zbs_GetUserDBFees($login, $date_from, $date_to);
  718. $tmpAdditionalFees = zbs_GetUserAdditionalFees($login, $date_from, $date_to);
  719. $allFees = zbs_concatArraysAvoidDuplicateKeys($tmpFees, $tmpAdditionalFees);
  720. if (!empty($allFees)) {
  721. ksort($allFees);
  722. }
  723. if (!empty($allFees)) {
  724. foreach ($allFees as $io => $eachFee) {
  725. $feeCharges[$io]['date'] = $eachFee['date'];
  726. $feeCharges[$io]['summ'] = $eachFee['summ'];
  727. $feeCharges[$io]['balance'] = $eachFee['from'];
  728. $feeCharges[$io]['note'] = ((ispos($eachFee['note'], 'Service:') and !empty($vservicesLabeled[$eachFee['note']]))
  729. ? $vservicesLabeled[$eachFee['note']] : $eachFee['note']);
  730. if ($eachFee['operation'] == 'Fee') {
  731. $feeCharges[$io]['type'] = 'mainsrv';
  732. } elseif (ispos($eachFee['note'], 'Service:')) {
  733. $feeCharges[$io]['type'] = 'virtualsrv';
  734. } else {
  735. $feeCharges[$io]['type'] = 'other';
  736. }
  737. }
  738. }
  739. return ($feeCharges);
  740. }
  741. /**
  742. * Support request creation routine
  743. *
  744. * @param $login
  745. * @param $tickettext
  746. *
  747. * @return array[]
  748. * @throws Exception
  749. */
  750. protected function createSupportTicket($login, $tickettext, $replyID) {
  751. $ticketID = 0;
  752. $replyID = empty($replyID) ? 'NULL' : $replyID;
  753. $result = array();
  754. if (!empty($login) and !empty($tickettext)) {
  755. $from = mysql_real_escape_string($login);
  756. $text = mysql_real_escape_string(strip_tags($tickettext));
  757. $date = curdatetime();
  758. $ticketDB = new NyanORM('ticketing');
  759. $ticketDB->dataArr(array(
  760. 'date' => $date,
  761. 'replyid' => $replyID,
  762. 'status' => '0',
  763. 'from' => $from,
  764. 'text' => $text
  765. ));
  766. $ticketDB->create();
  767. $ticketID = $ticketDB->getLastId();
  768. }
  769. if (empty($ticketID)) {
  770. $result = array('ticket' => array('created' => 'error', 'id' => 0));
  771. } else {
  772. $result = array('ticket' => array('created' => 'success', 'id' => $ticketID));
  773. if (empty($replyID) or $replyID == 'NULL') {
  774. $logEvent = 'TICKET CREATE (' . $from . ') NEW [' . $ticketID . ']';
  775. } else {
  776. $logEvent = 'TICKET CREATE (' . $from . ') REPLY TO [' . $replyID . ']';
  777. }
  778. log_register($logEvent);
  779. }
  780. return ($result);
  781. }
  782. /**
  783. * Sign up request creation routine
  784. *
  785. * @param $requestBody
  786. *
  787. * @return array[]
  788. * @throws Exception
  789. */
  790. protected function createSignUpRequest($requestBody) {
  791. $sigreqID = 0;
  792. $result = array();
  793. // expected $requestBody JSON structure:
  794. /*
  795. {
  796. "date": "2024-02-29 19:57:50",
  797. "state": 0,
  798. "ip": "app_IP_addr",
  799. "street": "Some_City Some_Street",
  800. "build": "111",
  801. "apt": "222",
  802. "realname": "FirstName LastName",
  803. "phone": "0551234567",
  804. "service": "Internet",
  805. "notes": "Some important notes here"
  806. }
  807. */
  808. if (!empty($requestBody)) {
  809. $requestBody = json_decode($requestBody);
  810. $sigreqDB = new NyanORM('sigreq');
  811. $sigreqDB->dataArr($requestBody);
  812. $sigreqDB->create();
  813. $sigreqID = $sigreqDB->getLastId();
  814. }
  815. if (empty($sigreqID)) {
  816. $result = array('signup_request' => array('created' => 'error', 'id' => 0));
  817. } else {
  818. $result = array('signup_request' => array('created' => 'success', 'id' => $sigreqID));
  819. $logEvent = 'SIGNUP REQUEST CREATED WITH ID: ' . $sigreqID;
  820. log_register($logEvent);
  821. }
  822. return ($result);
  823. }
  824. /**
  825. * Returns the tariffs list which current user tariff might be switched to
  826. * according to the current TariffMatrix settings
  827. *
  828. * Yep, this one, probably, should be somewhere in "tariffchanger" module
  829. * Well, it probably will, when "tariffchanger" will get a separate Class definition
  830. *
  831. * @param $userTariff
  832. *
  833. * @return false|string[]
  834. */
  835. public static function getTariffMatrixAllowedTo($userTariff) {
  836. $matrix = parse_ini_file(self::TARIFF_MATRIX_CONFIG_PATH);
  837. $result = false;
  838. if (!empty($matrix)) {
  839. if (isset($matrix[$userTariff])) {
  840. //extract tariff movement rules
  841. $result = explode(',', $matrix[$userTariff]);
  842. } else {
  843. //no tariff match
  844. $result = false;
  845. }
  846. } else {
  847. //no matrix entries
  848. $result = false;
  849. }
  850. return ($result);
  851. }
  852. /**
  853. * Writes some debbuggins to log or/and to a local file
  854. *
  855. * @param $debugData
  856. *
  857. * @return void
  858. */
  859. protected function debugLog($restapiMethod = '', $debugData = '') {
  860. if ($this->debug) {
  861. $requsterIP = $_SERVER['REMOTE_ADDR'];
  862. $requestMethod = $_SERVER['REQUEST_METHOD'];
  863. $requestURI = $_SERVER['REQUEST_URI'];
  864. log_register('XMLAGENT: [ ' . $restapiMethod . ' ] was called from [ ' . $requsterIP . ' ] via HTTP ' . $requestMethod . ' with params: ' . $requestURI);
  865. }
  866. if ($this->debugDeep) {
  867. if (file_exists(self::DEBUG_FILE_PATH)) {
  868. file_put_contents(self::DEBUG_FILE_PATH,
  869. "======== START OF DEBUG RECORD ========" . "\n" .
  870. curdatetime() . ' ' . $restapiMethod . "\n" .
  871. "Debug data:\n" . $debugData . "\n" .
  872. "******** GLOBAL ARRAYS ********\n" .
  873. "SERVER:\n" . print_r($_SERVER, true) . "\n\n" .
  874. "RQUEST:\n" . print_r($_REQUEST, true) . "\n" .
  875. "======== END OF DEBUG RECORD ========" . "\n\n\n\n",
  876. FILE_APPEND
  877. );
  878. } else {
  879. log_register('XMLAGENT: trying to use "deep" debugging, but no "exports" DIR found where it should be');
  880. }
  881. }
  882. }
  883. }