api.badkarma.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. /**
  3. * Basic MySQL user cash storage double precision fixing class
  4. */
  5. class BadKarma {
  6. /**
  7. * Contains default online users detection path. May be customizable in future.
  8. *
  9. * @var string
  10. */
  11. protected $onlineDataPath = '/etc/stargazer/dn/';
  12. /**
  13. * Contains all available users data as login=>userdata
  14. *
  15. * @var array
  16. */
  17. protected $allUsersData = array();
  18. /**
  19. * System message helper placeholder
  20. *
  21. * @var object
  22. */
  23. protected $messages = '';
  24. /**
  25. * Contains all of online users as login=>login
  26. *
  27. * @var array
  28. */
  29. protected $allOnlineUsers = array();
  30. /**
  31. * Contains default lower active user cash limit to detect his karma
  32. *
  33. * @var float
  34. */
  35. protected $lowerCashLimit = -0.02;
  36. /**
  37. * Default timeout between checks is actions really do something or not in sec.
  38. *
  39. * @var int
  40. */
  41. protected $waitTimeout = 1;
  42. /**
  43. * Some predefined routes etc..
  44. */
  45. const URL_PROFILE = '?module=userprofile&username=';
  46. const URL_ME = '?module=badkarma';
  47. const ROUTE_MASSRESET = 'domassreset';
  48. const ROUTE_FIX = 'fixuserkarma';
  49. const COLOR_BAD = '#AB0000';
  50. /**
  51. * Creates new BadKarma instance
  52. *
  53. * @param bool $noDataLoad dont load full data set
  54. *
  55. * @return void
  56. */
  57. public function __construct($noDataLoad = false) {
  58. $this->initMessages();
  59. if (!$noDataLoad) {
  60. $this->loadUserData();
  61. $this->loadOnlineUsers();
  62. }
  63. }
  64. /**
  65. * Inits system messages helper
  66. *
  67. * @return void
  68. */
  69. protected function initMessages() {
  70. $this->messages = new UbillingMessageHelper();
  71. }
  72. /**
  73. * Loads existing userdata from database
  74. *
  75. * @return void
  76. */
  77. protected function loadUserData() {
  78. $this->allUsersData = zb_UserGetAllData();
  79. }
  80. /**
  81. * Loads list of online users
  82. *
  83. * @return void
  84. */
  85. protected function loadOnlineUsers() {
  86. if (file_exists($this->onlineDataPath)) {
  87. $all = rcms_scandir($this->onlineDataPath);
  88. if (!empty($all)) {
  89. foreach ($all as $io => $each) {
  90. $this->allOnlineUsers[$each] = $each;
  91. }
  92. }
  93. }
  94. }
  95. /**
  96. * Checks is user acceptible to be online. Just with normal amount of money, not disabled, frozen, etc..
  97. *
  98. * @param array $userData
  99. *
  100. * @return bool
  101. */
  102. protected function userMustBeOnline($userData) {
  103. $result = false;
  104. //basic online availability flag
  105. if ($userData['AlwaysOnline'] == '1') {
  106. //user isnt disabled
  107. if ($userData['Down'] == '0') {
  108. //user is not frozen
  109. if ($userData['Passive'] == '0') {
  110. //check financial amounts
  111. if ($userData['Cash'] >= $this->lowerCashLimit) {
  112. $result = true; // eezee, yeah?
  113. }
  114. }
  115. }
  116. }
  117. return($result);
  118. }
  119. /**
  120. * Checks is user really online based on On* scripts actions
  121. *
  122. * @param string $userLogin
  123. *
  124. * @return bool
  125. */
  126. protected function userIsOnline($userLogin) {
  127. $result = false;
  128. if (!empty($userLogin)) {
  129. if (isset($this->allOnlineUsers[$userLogin])) {
  130. $result = true;
  131. }
  132. }
  133. return($result);
  134. }
  135. /**
  136. * Checks for user real online appear without refresh internal structs
  137. *
  138. * @param string $userLogin
  139. *
  140. * @return bool
  141. */
  142. public function isUserOnlineRightNow($userLogin) {
  143. $result = false;
  144. if (file_exists($this->onlineDataPath . $userLogin)) {
  145. $result = true;
  146. }
  147. return($result);
  148. }
  149. /**
  150. * Returns some user karma state indicator with some controls
  151. *
  152. * @param string $userLogin
  153. * @param array $userData
  154. * @param int $size
  155. *
  156. * @return string
  157. */
  158. public function getKarmaIndicator($userLogin, $userData, $size = '') {
  159. $result = '';
  160. $iconSize = ($size) ? $size : '16';
  161. if ($this->userMustBeOnline($userData)) {
  162. if ($this->isUserOnlineRightNow($userLogin)) {
  163. $result = wf_img_sized('skins/karmagood.png', '', '', $iconSize) . ' ' . __('Good karma');
  164. } else {
  165. $result = wf_Link($this::URL_ME, wf_img_sized('skins/karmabad.png', __('Fix'), '', $iconSize)) . ' ' . __('Bad karma') . '!';
  166. }
  167. } else {
  168. $result = wf_img_sized('skins/karmafail.png', '', '', $iconSize) . ' ' . __('Normal');
  169. }
  170. return($result);
  171. }
  172. /**
  173. * Trying to repair user online state with all of possible actions
  174. *
  175. * @global object $billing
  176. *
  177. * @param string $userLogin
  178. *
  179. * @return void/string on error
  180. */
  181. public function fixUserKarma($userLogin) {
  182. global $billing;
  183. $result = '';
  184. $userData = (isset($this->allUsersData[$userLogin])) ? $this->allUsersData[$userLogin] : array();
  185. //really existing user?
  186. if (!empty($userData)) {
  187. //is user params acceptible to be online
  188. if ($this->userMustBeOnline($userData)) {
  189. //user isnt online right now.. just last check
  190. if (!$this->isUserOnlineRightNow($userLogin)) {
  191. //trying to just reset user
  192. $billing->resetuser($userLogin);
  193. log_register("RESET User (" . $userLogin . ")");
  194. sleep($this->waitTimeout);
  195. //thats doesnt work?
  196. if (!$this->isUserOnlineRightNow($userLogin)) {
  197. //ok.. tryin to fix his cash value to real zero
  198. if ($userData['Cash'] >= $this->lowerCashLimit) {
  199. if ($userData['Cash'] < 0.01) {
  200. zb_CashAdd($userLogin, 0, 'set', 1, 'KARMAFIX');
  201. sleep($this->waitTimeout);
  202. if (!$this->isUserOnlineRightNow($userLogin)) {
  203. //trying to reset user after cash correction
  204. $billing->resetuser($userLogin);
  205. log_register("RESET User (" . $userLogin . ")");
  206. sleep($this->waitTimeout);
  207. if (!$this->isUserOnlineRightNow($userLogin)) {
  208. //may be credit limit have not real zero value?
  209. if ($userData['Credit'] == 0) {
  210. $billing->setcredit($userLogin, 0);
  211. log_register('CHANGE Credit (' . $userLogin . ') ON 0');
  212. sleep($this->waitTimeout);
  213. //we give up :(
  214. if (!$this->isUserOnlineRightNow($userLogin)) {
  215. $result .= __('We tried all that we can. Nothing helps. This user is doomed.');
  216. log_register('KARMA (' . $userLogin . ') FIX FAIL AT CREDIT THATS ALL');
  217. } else {
  218. log_register('KARMA (' . $userLogin . ') FIXED ON CREDIT TO ZERO');
  219. }
  220. } else {
  221. //and give up again. Nothing to else to do.
  222. $result .= __('We tried all that we can. Nothing helps. This user is doomed.');
  223. log_register('KARMA (' . $userLogin . ') FIX FAIL AT ALL');
  224. }
  225. } else {
  226. log_register('KARMA (' . $userLogin . ') FIXED ON RESET2');
  227. }
  228. } else {
  229. log_register('KARMA (' . $userLogin . ') FIXED ON CASH TO ZERO');
  230. }
  231. } else {
  232. $result .= __('To much money') . ': ' . $userData['Cash'];
  233. log_register('KARMA (' . $userLogin . ') FIX FAIL ON MUCH_CASH');
  234. }
  235. }
  236. } else {
  237. log_register('KARMA (' . $userLogin . ') FIXED ON RESET1');
  238. }
  239. }
  240. }
  241. } else {
  242. $result .= __('User not exists');
  243. }
  244. return($result);
  245. }
  246. /**
  247. * Highlights corrupted zero cash value
  248. *
  249. * @param float $cashValue
  250. *
  251. * @return string
  252. */
  253. protected function highlightCorruptedCash($cashValue) {
  254. if ($cashValue == 0) {
  255. $result = wf_tag('font', false, '', 'color="' . self::COLOR_BAD . '"') . $cashValue . wf_tag('font', true);
  256. } else {
  257. $result = $cashValue;
  258. }
  259. return($result);
  260. }
  261. /**
  262. * Tries to basically fix karma by resseting bad karma users
  263. *
  264. * @global object $billing
  265. *
  266. * @param bool $guard
  267. *
  268. * @return void
  269. */
  270. public function runMassReset($guard = false) {
  271. global $billing;
  272. $tmpArr = array();
  273. $totalCount = 0;
  274. if (!empty($this->allUsersData)) {
  275. if (!empty($this->allOnlineUsers)) {
  276. foreach ($this->allUsersData as $eachUserLogin => $eachUserData) {
  277. //may this user be just... online?
  278. if ($this->userMustBeOnline($eachUserData)) {
  279. //and he is not?
  280. if (!$this->userIsOnline($eachUserLogin)) {
  281. //tryin to reset
  282. $billing->resetuser($eachUserLogin);
  283. $type = 'MASSRESET';
  284. if ($guard) {
  285. $type = 'GUARDRESET';
  286. }
  287. log_register('KARMA ' . $type . ' User (' . $eachUserLogin . ')');
  288. }
  289. }
  290. }
  291. }
  292. }
  293. }
  294. /**
  295. * Renders report of users which possible have an bad karma
  296. *
  297. * @return string
  298. */
  299. public function renderReport() {
  300. $result = '';
  301. $tmpArr = array();
  302. $totalCount = 0;
  303. if (!empty($this->allUsersData)) {
  304. if (!empty($this->allOnlineUsers)) {
  305. foreach ($this->allUsersData as $eachUserLogin => $eachUserData) {
  306. //may this user be just... online?
  307. if ($this->userMustBeOnline($eachUserData)) {
  308. //and he is not?
  309. if (!$this->userIsOnline($eachUserLogin)) {
  310. $tmpArr[$eachUserLogin] = $eachUserData; //yeah, fuck memory economy!
  311. }
  312. }
  313. }
  314. if (!empty($tmpArr)) {
  315. $cells = wf_TableCell(__('Login'));
  316. $cells .= wf_TableCell(__('Address'));
  317. $cells .= wf_TableCell(__('Real Name'));
  318. $cells .= wf_TableCell(__('IP'));
  319. $cells .= wf_TableCell(__('Tariff'));
  320. $cells .= wf_TableCell(__('Balance'));
  321. $cells .= wf_TableCell(__('Credit'));
  322. $cells .= wf_TableCell(__('Actions'));
  323. $rows = wf_TableRow($cells, 'row1');
  324. foreach ($tmpArr as $eachUserLogin => $eachUserData) {
  325. $userLinkControl = wf_Link(self::URL_PROFILE . $eachUserLogin, web_profile_icon() . ' ' . $eachUserLogin);
  326. $alertLabel = $this->messages->getEditAlert() . ' ' . __('Fix') . ' ' . $eachUserLogin . '?';
  327. $repairLinkControl = wf_JSAlert(self::URL_ME . '&' . self::ROUTE_FIX . '=' . $eachUserLogin, wf_img('skins/icon_repair.gif', __('Fix')), $alertLabel);
  328. $cells = wf_TableCell($userLinkControl);
  329. $cells .= wf_TableCell($eachUserData['fulladress']);
  330. $cells .= wf_TableCell($eachUserData['realname']);
  331. $cells .= wf_TableCell($eachUserData['ip']);
  332. $cells .= wf_TableCell($eachUserData['Tariff']);
  333. $cells .= wf_TableCell($this->highlightCorruptedCash($eachUserData['Cash']));
  334. $cells .= wf_TableCell($eachUserData['Credit']);
  335. $cells .= wf_TableCell($repairLinkControl);
  336. $rows .= wf_TableRow($cells, 'row5');
  337. $totalCount++;
  338. }
  339. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  340. $result .= __('Total') . ' ' . $totalCount;
  341. //some controls here
  342. if ($totalCount > 0) {
  343. if (cfr('ROOT')) {
  344. $result .= wf_delimiter();
  345. $result .= wf_Link(self::URL_ME . '&' . self::ROUTE_MASSRESET . '=true', wf_img('skins/refresh.gif') . ' ' . __('Mass reset'), false, 'ubButton');
  346. }
  347. }
  348. } else {
  349. $result .= $this->messages->getStyledMessage(__('Everything is good'), 'success');
  350. }
  351. } else {
  352. $result .= $this->messages->getStyledMessage(__('No online users at all'), 'warning');
  353. }
  354. } else {
  355. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'warning');
  356. }
  357. return($result);
  358. }
  359. }