api.smartup.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * Basic SmartUP interconnection class
  4. */
  5. class SmartUP {
  6. /**
  7. * Contains system alter.ini config as key=>value
  8. *
  9. * @var arrays
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * Contains all available users data as login=>data
  14. *
  15. * @var array
  16. */
  17. protected $allUserData = array();
  18. /**
  19. * Contains all available paymentIDs as login=>paymentID
  20. *
  21. * @var array
  22. */
  23. protected $allPaymenIds = array();
  24. /**
  25. * Using of cached data flag.
  26. *
  27. * @var bool
  28. */
  29. protected $useCaching = true;
  30. /**
  31. * System caching abstraction layer placeholder
  32. *
  33. * @var object
  34. */
  35. protected $cache = '';
  36. /**
  37. * Default caching timeout. May be configurable in future.
  38. *
  39. * @var int
  40. */
  41. protected $cacheTimeout = 86400;
  42. /**
  43. * Storage keys etc.
  44. */
  45. const PAYID_KEY = 'SMARTUP_PAYIDS';
  46. const USERDATA_KEY = 'SMARTUP_USERDATA';
  47. /**
  48. * Creates some magic instance
  49. */
  50. public function __construct() {
  51. $this->loadConfig();
  52. $this->initCache();
  53. $this->loadUserData();
  54. $this->loadPaymenIds();
  55. }
  56. /**
  57. * Loads required configs and sets some options
  58. *
  59. * @global object $ubillingConfig
  60. *
  61. * @return void
  62. */
  63. protected function loadConfig() {
  64. global $ubillingConfig;
  65. $this->altCfg = $ubillingConfig->getAlter();
  66. $this->useCaching = (@$this->altCfg['SMARTUP_NOCACHE']) ? false : true;
  67. }
  68. /**
  69. * Loads all required by SmartUp users data from database
  70. *
  71. * @return array
  72. */
  73. protected function getAllUserData() {
  74. $result = array();
  75. $query = "SELECT `users`.`login`,`IP`, `realname`.`realname`, `Tariff`,`Cash` FROM `users` LEFT JOIN `realname` ON (`users`.`login`=`realname`.`login`)";
  76. $all = simple_queryall($query);
  77. if (!empty($all)) {
  78. foreach ($all as $io => $each) {
  79. $result[$each['login']] = $each;
  80. }
  81. }
  82. return($result);
  83. }
  84. /**
  85. * Loads all avilable users data from database into protected prop for further usage
  86. *
  87. * @return void
  88. */
  89. protected function loadUserData() {
  90. if ($this->useCaching) {
  91. $this->allUserData = $this->cache->get(self::USERDATA_KEY, $this->cacheTimeout);
  92. if (empty($this->allUserData)) {
  93. //cache update required
  94. $this->allUserData = $this->getAllUserData();
  95. $this->cache->set(self::USERDATA_KEY, $this->allUserData, $this->cacheTimeout);
  96. }
  97. } else {
  98. $this->allUserData = $this->getAllUserData();
  99. }
  100. }
  101. /**
  102. * Inits system caching object instance
  103. *
  104. * @return void
  105. */
  106. protected function initCache() {
  107. $this->cache = new UbillingCache();
  108. }
  109. /**
  110. * Loads payment IDs from database for further usage
  111. *
  112. * @return void
  113. */
  114. protected function loadPaymenIds() {
  115. $cachedPaymentIds = $this->cache->get(self::PAYID_KEY, $this->cacheTimeout);
  116. if (empty($cachedPaymentIds)) {
  117. $opCustomers = new NyanORM('op_customers');
  118. $payIdsTmp = $opCustomers->getAll();
  119. if (!empty($payIdsTmp)) {
  120. foreach ($payIdsTmp as $io => $each) {
  121. $this->allPaymenIds[$each['realid']] = $each['virtualid'];
  122. }
  123. }
  124. //store updated data to cache
  125. $this->cache->set(self::PAYID_KEY, $this->allPaymenIds, $this->cacheTimeout);
  126. } else {
  127. $this->allPaymenIds = $cachedPaymentIds;
  128. }
  129. }
  130. /**
  131. * Returns some login by assigned IP
  132. *
  133. * @param string $ip
  134. *
  135. * @return string
  136. */
  137. protected function getUserByIp($ip) {
  138. $result = '';
  139. if (!empty($ip)) {
  140. if (!empty($this->allUserData)) {
  141. foreach ($this->allUserData as $io => $each) {
  142. if ($each['IP'] == $ip) {
  143. $result .= $each['login'];
  144. break;
  145. }
  146. }
  147. }
  148. }
  149. return($result);
  150. }
  151. /**
  152. * Returns reply for user and tariff existense
  153. *
  154. * @param string $ip
  155. *
  156. * @return array
  157. */
  158. public function getAuthByIP($ip) {
  159. $result = array();
  160. $userLogin = $this->getUserByIp($ip);
  161. if (!empty($userLogin)) {
  162. $result = array(
  163. 'login' => $userLogin,
  164. 'tp' => $this->allUserData[$userLogin]['Tariff']
  165. );
  166. } else {
  167. //no user with such IP assigned
  168. $result = array(
  169. 'error' => 'user not exists'
  170. );
  171. }
  172. return($result);
  173. }
  174. /**
  175. * Returns some data by user login
  176. *
  177. * @param string $login
  178. *
  179. * @return array
  180. */
  181. public function getUserInfo($login) {
  182. $result = array();
  183. if (isset($this->allUserData[$login])) {
  184. $userData = $this->allUserData[$login]; //fuck memory economy lol :P
  185. $result = array(
  186. 'fio' => $userData['realname'],
  187. 'balance' => $userData['Cash'],
  188. 'tariff' => $userData['Tariff'],
  189. 'account' => @$this->allPaymenIds[$login]
  190. );
  191. } else {
  192. $result = array(
  193. 'error' => __('User not exists')
  194. );
  195. }
  196. return($result);
  197. }
  198. /**
  199. * Renders data array as JSON encoded string
  200. *
  201. * @param array $data
  202. *
  203. * @return void
  204. */
  205. public function renderReply($data) {
  206. $json = json_encode($data);
  207. die($json);
  208. }
  209. }