api.tsupport.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. /**
  3. * Looks like forgotten external applications interraction interface
  4. */
  5. class TSupportApi {
  6. /**
  7. * Stores system alter config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * Contains all of available tariffs data as tariffname=>data
  14. *
  15. * @var array
  16. */
  17. protected $allTariffs = array();
  18. /**
  19. * Contains all of available tariff speeds as tariffname=>data (speeddown/speedup keys)
  20. *
  21. * @var array
  22. */
  23. protected $allTariffSpeeds = array();
  24. /**
  25. * Contains all tariffs periods as tariffname=>period (month/day)
  26. *
  27. * @var array
  28. */
  29. protected $allTariffPeriods = array();
  30. /**
  31. * Contains available cities as cityid=>data
  32. *
  33. * @var array
  34. */
  35. protected $allCities = array();
  36. /**
  37. * Contains available streets array as streetid=>data
  38. *
  39. * @var array
  40. */
  41. protected $allStreets = array();
  42. /**
  43. * Contains all available builds array as buildid=>builddata
  44. *
  45. * @var array
  46. */
  47. protected $allBuilds = array();
  48. /**
  49. * Contains available custom fields types as id=>name
  50. *
  51. * @var array
  52. */
  53. protected $allCfTypes = array();
  54. /**
  55. * Contains available custom fields data as login+cftypeid=>data
  56. *
  57. * @var array
  58. */
  59. protected $allCfData = array();
  60. /**
  61. * Contains data of all available Internet users as login=>data
  62. *
  63. * @var array
  64. */
  65. protected $allUserData = array();
  66. /**
  67. * Contains available tag types as id=>name
  68. *
  69. * @var array
  70. */
  71. protected $allTagTypes = array();
  72. /**
  73. * Contains supported methods list
  74. *
  75. * @var array
  76. */
  77. protected $supportedMethods = array();
  78. /**
  79. * Default streets type. May be configurable in future
  80. *
  81. * @var string
  82. */
  83. protected $defaultStreetType = '';
  84. /**
  85. * Default city type. May be configurable in future
  86. *
  87. * @var string
  88. */
  89. protected $defaultCityType = '';
  90. protected $allRealNames = array();
  91. protected $userInfo = array();
  92. protected $debugMode = false;
  93. public function __construct() {
  94. $this->setOptions();
  95. $this->loadAlter();
  96. }
  97. /**
  98. * Loads system alter config into private property for further usage
  99. *
  100. * @global object $ubillingConfig
  101. *
  102. * @return void
  103. */
  104. protected function loadAlter() {
  105. global $ubillingConfig;
  106. $this->altCfg = $ubillingConfig->getAlter();
  107. }
  108. /**
  109. * Sets object default properties
  110. *
  111. * @return void
  112. */
  113. protected function setOptions() {
  114. $this->defaultStreetType = __('st.');
  115. $this->defaultCityType = __('ct.');
  116. $this->supportedMethods = array(
  117. 'get_supported_method_list' => __('Returns supported methods list'),
  118. 'get_api_information' => __('Returns UserSide API version'),
  119. 'get_tariff_list' => __('Returns available tariffs'),
  120. 'get_city_list' => __('Returns available cities data'),
  121. 'get_street_list' => __('Returns available streets data'),
  122. 'get_house_list' => __('Returns available builds data'),
  123. 'get_user_additional_data_type_list' => __('Returns user profile custom fields data'),
  124. 'get_user_state_list' => __('Returns users state data'),
  125. 'get_user_group_list' => __('Returns user tags list'),
  126. 'get_system_information' => __('Returns system information'),
  127. 'get_user_list' => __('Returns available users data'),
  128. 'get_realnames' => '',
  129. 'get_user_info' => ''
  130. );
  131. }
  132. protected function loadRealNames($like) {
  133. $like_esc = mysql_real_escape_string($like);
  134. $query = 'SELECT * FROM `realname` WHERE `realname` LIKE "%' . $like_esc . '%"';
  135. $all = simple_queryall($query);
  136. if (!empty($all)) {
  137. $i = 0;
  138. foreach ($all as $each) {
  139. $this->allRealNames[] = $each['login'] . '|' . $each['realname'];
  140. }
  141. }
  142. }
  143. protected function loadUserInfo($login) {
  144. $login_esc = mysql_real_escape_string($login);
  145. $users_qry = 'SELECT * FROM `users` WHERE `login` = "' . $login_esc . '"';
  146. $users = simple_query($users_qry);
  147. if (!empty($users)) {
  148. $names_qry = 'SELECT * FROM `realname` WHERE `login` ="' . $login_esc . '"';
  149. $tariff_speed_qry = 'SELECT * FROM `speeds` WHERE `tariff` = "' . $users['Tariff'] . '"';
  150. $tariffs_qry = 'SELECT * FROM `tariffs` WHERE `name`="' . $users['Tariff'] . '"';
  151. $tariffs = simple_query($tariffs_qry);
  152. $names = simple_query($names_qry);
  153. $tariffSpeed = simple_query($tariff_speed_qry);
  154. $this->userInfo['Cash'] = $users['Cash'];
  155. if ($users['Down']) {
  156. $this->userInfo['Down'] = __('Yes');
  157. } else {
  158. $this->userInfo['Down'] = __('No');
  159. }
  160. if ($users['AlwaysOnline']) {
  161. $this->userInfo['AlwaysOnline'] = __('Yes');
  162. } else {
  163. $this->userInfo['AlwaysOnline'] = __('No');
  164. }
  165. $this->userInfo['IP'] = $users['IP'];
  166. if (!empty($names['realname'])) {
  167. $this->userInfo['Name'] = $names['realname'];
  168. }
  169. if ($users['Passive']) {
  170. $this->userInfo['Passive'] = __('Yes');
  171. } else {
  172. $this->userInfo['Passive'] = __('No');
  173. }
  174. $this->userInfo['Tariff'] = $users['Tariff'];
  175. $this->userInfo['Credit'] = $users['Credit'];
  176. $this->userInfo['TariffCost'] = $tariffs['Fee'];
  177. $this->userInfo['TariffPeriod'] = __($tariffs['period']);
  178. $this->userInfo['SpeedDown'] = $tariffSpeed['speeddown'] . 'Kbit/s';
  179. $this->userInfo['SpeedUp'] = $tariffSpeed['speedup'] . 'Kbit/s';
  180. }
  181. }
  182. /**
  183. * Renders API reply as JSON string
  184. *
  185. * @param array $data
  186. *
  187. * @rerutn void
  188. */
  189. protected function renderReply($data) {
  190. $result = 'undefined';
  191. if (!$this->debugMode) {
  192. header('Content-Type: application/json');
  193. if (!empty($data)) {
  194. $result = json_encode($data);
  195. }
  196. die($result);
  197. } else {
  198. debarr($data);
  199. }
  200. }
  201. /**
  202. * Returns users states data
  203. *
  204. * @return array
  205. */
  206. protected function getUsersStateList() {
  207. $result = array();
  208. $result[5]['id'] = 5;
  209. $result[5]['name'] = __('Active');
  210. $result[5]['functional'] = 'work';
  211. $result[1]['id'] = 1;
  212. $result[1]['name'] = __('Debt');
  213. $result[1]['functional'] = 'nomoney';
  214. $result[2]['id'] = 2;
  215. $result[2]['name'] = __('User passive');
  216. $result[2]['functional'] = 'pause';
  217. $result[3]['id'] = 3;
  218. $result[3]['name'] = __('User down');
  219. $result[3]['functional'] = 'disable';
  220. $result[4]['id'] = 4;
  221. $result[4]['name'] = __('No tariff');
  222. $result[4]['functional'] = 'new';
  223. return ($result);
  224. }
  225. /**
  226. * Returns available methods array
  227. *
  228. * @return array
  229. */
  230. protected function getMethodsList() {
  231. $result = array();
  232. if (!empty($this->supportedMethods)) {
  233. foreach ($this->supportedMethods as $io => $each) {
  234. $result[$io]['comment'] = $each;
  235. }
  236. }
  237. return ($result);
  238. }
  239. /**
  240. * Returns Userside API information
  241. *
  242. * @return array
  243. */
  244. protected function getApiInformation() {
  245. $result = array();
  246. $result['version'] = self::API_VER;
  247. $result['date'] = self::API_DATE;
  248. return ($result);
  249. }
  250. /**
  251. * Listens API requests and renders replies for it
  252. *
  253. * @return void
  254. */
  255. public function catchRequest() {
  256. if (wf_CheckGet(array('request'))) {
  257. $request = $_GET['request'];
  258. if (isset($this->supportedMethods[$request])) {
  259. switch ($request) {
  260. case 'get_user_info':
  261. if (isset($_GET['username'])) {
  262. $this->loadUserInfo($_GET['username']);
  263. $this->renderReply($this->userInfo);
  264. }
  265. break;
  266. case 'get_realnames':
  267. if (isset($_GET['like'])) {
  268. $this->loadRealNames($_GET['like']);
  269. $this->renderReply($this->allRealNames);
  270. }
  271. break;
  272. }
  273. } else {
  274. header('HTTP/1.1 400 Unknown Action"', true, 400);
  275. die('Unknown Action');
  276. }
  277. } else {
  278. header('HTTP/1.1 400 Undefined request', true, 400);
  279. die('Undefined request');
  280. }
  281. }
  282. }
  283. ?>