api.userside.php 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794
  1. <?php
  2. /**
  3. * Userside integration API
  4. * https://www.userside.eu
  5. */
  6. class UserSideApi {
  7. const API_VER = '1.5';
  8. const API_DATE = '06.10.2017';
  9. /**
  10. * Stores system alter config as key=>value
  11. *
  12. * @var array
  13. */
  14. protected $altCfg = array();
  15. /**
  16. * Contains all of available tariffs data as tariffname=>data
  17. *
  18. * @var array
  19. */
  20. protected $allTariffs = array();
  21. /**
  22. * Contains all of available tariff speeds as tariffname=>data (speeddown/speedup keys)
  23. *
  24. * @var array
  25. */
  26. protected $allTariffSpeeds = array();
  27. /**
  28. * Contains all tariffs periods as tariffname=>period (month/day)
  29. *
  30. * @var array
  31. */
  32. protected $allTariffPeriods = array();
  33. /**
  34. * Contains available cities as cityid=>data
  35. *
  36. * @var array
  37. */
  38. protected $allCities = array();
  39. /**
  40. * Contains available streets array as streetid=>data
  41. *
  42. * @var array
  43. */
  44. protected $allStreets = array();
  45. /**
  46. * Contains all available builds array as buildid=>builddata
  47. *
  48. * @var array
  49. */
  50. protected $allBuilds = array();
  51. /**
  52. * Contains available custom fields types as id=>name
  53. *
  54. * @var array
  55. */
  56. protected $allCfTypes = array();
  57. /**
  58. * Contains available custom fields data as login+cftypeid=>data
  59. *
  60. * @var array
  61. */
  62. protected $allCfData = array();
  63. /**
  64. * Contains data of all available Internet users as login=>data
  65. *
  66. * @var array
  67. */
  68. protected $allUserData = array();
  69. /**
  70. * Contains available tag types as id=>name
  71. *
  72. * @var array
  73. */
  74. protected $allTagTypes = array();
  75. /**
  76. * Contains supported methods list
  77. *
  78. * @var array
  79. */
  80. protected $supportedMethods = array();
  81. /**
  82. * Contains supported methods for change_user_data request
  83. *
  84. * @var array
  85. */
  86. protected $supportedChangeMethods = array();
  87. /**
  88. * Contains supported user states to change
  89. *
  90. * @var array
  91. */
  92. protected $supportedChageUserState = array();
  93. /**
  94. * Contains localised error notices
  95. *
  96. * @var array
  97. */
  98. protected $errorNotices = array();
  99. /**
  100. * Default streets type. May be configurable in future
  101. *
  102. * @var string
  103. */
  104. protected $defaultStreetType = '';
  105. /**
  106. * Default city type. May be configurable in future
  107. *
  108. * @var string
  109. */
  110. protected $defaultCityType = '';
  111. /**
  112. * Contains build passports data as buildid=>data
  113. *
  114. * @var array
  115. */
  116. protected $buildPassports = array();
  117. /**
  118. * Contains list of available virtual services as id=>data
  119. *
  120. * @var array
  121. */
  122. protected $vServices = array();
  123. /**
  124. * Contains virtual services to user tags mappings as tagid=>serviceid
  125. *
  126. * @var array
  127. */
  128. protected $serviceTagMappings = array();
  129. /**
  130. * Contains available device types as id=>name
  131. *
  132. * @var array
  133. */
  134. protected $allDeviceTypes = array();
  135. /**
  136. * Contains available devices models as id=>modeldata
  137. *
  138. * @var array
  139. */
  140. protected $allSwitchModels = array();
  141. /**
  142. * Contains available devices directory as id=>devicedata
  143. *
  144. * @var array
  145. */
  146. protected $allSwitches = array();
  147. /**
  148. * Debug mode flag
  149. *
  150. * @var bool
  151. */
  152. protected $debugMode = false;
  153. /**
  154. * Creates new instance of basic UserSide API
  155. */
  156. public function __construct() {
  157. $this->loadAlter();
  158. $this->setOptions();
  159. $this->loadTariffs();
  160. $this->loadTariffSpeeds();
  161. $this->loadTariffPeriods();
  162. $this->loadCities();
  163. $this->loadStreets();
  164. $this->loadBuilds();
  165. $this->loadCF();
  166. $this->loadUsers();
  167. $this->loadTagTypes();
  168. $this->loadBuildPassports();
  169. $this->loadVservices();
  170. $this->loadSwitchModels();
  171. $this->loadSwitchesAll();
  172. }
  173. /**
  174. * Loads system alter config into private property for further usage
  175. *
  176. * @global object $ubillingConfig
  177. *
  178. * @return void
  179. */
  180. protected function loadAlter() {
  181. global $ubillingConfig;
  182. $this->altCfg = $ubillingConfig->getAlter();
  183. }
  184. /**
  185. * Sets object default properties
  186. *
  187. * @return void
  188. */
  189. protected function setOptions() {
  190. $this->defaultStreetType = __('st.');
  191. $this->defaultCityType = __('ct.');
  192. $this->supportedMethods = array(
  193. 'get_supported_method_list' => __('Returns supported methods list'),
  194. 'get_api_information' => __('Returns UserSide API version'),
  195. 'get_tariff_list' => __('Returns available tariffs'),
  196. 'get_city_list' => __('Returns available cities data'),
  197. 'get_street_list' => __('Returns available streets data'),
  198. 'get_house_list' => __('Returns available builds data'),
  199. 'get_user_additional_data_type_list' => __('Returns user profile custom fields data'),
  200. 'get_user_state_list' => __('Returns users state data'),
  201. 'get_user_group_list' => __('Returns user tags list'),
  202. 'get_system_information' => __('Returns system information'),
  203. 'get_user_list' => __('Returns available users data'),
  204. 'get_user_tags' => __('Returns available users tags'),
  205. 'get_services_list' => __('Returns available user services'),
  206. 'get_user_history' => __('Returns user financial operations history'),
  207. 'get_user_messages' => __('Previous user tickets'),
  208. 'change_user_data' => __('Do some changes with user data'),
  209. 'get_supported_change_user_data_list' => __('Returns list of supported change user data methods'),
  210. 'get_supported_change_user_state' => __('Returns list of supported change user states'),
  211. 'get_supported_change_user_tariff' => __('Returns list of supported change user tariffs'),
  212. 'get_device_type' => __('Returns device type'),
  213. 'get_device_model' => __('Returns device model'),
  214. 'get_device_list' => __('Returns devices list'),
  215. 'get_connect_list' => __('Get device connection list')
  216. );
  217. $this->supportedChangeMethods = array(
  218. 'balance_operation' => __('User balance operations'),
  219. 'balance_correct_operation' => __('User balance operations'),
  220. 'name' => __('User name operations'),
  221. 'comment' => __('User notes operations'),
  222. 'tariff' => __('User tariff operations'),
  223. 'state' => __('User state operations'),
  224. );
  225. $this->supportedChageUserState = array(
  226. 'frozen' => __('Frozen user'),
  227. 'unfrozen' => __('Not frozen user'),
  228. 'down' => __('User down'),
  229. 'notdown' => __('User not down'),
  230. 'ao' => __('User AlwaysOnline'),
  231. 'notao' => __('User not AlwaysOnline')
  232. );
  233. $this->allDeviceTypes = array(
  234. 1 => 'switch',
  235. 2 => 'radio',
  236. 3 => 'olt',
  237. 4 => 'onu',
  238. 5 => 'other'
  239. );
  240. $this->errorNotices = array(
  241. 'EX_NO_PARAMS' => __('No request parameters set'),
  242. 'EX_USER_NOT_EXISTS' => __('No such user available'),
  243. 'EX_PARAM_MISSED' => __('Important parameter missed'),
  244. 'EX_METHOD_NOT_SUPPORTED' => __('Method not supported'),
  245. 'EX_BAD_MONEY_FORMAT' => __('Wrong format of money sum'),
  246. 'EX_WRONG_TARIFF' => __('Wrong tariff name')
  247. );
  248. }
  249. /**
  250. * Loads all existing Internet users from database
  251. *
  252. * @return void
  253. */
  254. protected function loadUsers() {
  255. $all = zb_UserGetAllStargazerData();
  256. if (!empty($all)) {
  257. foreach ($all as $io => $each) {
  258. $this->allUserData[$each['login']] = $each;
  259. }
  260. }
  261. }
  262. /**
  263. * Loads existing tariffs from database into protected property for further usage
  264. *
  265. * @return void
  266. */
  267. protected function loadTariffs() {
  268. $query = "SELECT * from `tariffs`";
  269. $all = simple_queryall($query);
  270. if (!empty($all)) {
  271. foreach ($all as $io => $each) {
  272. $this->allTariffs[$each['name']] = $each;
  273. }
  274. }
  275. }
  276. /**
  277. * Loads existing tariff speeds from database into protected property for further usage
  278. *
  279. * @return void
  280. */
  281. protected function loadTariffSpeeds() {
  282. $query = "SELECT * from `speeds`";
  283. $all = simple_queryall($query);
  284. if (!empty($all)) {
  285. foreach ($all as $io => $each) {
  286. $this->allTariffSpeeds[$each['tariff']] = $each;
  287. }
  288. }
  289. }
  290. /**
  291. * Loads existing tariff periods from database into protected property for further usage
  292. *
  293. * @return void
  294. */
  295. protected function loadTariffPeriods() {
  296. $this->allTariffPeriods = zb_TariffGetPeriodsAll();
  297. }
  298. /**
  299. * Loads existing cities from database
  300. *
  301. * @return void
  302. */
  303. protected function loadCities() {
  304. $query = "SELECT * from `city`";
  305. $all = simple_queryall($query);
  306. if (!empty($all)) {
  307. foreach ($all as $io => $each) {
  308. $this->allCities[$each['id']] = $each;
  309. }
  310. }
  311. }
  312. /**
  313. * Loads existing builds from database
  314. *
  315. * @return void
  316. */
  317. protected function loadBuilds() {
  318. if (@$this->altCfg['DEAD_HIDE'] AND @ $this->altCfg['DEAD_HIDE_USERSIDE'] AND @ $this->altCfg['DEAD_HIDE_BUILDS_USERSIDE']) {
  319. $query = "SELECT `build`.* from `address`
  320. LEFT JOIN `apt` ON (`address`.`aptid`=`apt`.`id`)
  321. LEFT JOIN `build` ON (`apt`.`buildid` = `build`.`id`)
  322. WHERE `apt`.`id` IS NOT NULL
  323. AND `address`.`login` NOT IN (SELECT login FROM `tags` WHERE `tagid` = '" . vf($this->altCfg['DEAD_TAGID'], 3) . "')
  324. GROUP BY `build`.`id`
  325. ";
  326. } else {
  327. $query = "SELECT * from `build`";
  328. }
  329. $all = simple_queryall($query);
  330. if (!empty($all)) {
  331. foreach ($all as $io => $each) {
  332. $this->allBuilds[$each['id']] = $each;
  333. }
  334. }
  335. }
  336. /**
  337. * Loads existing streets from database
  338. *
  339. * @return void
  340. */
  341. protected function loadStreets() {
  342. $query = "SELECT * from `street`";
  343. $all = simple_queryall($query);
  344. if (!empty($all)) {
  345. foreach ($all as $io => $each) {
  346. $this->allStreets[$each['id']] = $each;
  347. }
  348. }
  349. }
  350. /**
  351. * Loads existing custom fields data from database
  352. *
  353. * @return void
  354. */
  355. protected function loadCF() {
  356. //getting CF types
  357. $query = "SELECT * from `cftypes`";
  358. $allCfTypes = simple_queryall($query);
  359. if (!empty($allCfTypes)) {
  360. foreach ($allCfTypes as $io => $eachTypeData) {
  361. $this->allCfTypes[$eachTypeData['id']] = $eachTypeData['name'];
  362. }
  363. //getting users assigned CF content
  364. $query = "SELECT * from `cfitems`";
  365. $allCfData = simple_queryall($query);
  366. $i = 0;
  367. if (!empty($allCfData)) {
  368. foreach ($allCfData as $io => $each) {
  369. if (!empty($each['content'])) {
  370. $this->allCfData[$each['login']][$i]['id'] = $each['typeid'];
  371. $this->allCfData[$each['login']][$i]['value'] = $each['content'];
  372. }
  373. $i++;
  374. }
  375. }
  376. }
  377. }
  378. /**
  379. * Returns all users registration dates as login=>date
  380. *
  381. * @return array
  382. */
  383. protected function getUserRegData() {
  384. $result = array();
  385. $query = "SELECT * from `userreg`";
  386. $all = simple_queryall($query);
  387. if (!empty($all)) {
  388. foreach ($all as $io => $each) {
  389. $result[$each['login']] = $each['date'];
  390. }
  391. }
  392. return ($result);
  393. }
  394. /**
  395. * Loads existing tag types from database
  396. *
  397. * @return void
  398. */
  399. protected function loadTagTypes() {
  400. $query = "SELECT * from `tagtypes`";
  401. $all = simple_queryall($query);
  402. if (!empty($all)) {
  403. foreach ($all as $io => $each) {
  404. $this->allTagTypes[$each['id']] = $each['tagname'];
  405. }
  406. }
  407. }
  408. /**
  409. * Preloads available build passports for further usage
  410. *
  411. * @return void
  412. */
  413. protected function loadBuildPassports() {
  414. if ($this->altCfg['BUILD_EXTENDED']) {
  415. $query = "SELECT * from `buildpassport`";
  416. $all = simple_queryall($query);
  417. if (!empty($all)) {
  418. foreach ($all as $io => $each) {
  419. $this->buildPassports[$each['buildid']] = $each;
  420. }
  421. }
  422. }
  423. }
  424. /**
  425. * Loads array of available virtual services
  426. *
  427. * @return void
  428. */
  429. protected function loadVservices() {
  430. $query = "SELECT * from `vservices`";
  431. $all = simple_queryall($query);
  432. if (!empty($all)) {
  433. foreach ($all as $io => $each) {
  434. $this->vServices[$each['id']] = $each;
  435. $this->serviceTagMappings[$each['tagid']] = $each['id'];
  436. }
  437. }
  438. }
  439. /**
  440. * Loads available devices models from database
  441. *
  442. * @return void
  443. */
  444. protected function loadSwitchModels() {
  445. $all = zb_SwitchModelsGetAll();
  446. if (!empty($all)) {
  447. foreach ($all as $io => $each) {
  448. $this->allSwitchModels[$each['id']] = $each;
  449. }
  450. }
  451. }
  452. /**
  453. * Loads existing devices directory from database
  454. *
  455. * @return void
  456. */
  457. protected function loadSwitchesAll() {
  458. $all = zb_SwitchesGetAll();
  459. if (!empty($all)) {
  460. foreach ($all as $io => $each) {
  461. $this->allSwitches[$each['id']] = $each;
  462. }
  463. }
  464. }
  465. /**
  466. * Renders API reply as JSON string
  467. *
  468. * @param array $data
  469. *
  470. * @rerutn void
  471. */
  472. protected function renderReply($data) {
  473. $result = 'undefined';
  474. if (!$this->debugMode) {
  475. header('Content-Type: application/json');
  476. if (!empty($data)) {
  477. $result = json_encode($data);
  478. }
  479. die($result);
  480. } else {
  481. debarr($data);
  482. }
  483. }
  484. /**
  485. * Returns array of all of existing tariffs data
  486. *
  487. * @param bool $brief - brief tariffs listing only with names
  488. *
  489. * @return array
  490. */
  491. protected function getTariffsData($brief = false) {
  492. $result = array();
  493. if (!empty($this->allTariffs)) {
  494. foreach ($this->allTariffs as $tariffName => $tariffData) {
  495. if (!$brief) {
  496. $result[$tariffName]['id'] = $tariffName;
  497. $result[$tariffName]['name'] = $tariffName;
  498. $result[$tariffName]['payment'] = $tariffData['Fee'];
  499. $result[$tariffName]['payment_interval'] = ($this->allTariffPeriods[$tariffName] == 'month') ? 30 : 1;
  500. $downspeed = (isset($this->allTariffSpeeds[$tariffName]['speeddown'])) ? $this->allTariffSpeeds[$tariffName]['speeddown'] : 0;
  501. $upspeed = (isset($this->allTariffSpeeds[$tariffName]['speedup'])) ? $this->allTariffSpeeds[$tariffName]['speedup'] : 0;
  502. $result[$tariffName]['speed'] = array(
  503. 'up' => $upspeed,
  504. 'down' => $downspeed,
  505. );
  506. $result[$tariffName]['traffic'] = ($tariffData['Free']) ? $tariffData['Free'] : -1;
  507. $result[$tariffName]['service_type'] = 0;
  508. } else {
  509. $result[$tariffName]['id'] = $tariffName;
  510. $result[$tariffName]['name'] = $tariffName;
  511. }
  512. }
  513. }
  514. return ($result);
  515. }
  516. /**
  517. * Returns city data array
  518. *
  519. * @return array
  520. */
  521. protected function getCitiesData() {
  522. $result = array();
  523. if (!empty($this->allCities)) {
  524. foreach ($this->allCities as $cityId => $cityData) {
  525. $result[$cityId]['id'] = $cityId;
  526. $result[$cityId]['name'] = $cityData['cityname'];
  527. $result[$cityId]['type_name'] = $this->defaultCityType;
  528. }
  529. }
  530. return ($result);
  531. }
  532. /**
  533. * Returns streets data array
  534. *
  535. * @return array
  536. */
  537. protected function getStreetsData() {
  538. $result = array();
  539. if (!empty($this->allStreets)) {
  540. foreach ($this->allStreets as $streetId => $streetData) {
  541. $result[$streetId]['id'] = $streetId;
  542. $result[$streetId]['city_id'] = $streetData['cityid'];
  543. $result[$streetId]['name'] = $streetData['streetname'];
  544. $result[$streetId]['type_name'] = $this->defaultStreetType;
  545. $result[$streetId]['full_name'] = $this->defaultStreetType . ' ' . $streetData['streetname'];
  546. }
  547. }
  548. return ($result);
  549. }
  550. /**
  551. * Returns streets data array
  552. *
  553. * @return array
  554. */
  555. protected function getBuildsData() {
  556. $result = array();
  557. if (!empty($this->allBuilds)) {
  558. foreach ($this->allBuilds as $buildId => $buildData) {
  559. $result[$buildId]['id'] = $buildId;
  560. $streetId = $buildData['streetid'];
  561. $streetName = @$this->allStreets[$streetId]['streetname'];
  562. $result[$buildId]['city_district_id'] = '';
  563. $result[$buildId]['street_id'] = $buildData['streetid'];
  564. $result[$buildId]['full_name'] = $streetName . ' ' . $buildData['buildnum'];
  565. $result[$buildId]['postcode'] = '';
  566. if (isset($this->buildPassports[$buildId])) {
  567. $result[$buildId]['floor'] = $this->buildPassports[$buildId]['floors'];
  568. $result[$buildId]['entrance'] = $this->buildPassports[$buildId]['entrances'];
  569. } else {
  570. $result[$buildId]['floor'] = '';
  571. $result[$buildId]['entrance'] = '';
  572. }
  573. if (ispos($buildData['buildnum'], '/')) {
  574. $buildExpl = explode('/', $buildData['buildnum']);
  575. @$buildNumber = vf($buildExpl[0], 3);
  576. @$blockLetter = $buildExpl[1];
  577. } else {
  578. $buildNumber = vf($buildData['buildnum'], 3);
  579. $blockLetter = preg_replace('/\P{L}+/u', '', $buildData['buildnum']);
  580. $blockLetter = trim($blockLetter);
  581. }
  582. $result[$buildId]['number'] = $buildNumber;
  583. $result[$buildId]['block'] = $blockLetter;
  584. $result[$buildId]['coordinates'] = $buildData['geo'];
  585. }
  586. }
  587. return ($result);
  588. }
  589. /**
  590. * Returns customfield types data
  591. *
  592. * @return array
  593. */
  594. protected function getCFTypesData() {
  595. $result = array();
  596. if (!empty($this->allCfTypes)) {
  597. foreach ($this->allCfTypes as $cftypeId => $eachCftypeName) {
  598. $result[$cftypeId]['id'] = $cftypeId;
  599. $result[$cftypeId]['name'] = $eachCftypeName;
  600. }
  601. }
  602. return ($result);
  603. }
  604. /**
  605. * Returns users states data
  606. *
  607. * @return array
  608. */
  609. protected function getUsersStateList() {
  610. $result = array();
  611. $result[5]['id'] = 5;
  612. $result[5]['name'] = __('Active');
  613. $result[5]['functional'] = 'work';
  614. $result[1]['id'] = 1;
  615. $result[1]['name'] = __('Debt');
  616. $result[1]['functional'] = 'nomoney';
  617. $result[2]['id'] = 2;
  618. $result[2]['name'] = __('User passive');
  619. $result[2]['functional'] = 'pause';
  620. $result[3]['id'] = 3;
  621. $result[3]['name'] = __('User down');
  622. $result[3]['functional'] = 'disable';
  623. $result[4]['id'] = 4;
  624. $result[4]['name'] = __('No tariff');
  625. $result[4]['functional'] = 'new';
  626. return ($result);
  627. }
  628. /**
  629. * Returns available tag types data
  630. *
  631. * @return array
  632. */
  633. protected function getTagTypesList() {
  634. $result = array();
  635. /* Правда твоя - теплый асфальт */
  636. if (!empty($this->allTagTypes)) {
  637. foreach ($this->allTagTypes as $tagId => $eachTagName) {
  638. $result[$tagId]['id'] = $tagId;
  639. $result[$tagId]['name'] = $eachTagName;
  640. }
  641. }
  642. return ($result);
  643. }
  644. /**
  645. * Returns available methods array
  646. *
  647. * @return array
  648. */
  649. protected function getMethodsList() {
  650. $result = array();
  651. if (!empty($this->supportedMethods)) {
  652. foreach ($this->supportedMethods as $io => $each) {
  653. $result[$io]['comment'] = $each;
  654. }
  655. }
  656. return ($result);
  657. }
  658. /**
  659. * Returns available change methods list
  660. *
  661. * @return array
  662. */
  663. protected function getChangeMethodsList() {
  664. $result = array();
  665. if (!empty($this->supportedChangeMethods)) {
  666. foreach ($this->supportedChangeMethods as $io => $each) {
  667. $result[$io]['comment'] = $each;
  668. }
  669. }
  670. return ($result);
  671. }
  672. /**
  673. * Returns supported change user state options
  674. *
  675. * @return array
  676. */
  677. protected function getChangeStateMethodsList() {
  678. $result = array();
  679. if (!empty($this->supportedChageUserState)) {
  680. foreach ($this->supportedChageUserState as $io => $each) {
  681. $result[$io] = $each;
  682. }
  683. }
  684. return ($result);
  685. }
  686. /**
  687. * Returns available devices types
  688. *
  689. * @return array
  690. */
  691. protected function getDeviceTypesList() {
  692. $result = $this->allDeviceTypes;
  693. return ($result);
  694. }
  695. /**
  696. * Returns available device models
  697. *
  698. * @return array
  699. */
  700. protected function getDeviceModels() {
  701. $result = array();
  702. if (!empty($this->allSwitchModels)) {
  703. foreach ($this->allSwitchModels as $io => $each) {
  704. $result[$each['id']]['id'] = $each['id'];
  705. $result[$each['id']]['type_id'] = ''; //empty now, because model don't know anything about this
  706. $result[$each['id']]['name'] = $each['modelname'];
  707. $result[$each['id']]['iface_count'] = $each['ports'];
  708. }
  709. }
  710. return ($result);
  711. }
  712. /**
  713. * Returns list of available devices in database
  714. *
  715. * @param string $types
  716. *
  717. * @return array
  718. */
  719. protected function getDevicesList($types = '') {
  720. $result = array();
  721. if (!empty($this->allSwitches)) {
  722. $typesFilter = array(); //ids of device types for result rendering
  723. $switchCemetery = zb_SwitchesGetAllDeathTime(); //getting currently dead switches list
  724. if (!empty($types)) {
  725. //filters preprocessing
  726. $filtersTmp = explode(',', $types);
  727. if (!empty($filtersTmp)) {
  728. foreach ($filtersTmp as $ia => $eachFilter) {
  729. $tidTmp = array_search($eachFilter, $this->allDeviceTypes);
  730. if ($tidTmp) {
  731. $typesFilter[$tidTmp] = $this->allDeviceTypes[$tidTmp];
  732. }
  733. }
  734. }
  735. }
  736. foreach ($this->allSwitches as $io => $each) {
  737. //setting device type
  738. $deviceType = $this->getDeviceType($each['id']);
  739. //applying filters if required
  740. if (empty($typesFilter)) {
  741. $filteredFlag = true;
  742. } else {
  743. if (isset($typesFilter[$deviceType])) {
  744. $filteredFlag = true;
  745. } else {
  746. $filteredFlag = false;
  747. }
  748. }
  749. $switchIp = $each['ip'];
  750. if (isset($switchCemetery[$switchIp])) {
  751. $lastActivityTime = $switchCemetery[$switchIp];
  752. } else {
  753. $lastActivityTime = curdatetime();
  754. }
  755. $switchMac = (check_mac_format($each['swid'])) ? $each['swid'] : '';
  756. if ($filteredFlag) {
  757. $result[$each['id']]['id'] = $each['id'];
  758. $result[$each['id']]['type_id'] = $deviceType;
  759. $result[$each['id']]['model_id'] = $each['modelid'];
  760. $result[$each['id']]['ip'] = $each['ip'];
  761. $result[$each['id']]['mac'] = $switchMac; //requires SWITCHES_EXTENDED option enabled.
  762. $result[$each['id']]['house_id'] = ''; //no normal topology points at this moment
  763. $result[$each['id']]['entrance'] = '';
  764. $result[$each['id']]['floor'] = '';
  765. $result[$each['id']]['node_id'] = '';
  766. $result[$each['id']]['location'] = $each['location'];
  767. $result[$each['id']]['geo'] = $each['geo'];
  768. $result[$each['id']]['comment'] = $each['desc'];
  769. $result[$each['id']]['date_activity'] = $lastActivityTime;
  770. $result[$each['id']]['date_create'] = '';
  771. $result[$each['id']]['snmp_version'] = '2c';
  772. $result[$each['id']]['snmp_port'] = '161';
  773. $result[$each['id']]['snmp_read_community'] = $each['snmp'];
  774. $result[$each['id']]['software_version'] = '';
  775. }
  776. }
  777. }
  778. return ($result);
  779. }
  780. /**
  781. * Returns device type id
  782. *
  783. * @param int $deviceId
  784. *
  785. * @return int
  786. */
  787. protected function getDeviceType($deviceId) {
  788. //switch by default
  789. $result = 1;
  790. if (isset($this->allSwitches[$deviceId])) {
  791. if (ispos($this->allSwitches[$deviceId]['desc'], 'OLT')) {
  792. $result = 3;
  793. }
  794. if ((ispos($this->allSwitches[$deviceId]['desc'], 'MTSIGMON')) OR ( ispos($this->allSwitches[$deviceId]['desc'], 'AP')) OR ( ispos($this->allSwitches[$deviceId]['desc'], 'ssid:'))) {
  795. $result = 2;
  796. }
  797. }
  798. return ($result);
  799. }
  800. /**
  801. * Returns array of devices connection topology
  802. *
  803. * @return array
  804. */
  805. protected function getDeviceConnectionsList() {
  806. $result = array();
  807. if (!empty($this->allSwitches)) {
  808. foreach ($this->allSwitches as $io => $each) {
  809. //setting device type
  810. $deviceType = $this->getDeviceType($each['id']);
  811. // We dont know anyting about is. Like John Snow, yeah.
  812. $uplinkPort = 1;
  813. //detecting, have device uplinks or not?
  814. if (!empty($each['parentid'])) {
  815. //uplinks description
  816. $result[$this->allDeviceTypes[$deviceType]][$each['id']][1][$uplinkPort][] = array(
  817. 'type' => $this->allDeviceTypes[$this->getDeviceType($each['parentid'])],
  818. 'id' => $each['parentid'],
  819. 'direction' => 1,
  820. 'interface' => $uplinkPort);
  821. //downlinks description
  822. $result[$this->allDeviceTypes[$this->getDeviceType($each['parentid'])]][$each['parentid']][1][$uplinkPort][] = array(
  823. 'type' => $this->allDeviceTypes[$deviceType],
  824. 'id' => $each['id'],
  825. 'direction' => 1,
  826. 'interface' => 2
  827. );
  828. }
  829. }
  830. }
  831. return ($result);
  832. }
  833. /**
  834. * Returns Userside API information
  835. *
  836. * @return array
  837. */
  838. protected function getApiInformation() {
  839. $result = array();
  840. $result['version'] = self::API_VER;
  841. $result['date'] = self::API_DATE;
  842. return ($result);
  843. }
  844. /**
  845. * Returns server system information
  846. *
  847. * @return array
  848. */
  849. protected function getSystemInformation() {
  850. $result = array();
  851. $curdate = curdatetime();
  852. $operatingSystem = shell_exec('uname');
  853. $billingVersion = file_get_contents('RELEASE');
  854. $result['date'] = $curdate;
  855. $result['os'] = trim($operatingSystem);
  856. $result['billing']['name'] = 'Ubilling';
  857. $result['billing']['version'] = trim($billingVersion);
  858. return ($result);
  859. }
  860. /**
  861. * Returns array of all tagtypes set to users
  862. *
  863. * @return array
  864. */
  865. protected function getAllUsersTags() {
  866. $result = array();
  867. $query = "SELECT * from `tags`";
  868. $all = simple_queryall($query);
  869. if (!empty($all)) {
  870. foreach ($all as $io => $each) {
  871. $result[$each['login']][] = $each['tagid'];
  872. }
  873. }
  874. return ($result);
  875. }
  876. /**
  877. * Returns array of all available PaymentIDs
  878. *
  879. * @return array
  880. */
  881. protected function getAllUserPaymentIds() {
  882. $result = array();
  883. if ($this->altCfg['OPENPAYZ_SUPPORT']) {
  884. if ($this->altCfg['OPENPAYZ_REALID']) {
  885. $query = "SELECT * from `op_customers`";
  886. $all = simple_queryall($query);
  887. if (!empty($all)) {
  888. foreach ($all as $io => $each) {
  889. $result[$each['realid']] = $each['virtualid'];
  890. }
  891. }
  892. } else {
  893. if (!empty($this->allUserData)) {
  894. foreach (@$this->allUserData as $io => $each) {
  895. $result[$each['login']] = ip2int($each['IP']);
  896. }
  897. }
  898. }
  899. }
  900. return ($result);
  901. }
  902. /**
  903. * Returns array of all available login=>apt bindings
  904. *
  905. * @return array
  906. */
  907. protected function getAllAddressList() {
  908. $result = array();
  909. $query = "SELECT * from `address`";
  910. $all = simple_queryall($query);
  911. if (!empty($all)) {
  912. foreach ($all as $io => $each) {
  913. $result[$each['login']] = $each['aptid'];
  914. }
  915. }
  916. return ($result);
  917. }
  918. /**
  919. * Returns array of all available apartments data as id=>data
  920. *
  921. * @return array
  922. */
  923. protected function getAllAptList() {
  924. $result = array();
  925. $query = "SELECT * from `apt`";
  926. $all = simple_queryall($query);
  927. if (!empty($all)) {
  928. foreach ($all as $io => $each) {
  929. $result[$each['id']] = $each;
  930. }
  931. }
  932. return ($result);
  933. }
  934. /**
  935. * Returns array of all nethosts data as ip=>data
  936. *
  937. * @return array
  938. */
  939. protected function getNethostsData() {
  940. $result = array();
  941. $query = "SELECT * from `nethosts`";
  942. $all = simple_queryall($query);
  943. if (!empty($all)) {
  944. foreach ($all as $io => $each) {
  945. $result[$each['ip']] = $each;
  946. }
  947. }
  948. return ($result);
  949. }
  950. /**
  951. * Returns data of available multinet networks as netid=>data
  952. *
  953. * @return array
  954. */
  955. protected function getNetworksData() {
  956. $result = array();
  957. $query = "SELECT * from `networks`";
  958. $all = simple_queryall($query);
  959. if (!empty($all)) {
  960. foreach ($all as $io => $each) {
  961. $result[$each['id']] = $each;
  962. }
  963. }
  964. return ($result);
  965. }
  966. /**
  967. * Returns all users support tickets
  968. *
  969. * @return array
  970. */
  971. protected function getUsersMessages() {
  972. $result = array();
  973. $query = "SELECT * from `ticketing` ORDER BY `id` ASC;";
  974. $all = simple_queryall($query);
  975. if (!empty($all)) {
  976. foreach ($all as $io => $each) {
  977. if (($each['from'] != 'NULL') AND ( empty($each['replyid']))) {
  978. //original ticket body
  979. $result[$each['id']]['id'] = $each['id'];
  980. $result[$each['id']]['user_id'] = $each['from'];
  981. $result[$each['id']]['msg_date'] = $each['date'];
  982. $result[$each['id']]['subject'] = __('Ticket') . ' ' . $each['date'];
  983. $result[$each['id']]['text'] = $each['text'];
  984. } else {
  985. //thats replies for a ticket
  986. if ((isset($result[$each['replyid']]))) {
  987. $replyAuthor = (!empty($each['admin'])) ? $each['admin'] : $each['from'];
  988. $result[$each['replyid']]['text'] .= PHP_EOL . '=========' . PHP_EOL;
  989. $result[$each['replyid']]['text'] .= PHP_EOL . __('Message') . ' ' . $each['date'] . ' (' . $replyAuthor . ')';
  990. $result[$each['replyid']]['text'] .= ': ' . PHP_EOL . $each['text'] . PHP_EOL;
  991. }
  992. }
  993. }
  994. }
  995. return ($result);
  996. }
  997. /**
  998. * Returns existing users full info
  999. *
  1000. * @param string $customerId
  1001. *
  1002. * @return array
  1003. */
  1004. protected function getUsersList($customerId = '') {
  1005. $result = array();
  1006. $allRealNames = zb_UserGetAllRealnames();
  1007. $allContracts = zb_UserGetAllContracts();
  1008. $allUserTags = $this->getAllUsersTags();
  1009. $allUserNotes = zb_UserGetAllNotes();
  1010. $allPaymentIds = $this->getAllUserPaymentIds();
  1011. $allAddresBindings = $this->getAllAddressList();
  1012. $allAptData = $this->getAllAptList();
  1013. $allPhones = zb_UserGetAllPhoneData();
  1014. $allEmails = zb_UserGetAllEmails();
  1015. $allNethosts = $this->getNethostsData();
  1016. $allNetworks = $this->getNetworksData();
  1017. $allRegData = $this->getUserRegData();
  1018. $scanData = array();
  1019. $hiddenDeadUsers = array();
  1020. if (@$this->altCfg['DEAD_HIDE'] AND @ $this->altCfg['DEAD_HIDE_USERSIDE']) {
  1021. $deadTagId = @$this->altCfg['DEAD_TAGID'];
  1022. if (!empty($deadTagId)) {
  1023. if (!empty($allUserTags)) {
  1024. foreach ($allUserTags as $dTagLogin => $dTagData) {
  1025. if (!empty($dTagData)) {
  1026. foreach ($dTagData as $dTagIndex => $dTagId) {
  1027. if ($dTagId == $deadTagId) {
  1028. $hiddenDeadUsers[$dTagLogin] = $dTagId;
  1029. }
  1030. }
  1031. }
  1032. }
  1033. }
  1034. }
  1035. }
  1036. if (!empty($allContracts)) {
  1037. $allContracts = array_flip($allContracts);
  1038. }
  1039. $contractDates = new ContractDates();
  1040. $allContractDates = $contractDates->getAllDatesBasic();
  1041. //just one user
  1042. if (!empty($customerId)) {
  1043. if (isset($this->allUserData[$customerId])) {
  1044. $scanData[$customerId] = $this->allUserData[$customerId];
  1045. } else {
  1046. $scanData = array();
  1047. }
  1048. } else {
  1049. $scanData = $this->allUserData;
  1050. }
  1051. if (!empty($scanData)) {
  1052. foreach ($scanData as $userLogin => $userData) {
  1053. if (!isset($hiddenDeadUsers[$userLogin])) {
  1054. $result[$userLogin]['id'] = $userLogin;
  1055. $result[$userLogin]['login'] = $userLogin;
  1056. $result[$userLogin]['full_name'] = @$allRealNames[$userLogin];
  1057. $result[$userLogin]['flag_corporate'] = 0;
  1058. if ($userData['TariffChange']) {
  1059. $curMonth = date('n');
  1060. $curYear = date('Y');
  1061. $firstDayNextMonth = ($curMonth == 12) ? mktime(0, 0, 0, 0, 0, $curYear + 1) : mktime(0, 0, 0, $curMonth + 1, 1);
  1062. $firstDayNextMonth = date("Y-m-d", $firstDayNextMonth);
  1063. } else {
  1064. $firstDayNextMonth = '';
  1065. }
  1066. $result[$userLogin]['tariff']['current'][0]['id'] = $userData['Tariff'];
  1067. if ($firstDayNextMonth) {
  1068. $result[$userLogin]['tariff']['current'][0]['date_finish'] = $firstDayNextMonth;
  1069. }
  1070. if ($userData['TariffChange']) {
  1071. $result[$userLogin]['tariff']['new'][0]['id'] = $userData['TariffChange'];
  1072. $result[$userLogin]['tariff']['new'][0]['date_start'] = $firstDayNextMonth;
  1073. }
  1074. $userContract = @$allContracts[$userLogin];
  1075. if ($userContract) {
  1076. $result[$userLogin]['agreement'][0]['number'] = $userContract;
  1077. $contractDate = @$allContractDates[$userContract];
  1078. if ($contractDate) {
  1079. $result[$userLogin]['agreement'][0]['date'] = $contractDate;
  1080. }
  1081. }
  1082. $result[$userLogin]['account_number'] = @$allPaymentIds[$userLogin]; // yep, this is something like Payment ID
  1083. if (isset($allUserTags[$userLogin])) {
  1084. foreach ($allUserTags[$userLogin] as $tagIo => $eachTagid) {
  1085. $result[$userLogin]['group'][$eachTagid] = $eachTagid;
  1086. }
  1087. }
  1088. $userNotes = @$allUserNotes[$userLogin];
  1089. if ($userNotes) {
  1090. $result[$userLogin]['comment'] = $userNotes;
  1091. }
  1092. $result[$userLogin]['balance'] = $userData['Cash'];
  1093. $result[$userLogin]['credit'] = $userData['Credit'];
  1094. $userState = 5; // work
  1095. if ($userData['Cash'] < '-' . $userData['Credit']) {
  1096. $userState = 1; //nomoney
  1097. }
  1098. if ($userData['Passive'] == 1) {
  1099. $userState = 2; // pause
  1100. }
  1101. if ($userData['Down'] == 1) {
  1102. $userState = 3; // disable
  1103. }
  1104. if ($userData['Tariff'] == '*_NO_TARIFF_*') {
  1105. $userState = 4; // new
  1106. }
  1107. $result[$userLogin]['state_id'] = $userState;
  1108. if (isset($allRegData[$userLogin])) {
  1109. $result[$userLogin]['date_create'] = $allRegData[$userLogin];
  1110. $result[$userLogin]['date_connect'] = $allRegData[$userLogin];
  1111. } else {
  1112. $result[$userLogin]['date_create'] = '';
  1113. $result[$userLogin]['date_connect'] = '';
  1114. }
  1115. if ($this->altCfg['DN_FULLHOSTSCAN']) {
  1116. $dnFilePath = DATA_PATH . 'dn/' . $userLogin;
  1117. if (file_exists($dnFilePath)) {
  1118. $actTimestamp = filemtime($dnFilePath);
  1119. $result[$userLogin]['date_activity'] = date("Y-m-d H:i:s", $actTimestamp);
  1120. } else {
  1121. $result[$userLogin]['date_activity'] = '';
  1122. }
  1123. } else {
  1124. $result[$userLogin]['date_activity'] = date("Y-m-d H:i:s", $userData['LastActivityTime']);
  1125. }
  1126. $result[$userLogin]['traffic']['month']['up'] = $userData['U0'];
  1127. $result[$userLogin]['traffic']['month']['down'] = $userData['D0'];
  1128. $result[$userLogin]['discount'] = 0; // TODO: to many discount models at this time
  1129. $userApartmentId = @$allAddresBindings[$userLogin];
  1130. if ($userApartmentId) {
  1131. $aptData = $allAptData[$userApartmentId];
  1132. $result[$userLogin]['address'][0]['type'] = 'connect';
  1133. $result[$userLogin]['address'][0]['house_id'] = $aptData['buildid'];
  1134. $result[$userLogin]['address'][0]['apartment']['id'] = $userApartmentId;
  1135. $result[$userLogin]['address'][0]['apartment']['full_name'] = $aptData['apt'];
  1136. $result[$userLogin]['address'][0]['apartment']['number'] = vf($aptData['apt'], 3);
  1137. if ($aptData['entrance']) {
  1138. $result[$userLogin]['address'][0]['entrance'] = $aptData['entrance'];
  1139. }
  1140. if ($aptData['floor']) {
  1141. $result[$userLogin]['address'][0]['floor'] = $aptData['floor'];
  1142. }
  1143. }
  1144. $userPhoneData = @$allPhones[$userLogin];
  1145. if (!empty($userPhoneData)) {
  1146. if (isset($userPhoneData['phone'])) {
  1147. $result[$userLogin]['phone'][0]['number'] = $userPhoneData['phone'];
  1148. $result[$userLogin]['phone'][0]['flag_main'] = 0;
  1149. }
  1150. if (isset($userPhoneData['mobile'])) {
  1151. $result[$userLogin]['phone'][1]['number'] = $userPhoneData['mobile'];
  1152. $result[$userLogin]['phone'][1]['flag_main'] = 1;
  1153. }
  1154. }
  1155. $userEmail = @$allEmails[$userLogin];
  1156. if ($userEmail) {
  1157. $result[$userLogin]['email'][0]['address'] = $userEmail;
  1158. $result[$userLogin]['email'][0]['flag_main'] = 1;
  1159. }
  1160. $userIp = $userData['IP'];
  1161. $userIp = ip2int($userIp);
  1162. $result[$userLogin]['ip_mac'][0]['ip'] = $userIp;
  1163. $nethostsData = @$allNethosts[$userData['IP']];
  1164. if (!empty($nethostsData)) {
  1165. $subnetId = $nethostsData['netid'];
  1166. $userMac = $nethostsData['mac'];
  1167. $userMac = str_replace(':', '', $userMac);
  1168. $userMac = strtolower($userMac); // mac lowercased withot delimiters
  1169. $result[$userLogin]['ip_mac'][0]['mac'] = $userMac;
  1170. $result[$userLogin]['ip_mac'][0]['ip_net'] = @$allNetworks[$subnetId]['desc'];
  1171. }
  1172. if (isset($allUserTags[$userLogin])) {
  1173. foreach ($allUserTags[$userLogin] as $tagIo => $eachTagid) {
  1174. $result[$userLogin]['tag'][$eachTagid]['id'] = $eachTagid;
  1175. $result[$userLogin]['tag'][$eachTagid]['date_add'] = '';
  1176. }
  1177. }
  1178. if (isset($allUserTags[$userLogin])) {
  1179. $srvCount = 0;
  1180. foreach ($allUserTags[$userLogin] as $tagIo => $eachTagid) {
  1181. if (isset($this->serviceTagMappings[$eachTagid])) {
  1182. $serviceId = $this->serviceTagMappings[$eachTagid];
  1183. if (isset($result[$userLogin]['service'][$serviceId])) {
  1184. $srvCount++;
  1185. } else {
  1186. $srvCount = 0;
  1187. }
  1188. $result[$userLogin]['service'][$serviceId][$srvCount]['cost'] = $this->vServices[$serviceId]['price'];
  1189. $result[$userLogin]['service'][$serviceId][$srvCount]['date_add'] = '';
  1190. $result[$userLogin]['service'][$serviceId][$srvCount]['comment'] = '';
  1191. }
  1192. }
  1193. }
  1194. if (isset($this->allCfData[$userLogin])) {
  1195. $result[$userLogin]['additional_data'] = $this->allCfData[$userLogin];
  1196. }
  1197. $result[$userLogin]['password'] = $userData['Password'];
  1198. }
  1199. }
  1200. }
  1201. return ($result);
  1202. }
  1203. /**
  1204. * Returns array of available tags
  1205. *
  1206. * @return array
  1207. */
  1208. protected function getUserTags() {
  1209. $result = array();
  1210. if (!empty($this->allTagTypes)) {
  1211. foreach ($this->allTagTypes as $tagId => $tagName) {
  1212. $result[$tagId]['id'] = $tagId;
  1213. $result[$tagId]['name'] = $tagName;
  1214. }
  1215. }
  1216. return ($result);
  1217. }
  1218. /**
  1219. * Returns list of available services
  1220. *
  1221. * @return array
  1222. */
  1223. protected function getServicesList() {
  1224. $result = array();
  1225. if (!empty($this->vServices)) {
  1226. foreach ($this->vServices as $serviceId => $serviceData) {
  1227. $result[$serviceId]['id'] = $serviceId;
  1228. $result[$serviceId]['name'] = $this->allTagTypes[$serviceData['tagid']];
  1229. $result[$serviceId]['cost'] = $serviceData['price'];
  1230. }
  1231. }
  1232. return ($result);
  1233. }
  1234. /**
  1235. * Returns users finance operations history
  1236. *
  1237. * @param string $customerId
  1238. *
  1239. * @return array
  1240. */
  1241. protected function getUserFinanceHistory($customerId) {
  1242. $result = array();
  1243. if (isset($this->allUserData[$customerId])) {
  1244. $allServices = zb_VservicesGetAllNamesLabeled();
  1245. $fundsFlow = new FundsFlow();
  1246. $allfees = $fundsFlow->getFees($customerId);
  1247. $allpayments = $fundsFlow->getPayments($customerId);
  1248. $allcorrectings = $fundsFlow->getPaymentsCorr($customerId);
  1249. $allOps = $allfees + $allpayments + $allcorrectings;
  1250. $allOps = $fundsFlow->transformArray($allOps);
  1251. $i = 0;
  1252. if (!empty($allOps)) {
  1253. foreach ($allOps as $io => $each) {
  1254. // print_r($each);
  1255. $result[] = array(
  1256. 'id' => $i,
  1257. 'date' => $each['date'],
  1258. 'type' => 'financial',
  1259. 'name' => __($each['operation']),
  1260. 'data' => json_encode(array(
  1261. 'amount' => $each['summ'],
  1262. 'from' => $each['from'],
  1263. 'to' => $each['to'],
  1264. 'operator_name' => $each['admin']
  1265. )),
  1266. 'comment' => zb_TranslatePaymentNote($each['note'], $allServices)
  1267. );
  1268. $i++;
  1269. }
  1270. }
  1271. } else {
  1272. $result = array('result' => 'error', 'error' => $this->errorNotices['EX_USER_NOT_EXISTS'] . ': ' . $customerId);
  1273. }
  1274. return ($result);
  1275. }
  1276. /**
  1277. * Catches and preprocess change_user_data request params
  1278. *
  1279. * @return array
  1280. */
  1281. protected function catchChangeParams() {
  1282. $result = array();
  1283. /**
  1284. * There is a house in New Orleans
  1285. * They call the Rising Sun
  1286. * And it's been the ruin of many a poor boy
  1287. * And God, I know I'm one
  1288. */
  1289. if (wf_CheckGet(array('customer_id'))) {
  1290. $result['customerid'] = $_GET['customer_id'];
  1291. }
  1292. if (wf_CheckGet(array('type'))) {
  1293. $result['type'] = vf($_GET['type']);
  1294. }
  1295. if (wf_CheckGet(array('value'))) {
  1296. $result['value'] = $_GET['value'];
  1297. }
  1298. if (wf_CheckGet(array('comment'))) {
  1299. $result['comment'] = $_GET['comment'];
  1300. }
  1301. return ($result);
  1302. }
  1303. /**
  1304. * Do some user finance data changes
  1305. *
  1306. * @param array $changeParams
  1307. *
  1308. * @return array
  1309. */
  1310. protected function changeUserFinance($changeParams) {
  1311. $result = array();
  1312. if (isset($changeParams['customerid'])) {
  1313. if (isset($this->allUserData[$changeParams['customerid']])) {
  1314. if (isset($changeParams['value'])) {
  1315. if (zb_checkMoney($changeParams['value'])) {
  1316. $paymentNotes = (isset($changeParams['comment'])) ? $changeParams['comment'] : '';
  1317. $cashTypeId = 1; //like default
  1318. if (isset($this->altCfg['USERSIDE_CASHTYPE'])) {
  1319. if (!empty($this->altCfg['USERSIDE_CASHTYPE'])) {
  1320. $cashTypeId = $this->altCfg['USERSIDE_CASHTYPE'];
  1321. }
  1322. }
  1323. zb_CashAdd($changeParams['customerid'], $changeParams['value'], 'add', $cashTypeId, $paymentNotes);
  1324. $result = array('result' => 'ok');
  1325. } else {
  1326. $result = array('result' => 'error', 'error' => $this->errorNotices['EX_BAD_MONEY_FORMAT'] . ': ' . $changeParams['value']);
  1327. }
  1328. } else {
  1329. $result = array('result' => 'error', 'error' => $this->errorNotices['EX_PARAM_MISSED'] . ': value');
  1330. }
  1331. } else {
  1332. $result = array('result' => 'error', 'error' => $this->errorNotices['EX_USER_NOT_EXISTS'] . ': ' . $changeParams['customerid']);
  1333. }
  1334. } else {
  1335. $result = array('result' => 'error', 'error' => $this->errorNotices['EX_PARAM_MISSED'] . ': customer_id');
  1336. }
  1337. return ($result);
  1338. }
  1339. /**
  1340. * Do some user finance data correct
  1341. *
  1342. * @param array $changeParams
  1343. *
  1344. * @return array
  1345. */
  1346. protected function changeUserFinanceCorrect($changeParams) {
  1347. $result = array();
  1348. if (isset($changeParams['customerid'])) {
  1349. if (isset($this->allUserData[$changeParams['customerid']])) {
  1350. if (isset($changeParams['value'])) {
  1351. if (zb_checkMoney($changeParams['value'])) {
  1352. $paymentNotes = (isset($changeParams['comment'])) ? $changeParams['comment'] : '';
  1353. $cashTypeId = 1; //like default
  1354. if (isset($this->altCfg['USERSIDE_CASHTYPE'])) {
  1355. if (!empty($this->altCfg['USERSIDE_CASHTYPE'])) {
  1356. $cashTypeId = $this->altCfg['USERSIDE_CASHTYPE'];
  1357. }
  1358. }
  1359. zb_CashAdd($changeParams['customerid'], $changeParams['value'], 'correct', $cashTypeId, $paymentNotes);
  1360. $result = array('result' => 'ok');
  1361. } else {
  1362. $result = array('result' => 'error', 'error' => $this->errorNotices['EX_BAD_MONEY_FORMAT'] . ': ' . $changeParams['value']);
  1363. }
  1364. } else {
  1365. $result = array('result' => 'error', 'error' => $this->errorNotices['EX_PARAM_MISSED'] . ': value');
  1366. }
  1367. } else {
  1368. $result = array('result' => 'error', 'error' => $this->errorNotices['EX_USER_NOT_EXISTS'] . ': ' . $changeParams['customerid']);
  1369. }
  1370. } else {
  1371. $result = array('result' => 'error', 'error' => $this->errorNotices['EX_PARAM_MISSED'] . ': customer_id');
  1372. }
  1373. return ($result);
  1374. }
  1375. /**
  1376. * Changes user RealName
  1377. *
  1378. * @param array $changeParams
  1379. *
  1380. * @return array
  1381. */
  1382. protected function changeUserRealName($changeParams) {
  1383. $result = array();
  1384. if (isset($changeParams['customerid'])) {
  1385. if (isset($this->allUserData[$changeParams['customerid']])) {
  1386. if (isset($changeParams['value'])) {
  1387. zb_UserChangeRealName($changeParams['customerid'], $changeParams['value']);
  1388. $result = array('result' => 'ok');
  1389. } else {
  1390. $result = array('result' => 'error', 'error' => $this->errorNotices['EX_PARAM_MISSED'] . ': value');
  1391. }
  1392. } else {
  1393. $result = array('result' => 'error', 'error' => $this->errorNotices['EX_USER_NOT_EXISTS'] . ': ' . $changeParams['customerid']);
  1394. }
  1395. } else {
  1396. $result = array('result' => 'error', 'error' => $this->errorNotices['EX_PARAM_MISSED'] . ': customer_id');
  1397. }
  1398. return ($result);
  1399. }
  1400. /**
  1401. * Changes user notes
  1402. *
  1403. * @param array $changeParams
  1404. *
  1405. * @return array
  1406. */
  1407. protected function changeUserNotes($changeParams) {
  1408. $result = array();
  1409. if (isset($changeParams['customerid'])) {
  1410. if (isset($this->allUserData[$changeParams['customerid']])) {
  1411. if (isset($changeParams['value'])) {
  1412. zb_UserDeleteNotes($changeParams['customerid']);
  1413. zb_UserCreateNotes($changeParams['customerid'], $changeParams['value']);
  1414. $result = array('result' => 'ok');
  1415. } else {
  1416. $result = array('result' => 'error', 'error' => $this->errorNotices['EX_PARAM_MISSED'] . ': value');
  1417. }
  1418. } else {
  1419. $result = array('result' => 'error', 'error' => $this->errorNotices['EX_USER_NOT_EXISTS'] . ': ' . $changeParams['customerid']);
  1420. }
  1421. } else {
  1422. $result = array('result' => 'error', 'error' => $this->errorNotices['EX_PARAM_MISSED'] . ': customer_id');
  1423. }
  1424. return ($result);
  1425. }
  1426. /**
  1427. * Changes user tariff
  1428. *
  1429. * @param array $changeParams
  1430. *
  1431. * @return array
  1432. */
  1433. protected function changeUserTariff($changeParams) {
  1434. $result = array();
  1435. global $billing;
  1436. if (isset($changeParams['customerid'])) {
  1437. if (isset($this->allUserData[$changeParams['customerid']])) {
  1438. if (isset($changeParams['value'])) {
  1439. if (isset($this->allTariffs[$changeParams['value']])) {
  1440. $newTariff = $changeParams['value'];
  1441. $billing->settariff($changeParams['customerid'], $newTariff);
  1442. log_register('CHANGE Tariff (' . $changeParams['customerid'] . ') ON `' . $newTariff . '`');
  1443. if ($this->altCfg['TARIFFCHGRESET']) {
  1444. $billing->resetuser($changeParams['customerid']);
  1445. log_register('RESET User (' . $changeParams['customerid'] . ')');
  1446. }
  1447. $result = array('result' => 'ok');
  1448. } else {
  1449. $result = array('result' => 'error', 'error' => $this->errorNotices['EX_WRONG_TARIFF'] . ': ' . $changeParams['value']);
  1450. }
  1451. } else {
  1452. $result = array('result' => 'error', 'error' => $this->errorNotices['EX_PARAM_MISSED'] . ': value');
  1453. }
  1454. } else {
  1455. $result = array('result' => 'error', 'error' => $this->errorNotices['EX_USER_NOT_EXISTS'] . ': ' . $changeParams['customerid']);
  1456. }
  1457. } else {
  1458. $result = array('result' => 'error', 'error' => $this->errorNotices['EX_PARAM_MISSED'] . ': customer_id');
  1459. }
  1460. return ($result);
  1461. }
  1462. /**
  1463. * Changes user basic state
  1464. *
  1465. * @global object $billing
  1466. * @param array $changeParams
  1467. *
  1468. * @return array
  1469. */
  1470. protected function changeUserState($changeParams) {
  1471. global $billing;
  1472. $result = array();
  1473. if (isset($changeParams['customerid'])) {
  1474. if (isset($this->allUserData[$changeParams['customerid']])) {
  1475. if (isset($changeParams['value'])) {
  1476. $newState = $changeParams['value'];
  1477. if (isset($this->supportedChageUserState[$newState])) {
  1478. switch ($newState) {
  1479. case 'frozen':
  1480. $billing->setpassive($changeParams['customerid'], 1);
  1481. log_register('CHANGE Passive (' . $changeParams['customerid'] . ') ON 1');
  1482. $result = array('result' => 'ok');
  1483. break;
  1484. case 'unfrozen':
  1485. $billing->setpassive($changeParams['customerid'], 0);
  1486. log_register('CHANGE Passive (' . $changeParams['customerid'] . ') ON 0');
  1487. $result = array('result' => 'ok');
  1488. break;
  1489. case 'down':
  1490. $billing->setdown($changeParams['customerid'], 1);
  1491. log_register('CHANGE Down (' . $changeParams['customerid'] . ') ON 1');
  1492. $result = array('result' => 'ok');
  1493. break;
  1494. case 'notdown':
  1495. $billing->setdown($changeParams['customerid'], 0);
  1496. log_register('CHANGE Down (' . $changeParams['customerid'] . ') ON 0');
  1497. $result = array('result' => 'ok');
  1498. break;
  1499. case 'ao':
  1500. $billing->setao($changeParams['customerid'], 1);
  1501. log_register('CHANGE AlwaysOnline (' . $changeParams['customerid'] . ') ON 1');
  1502. $result = array('result' => 'ok');
  1503. break;
  1504. case 'notao':
  1505. $billing->setao($changeParams['customerid'], 0);
  1506. log_register('CHANGE AlwaysOnline (' . $changeParams['customerid'] . ') ON 0');
  1507. $result = array('result' => 'ok');
  1508. break;
  1509. }
  1510. } else {
  1511. $result = array('result' => 'error', 'error' => $this->errorNotices['EX_METHOD_NOT_SUPPORTED'] . ': ' . $newState);
  1512. }
  1513. } else {
  1514. $result = array('result' => 'error', 'error' => $this->errorNotices['EX_PARAM_MISSED'] . ': value');
  1515. }
  1516. } else {
  1517. $result = array('result' => 'error', 'error' => $this->errorNotices['EX_USER_NOT_EXISTS'] . ': ' . $changeParams['customerid']);
  1518. }
  1519. } else {
  1520. $result = array('result' => 'error', 'error' => $this->errorNotices['EX_PARAM_MISSED'] . ': customer_id');
  1521. }
  1522. return ($result);
  1523. }
  1524. /**
  1525. * Listens API requests and renders replies for it
  1526. *
  1527. * @return void
  1528. */
  1529. public function catchRequest() {
  1530. if (wf_CheckGet(array('request'))) {
  1531. $request = $_GET['request'];
  1532. $customerId = (wf_CheckGet(array('customer_id'))) ? mysql_real_escape_string($_GET['customer_id']) : '';
  1533. if (isset($this->supportedMethods[$request])) {
  1534. switch ($request) {
  1535. case 'get_tariff_list':
  1536. $this->renderReply($this->getTariffsData());
  1537. break;
  1538. case 'get_city_list':
  1539. $this->renderReply($this->getCitiesData());
  1540. break;
  1541. case 'get_street_list':
  1542. $this->renderReply($this->getStreetsData());
  1543. break;
  1544. case 'get_house_list':
  1545. $this->renderReply($this->getBuildsData());
  1546. break;
  1547. case 'get_user_additional_data_type_list':
  1548. $this->renderReply($this->getCFTypesData());
  1549. break;
  1550. case 'get_user_state_list':
  1551. $this->renderReply($this->getUsersStateList());
  1552. break;
  1553. case 'get_supported_method_list':
  1554. $this->renderReply($this->getMethodsList());
  1555. break;
  1556. case 'get_api_information':
  1557. $this->renderReply($this->getApiInformation());
  1558. break;
  1559. case 'get_user_group_list':
  1560. $this->renderReply($this->getTagTypesList());
  1561. break;
  1562. case 'get_system_information':
  1563. $this->renderReply($this->getSystemInformation());
  1564. break;
  1565. case 'get_user_list':
  1566. if (empty($customerId)) {
  1567. $this->renderReply($this->getUsersList());
  1568. } else {
  1569. $this->renderReply($this->getUsersList($customerId));
  1570. }
  1571. break;
  1572. case 'get_user_messages':
  1573. $this->renderReply($this->getUsersMessages());
  1574. break;
  1575. case 'get_user_history':
  1576. if (!empty($customerId)) {
  1577. $this->renderReply($this->getUserFinanceHistory($customerId));
  1578. } else {
  1579. $this->renderReply(array('result' => 'error', 'error' => $this->errorNotices['EX_PARAM_MISSED'] . ': customer_id'));
  1580. }
  1581. break;
  1582. case 'get_user_tags':
  1583. $this->renderReply($this->getUserTags());
  1584. break;
  1585. case 'get_services_list':
  1586. $this->renderReply($this->getServicesList());
  1587. break;
  1588. case 'get_supported_change_user_data_list':
  1589. $this->renderReply($this->getChangeMethodsList());
  1590. break;
  1591. case 'get_supported_change_user_state':
  1592. $this->renderReply($this->getChangeStateMethodsList());
  1593. break;
  1594. case 'get_supported_change_user_tariff':
  1595. $this->renderReply($this->getTariffsData(true));
  1596. break;
  1597. case 'get_device_type':
  1598. $this->renderReply($this->getDeviceTypesList());
  1599. break;
  1600. case 'get_device_model':
  1601. $this->renderReply($this->getDeviceModels());
  1602. break;
  1603. case 'get_device_list':
  1604. $devTypeFilters = (wf_CheckGet(array('device_type'))) ? $_GET['device_type'] : '';
  1605. $this->renderReply($this->getDevicesList($devTypeFilters));
  1606. break;
  1607. case 'get_connect_list':
  1608. $this->renderReply($this->getDeviceConnectionsList());
  1609. break;
  1610. case 'change_user_data':
  1611. $changeParams = $this->catchChangeParams();
  1612. if (!empty($changeParams)) {
  1613. if (isset($changeParams['type'])) {
  1614. $changeOperationType = $changeParams['type'];
  1615. if (isset($this->supportedChangeMethods[$changeOperationType])) {
  1616. switch ($changeOperationType) {
  1617. case 'balance_operation':
  1618. $this->renderReply($this->changeUserFinance($changeParams));
  1619. break;
  1620. case 'balance_correct_operation':
  1621. $this->renderReply($this->changeUserFinanceCorrect($changeParams));
  1622. break;
  1623. case 'name':
  1624. $this->renderReply($this->changeUserRealName($changeParams));
  1625. break;
  1626. case 'comment':
  1627. $this->renderReply($this->changeUserNotes($changeParams));
  1628. break;
  1629. case 'tariff':
  1630. $this->renderReply($this->changeUserTariff($changeParams));
  1631. break;
  1632. case 'state':
  1633. $this->renderReply($this->changeUserState($changeParams));
  1634. break;
  1635. }
  1636. } else {
  1637. $this->renderReply(array('result' => 'error', 'error' => $this->errorNotices['EX_METHOD_NOT_SUPPORTED'] . ': ' . $changeOperationType));
  1638. }
  1639. } else {
  1640. $this->renderReply(array('result' => 'error', 'error' => $this->errorNotices['EX_PARAM_MISSED'] . ': type'));
  1641. }
  1642. } else {
  1643. $this->renderReply(array('result' => 'error', 'error' => $this->errorNotices['EX_NO_PARAMS']));
  1644. }
  1645. }
  1646. } else {
  1647. header('HTTP/1.1 400 Unknown Action"', true, 400);
  1648. die('Unknown Action');
  1649. }
  1650. } else {
  1651. header('HTTP/1.1 400 Undefined request', true, 400);
  1652. die('Undefined request');
  1653. }
  1654. }
  1655. }
  1656. ?>