api.ophanimflow.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /**
  3. * OphanimFlow integration implementation
  4. */
  5. class OphanimFlow {
  6. /**
  7. * Contains alter config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * Contains current year
  14. *
  15. * @var int
  16. */
  17. protected $currentYear = 0;
  18. /**
  19. * Contains current month number without leading zero
  20. *
  21. * @var int
  22. */
  23. protected $currentMonth = 0;
  24. /**
  25. * Traff stats database abstraction layer
  26. *
  27. * @var object
  28. */
  29. protected $traffDb = '';
  30. /**
  31. * Contains previously saved traffic stats as login=>id/login/month/year/d0/u0
  32. *
  33. * @var array
  34. */
  35. protected $traffStats = array();
  36. /**
  37. * Contains current run traff
  38. *
  39. * @var array
  40. */
  41. protected $currentTraff = array();
  42. /**
  43. * Contains array of all existing users IPs as IP=>login
  44. *
  45. * @var array
  46. */
  47. protected $allUserIps = array();
  48. /**
  49. * Contains OphanimFlow URLs to pull traffic data as idx=>url
  50. *
  51. * @var array
  52. */
  53. protected $ophanimUrls = array();
  54. //some predefined stuff here
  55. const TABLE_TRAFFDATA = 'ophtraff';
  56. const API_ENDPOINT = '/?module=gettraff';
  57. const OPTION_ENABLED = 'OPHANIMFLOW_ENABLED';
  58. const OPTION_URLS = 'OPHANIMFLOW_URLS';
  59. const OPTION_DIMENSIONS='OPHANIM_DIMENSIONS';
  60. const PID_SYNC='OPHANIMTRAFF';
  61. public function __construct() {
  62. // BE NOT AFRAID
  63. $this->setDates();
  64. $this->initDb();
  65. $this->loadConfigs();
  66. }
  67. /**
  68. * Sets current instance dates info
  69. *
  70. * @return void
  71. */
  72. protected function setDates() {
  73. $this->currentYear = date("Y");
  74. $this->currentMonth = date("n");
  75. }
  76. /**
  77. * Inits database abstraction layer
  78. *
  79. * @return void
  80. */
  81. protected function initDb() {
  82. $this->traffDb = new NyanORM(self::TABLE_TRAFFDATA);
  83. }
  84. /**
  85. * Load requred configs, sets some properties
  86. *
  87. * @global object $ubillingConfig
  88. *
  89. * @return void
  90. */
  91. protected function loadConfigs() {
  92. global $ubillingConfig;
  93. $this->altCfg = $ubillingConfig->getAlter();
  94. if (isset($this->altCfg[self::OPTION_URLS])) {
  95. if (!empty($this->altCfg[self::OPTION_URLS])) {
  96. $urlsRaw = explode(',', $this->altCfg[self::OPTION_URLS]);
  97. if (!empty($urlsRaw)) {
  98. foreach ($urlsRaw as $io => $eachUrl) {
  99. $this->ophanimUrls[] = $eachUrl;
  100. }
  101. }
  102. }
  103. }
  104. }
  105. /**
  106. * Loads all reqired user data
  107. *
  108. * @return void
  109. */
  110. protected function loadUserData() {
  111. $loginIps = zb_UserGetAllIPs();
  112. if (!empty($loginIps)) {
  113. $this->allUserIps = array_flip($loginIps);
  114. }
  115. }
  116. /**
  117. * Loads saved traff stats
  118. *
  119. * @return void
  120. */
  121. protected function loadTraffStats() {
  122. $this->traffDb->where('year', '=', $this->currentYear);
  123. $this->traffDb->where('month', '=', $this->currentMonth);
  124. $this->traffStats = $this->traffDb->getAll('login');
  125. }
  126. /**
  127. * Preprocess fetched data and update local database records
  128. *
  129. * @param array $rawData
  130. *
  131. * @return void
  132. */
  133. protected function processOphanimTraff($rawData) {
  134. if (!empty($rawData)) {
  135. foreach ($rawData as $eachIp => $eachTraffData) {
  136. if (isset($this->allUserIps[$eachIp])) {
  137. $userLogin = $this->allUserIps[$eachIp];
  138. if (isset($this->traffStats[$userLogin])) {
  139. //existing record
  140. $savedData = $this->traffStats[$userLogin];
  141. $recordId = $savedData['id'];
  142. $savedDl = $savedData['D0'];
  143. $savedUl = $savedData['U0'];
  144. $newDl = $eachTraffData['dl'];
  145. $newUl = $eachTraffData['ul'];
  146. //traffic counters changed?
  147. if ($newDl != $savedDl OR $newUl != $savedUl) {
  148. $this->traffDb->data('D0', $newDl);
  149. $this->traffDb->data('U0', $newUl);
  150. $this->traffDb->where('id', '=', $recordId);
  151. $this->traffDb->save(true, true);
  152. }
  153. } else {
  154. //new record always with 0 at start to prevent previous month traffic shift
  155. $this->traffDb->data('login', $userLogin);
  156. $this->traffDb->data('month', $this->currentMonth);
  157. $this->traffDb->data('year', $this->currentYear);
  158. $this->traffDb->data('D0', 0);
  159. $this->traffDb->data('U0', 0);
  160. $this->traffDb->create();
  161. }
  162. }
  163. }
  164. }
  165. }
  166. /**
  167. * Fetches data from ophanimUrls and updates local db
  168. *
  169. * @return void
  170. */
  171. public function traffDataProcessing() {
  172. if (!empty($this->ophanimUrls)) {
  173. $this->loadTraffStats();
  174. $this->loadUserData();
  175. foreach ($this->ophanimUrls as $io => $eachUrl) {
  176. $apiEndpoint = new OmaeUrl($eachUrl . self::API_ENDPOINT);
  177. $rawData = $apiEndpoint->response();
  178. if (!empty($rawData)) {
  179. @$rawData = json_decode($rawData, true);
  180. if (is_array($rawData)) {
  181. if (!empty($rawData)) {
  182. $this->processOphanimTraff($rawData);
  183. }
  184. }
  185. }
  186. }
  187. }
  188. }
  189. /**
  190. * Returns array of current month traffic for some user as D0/U0 array
  191. *
  192. * @param string $userLogin
  193. *
  194. * @return array
  195. */
  196. public function getUserCurMonthTraff($userLogin) {
  197. $result = array(
  198. 'D0' => 0,
  199. 'U0' => 0
  200. );
  201. $userLogin = ubRouting::filters($userLogin, 'mres');
  202. $this->traffDb->where('login', '=', $userLogin);
  203. $this->traffDb->where('year', '=', $this->currentYear);
  204. $this->traffDb->where('month', '=', $this->currentMonth);
  205. $rawData = $this->traffDb->getAll();
  206. if (!empty($rawData)) {
  207. $result['D0'] = $rawData[0]['D0'];
  208. $result['U0'] = $rawData[0]['U0'];
  209. }
  210. return($result);
  211. }
  212. /**
  213. * Returns all of previous user traff data
  214. *
  215. * @param string $userLogin
  216. *
  217. * @return array
  218. */
  219. public function getUserAllTraff($userLogin) {
  220. $userLogin = ubRouting::filters($userLogin, 'mres');
  221. $result = array();
  222. $this->traffDb->where('login', '=', $userLogin);
  223. $this->traffDb->orderBy('year,month', 'DESC');
  224. $result = $this->traffDb->getAll();
  225. return($result);
  226. }
  227. /**
  228. * Returns all users current month aggregated traffic summ as login=>bytesCount
  229. *
  230. * @return array
  231. */
  232. public function getAllUsersAggrTraff() {
  233. $result = array();
  234. $this->traffDb->selectable(array('login', 'D0', 'U0'));
  235. $this->traffDb->where('year', '=', $this->currentYear);
  236. $this->traffDb->where('month', '=', $this->currentMonth);
  237. $raw = $this->traffDb->getAll();
  238. if (!empty($raw)) {
  239. foreach ($raw as $io => $each) {
  240. $result[$each['login']] = $each['D0'] + $each['U0'];
  241. }
  242. }
  243. return($result);
  244. }
  245. /**
  246. * Життя майне, як хвилина
  247. * Кожна смертна година
  248. * Пручатись варто
  249. * Злому жарту?
  250. */
  251. }