api.cap.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. /**
  3. * Penalty aka Crime and punishment implementation
  4. */
  5. class CrimeAndPunishment {
  6. protected $altCfg = array();
  7. protected $allUsers = array();
  8. protected $capData = array();
  9. protected $login = '';
  10. protected $logPath = '';
  11. protected $curdate = '';
  12. protected $dayLimit = 0; // via CAP_DAYLIMIT
  13. protected $percentpenalty = false; // via CAP_PENALTY_PERCENT
  14. protected $penalty = 0; // via CAP_PENALTY
  15. protected $payId = 1; // via CAP_PAYID
  16. protected $ignoreFrozen = true; // via CAP_IGNOREFROZEN
  17. public function __construct() {
  18. $this->loadAlter();
  19. $this->setOptions();
  20. $this->loadUsers();
  21. $this->loadCapData();
  22. }
  23. /**
  24. * Loads system alter config into private data prop
  25. *
  26. * @global object $ubillingConfig
  27. *
  28. * @return void
  29. */
  30. protected function loadAlter() {
  31. global $ubillingConfig;
  32. $this->altCfg = $ubillingConfig->getAlter();
  33. }
  34. /**
  35. * Sets default options
  36. *
  37. * @return void
  38. */
  39. protected function setOptions() {
  40. $this->curdate = curdatetime();
  41. $this->dayLimit = vf($this->altCfg['CAP_DAYLIMIT'], 3);
  42. $this->percentpenalty = vf($this->altCfg['CAP_PENALTY_PERCENT'], 3);
  43. $this->penalty = ($this->percentpenalty) ? vf($this->altCfg['CAP_PENALTY_PERCENT'], 3) / 100 : vf($this->altCfg['CAP_PENALTY'], 3);
  44. $this->payId = vf($this->altCfg['CAP_PAYID'], 3);
  45. $this->ignoreFrozen = ($this->altCfg['CAP_IGNOREFROZEN']) ? true : false;
  46. $this->logPath = DATA_PATH . 'documents/crimeandpunishment.log';
  47. }
  48. /**
  49. * Pushes log data if debugging mode is enabled
  50. *
  51. * @param string $data
  52. */
  53. protected function debugLog($data) {
  54. file_put_contents($this->logPath, $this->curdate . ' ' . $data . "\n", FILE_APPEND); //append data to log
  55. }
  56. /**
  57. * Loads all users for processing into private data property
  58. *
  59. * @return void
  60. */
  61. protected function loadUsers() {
  62. if ($this->ignoreFrozen) {
  63. //$query = "SELECT * from `users` WHERE `Passive`='0';";
  64. $query = "SELECT `users`.*, `tariffs`.`fee` from `users` left join `tariffs` on `users`.`Tariff` = `tariffs`.`name` WHERE `Passive`='0';";
  65. } else {
  66. //$query = "SELECT * from `users`";
  67. $query = "SELECT `users`.*, `tariffs`.`fee` from `users` left join `tariffs` on `users`.`Tariff` = `tariffs`.`name`;";
  68. }
  69. $raw = simple_queryall($query);
  70. if (!empty($raw)) {
  71. foreach ($raw as $io => $each) {
  72. $this->allUsers[$each['login']] = $each;
  73. }
  74. }
  75. }
  76. /**
  77. * Loads CAP data with counters from database
  78. *
  79. * @return void
  80. */
  81. protected function loadCapData() {
  82. $query = "SELECT * from `capdata`";
  83. $raw = simple_queryall($query);
  84. if (!empty($raw)) {
  85. foreach ($raw as $io => $each) {
  86. $this->capData[$each['login']] = $each;
  87. }
  88. }
  89. }
  90. /**
  91. * Creates new CAP data entry for newly appeared user
  92. *
  93. * @param string $login
  94. * @param int $days
  95. */
  96. protected function createCap($login, $days) {
  97. $login = mysql_real_escape_string($login);
  98. $days = vf($days, 3);
  99. $query = "INSERT INTO `capdata` (`id`,`login`,`date`,`days`) VALUES"
  100. . "(NULL,'" . $login . "','" . $this->curdate . "','" . $days . "');";
  101. nr_query($query);
  102. $this->debugLog("CAP CREATE (" . $login . ")");
  103. }
  104. /**
  105. * Changes CAP entry days counter
  106. *
  107. * @param string $login
  108. * @param int $days
  109. *
  110. * @return void
  111. */
  112. protected function setCap($login, $days) {
  113. if (isset($this->capData[$login])) {
  114. $days = vf($days, 3);
  115. $login = mysql_real_escape_string($login);
  116. simple_update_field('capdata', 'days', $days, "WHERE `login`='" . $login . "'");
  117. $this->debugLog("CAP UPDATE (" . $login . ") DAYS:" . $days);
  118. }
  119. }
  120. /**
  121. * Performs an punishment
  122. *
  123. * @param string $login
  124. */
  125. protected function punish($login) {
  126. if (isset($this->capData[$login])) {
  127. $userTariff = $this->allUsers[$login]['Tariff'];
  128. $tariffFee = $this->allUsers[$login]['fee'];
  129. //optional power tariff price override?
  130. if ($tariffFee == 0) {
  131. if ($this->altCfg['PT_ENABLED']) {
  132. $pt = new PowerTariffs();
  133. if ($pt->isPowerTariff($userTariff)) {
  134. $tariffFee = $pt->getPowerTariffPrice($userTariff);
  135. }
  136. }
  137. }
  138. $penalty = '-' . ( ($this->percentpenalty) ? $this->penalty * $tariffFee : $this->penalty );
  139. zb_CashAdd($login, $penalty, 'add', $this->payId, 'PENALTY:' . $this->capData[$login]['days']);
  140. $this->debugLog("CAP PENALTY (" . $login . ") DAYS:" . $this->capData[$login]['days'] . " PENALTY:" . $penalty);
  141. }
  142. }
  143. /**
  144. * Run users processing
  145. *
  146. * @return void
  147. */
  148. public function processing() {
  149. if (!empty($this->allUsers)) {
  150. foreach ($this->allUsers as $login => $each) {
  151. //is user an debtor?
  152. if ($each['Cash'] < '-' . $each['Credit']) {
  153. //normal debtors processing
  154. if (isset($this->capData[$login])) {
  155. //just counter increment
  156. if ($this->capData[$login]['days'] != $this->dayLimit) {
  157. $this->setCap($login, $this->capData[$login]['days'] + 1);
  158. } else {
  159. //doing punishment
  160. $this->punish($login, $this->capData[$login]['days'] + 1);
  161. $this->setCap($login, $this->capData[$login]['days'] + 1);
  162. }
  163. } else {
  164. //newly down user
  165. $this->createCap($login, 1);
  166. }
  167. } else {
  168. //again not debtor - dropping down counter
  169. if (isset($this->capData[$login])) {
  170. //trying to save SQL query count
  171. if ($this->capData[$login]['days'] > 0) {
  172. $this->setCap($login, 0);
  173. $this->debugLog("CAP RESURRECTED (" . $login . ") DAYS:" . $this->capData[$login]['days']);
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }
  180. /**
  181. * Sets filtering login private property
  182. *
  183. * @param string $login
  184. *
  185. * @return void
  186. */
  187. public function setLogin($login) {
  188. $this->login = $login;
  189. }
  190. /**
  191. * Parses log data for some user login
  192. *
  193. * @return array
  194. */
  195. protected function getLogData() {
  196. $result = array();
  197. global $ubillingConfig;
  198. $billCfg = $ubillingConfig->getBilling();
  199. $cat = $billCfg['CAT'];
  200. $grep = $billCfg['GREP'];
  201. $i = 0;
  202. if (!empty($this->login)) {
  203. if (file_exists($this->logPath)) {
  204. $command = $cat . ' ' . $this->logPath . ' | grep "(' . $this->login . ')"';
  205. $raw = shell_exec($command);
  206. if (!empty($raw)) {
  207. $raw = explodeRows($raw);
  208. if (!empty($raw)) {
  209. foreach ($raw as $io => $each) {
  210. if (!empty($each)) {
  211. $line = explode(' ', $each);
  212. $date = $line[0] . ' ' . $line[1];
  213. $event = $line[3];
  214. $params = explode(')', $each);
  215. $params = $params[1];
  216. $result[$i]['date'] = $date;
  217. $result[$i]['event'] = $event;
  218. $result[$i]['params'] = $params;
  219. $i++;
  220. }
  221. }
  222. }
  223. }
  224. }
  225. }
  226. return ($result);
  227. }
  228. /**
  229. * Returns CAP data for some login
  230. *
  231. * @param string $login
  232. * @return array
  233. */
  234. protected function getCapData($login) {
  235. $result = array();
  236. if (isset($this->capData[$login])) {
  237. $result = $this->capData[$login];
  238. }
  239. return ($result);
  240. }
  241. /**
  242. * Renders Crime and Punishment report
  243. *
  244. * @return string
  245. */
  246. public function renderReport() {
  247. $result = '';
  248. $currentData = $this->getCapData($this->login);
  249. if (!empty($currentData)) {
  250. $result .= wf_tag('div', false, 'glamour') . __('Inactive days') . ': ' . $currentData['days'] . wf_tag('div', true);
  251. $result .= wf_CleanDiv();
  252. }
  253. $logData = $this->getLogData();
  254. if (!empty($logData)) {
  255. $cells = wf_TableCell(__('Date'));
  256. $cells .= wf_TableCell(__('Event'));
  257. $cells .= wf_TableCell(__('Details'));
  258. $rows = wf_TableRow($cells, 'row1');
  259. foreach ($logData as $io => $each) {
  260. $fc = wf_tag('font', false);
  261. $efc = wf_tag('font', true);
  262. if ($each['event'] == 'CREATE') {
  263. $fc = wf_tag('font', false, '', 'color="#ffac1b"');
  264. }
  265. if ($each['event'] == 'UPDATE') {
  266. $fc = wf_tag('font', false, '', 'color="#6396ff"');
  267. }
  268. if ($each['event'] == 'RESURRECTED') {
  269. $fc = wf_tag('font', false, '', 'color="#1c7700"');
  270. }
  271. if ($each['event'] == 'PENALTY') {
  272. $fc = wf_tag('font', false, '', 'color="#a90000"');
  273. }
  274. $params = $each['params'];
  275. $params = str_replace('DAYS', __('Day'), $params);
  276. $params = str_replace('PENALTY', __('Penalty'), $params);
  277. $cells = wf_TableCell($fc . $each['date'] . $efc);
  278. $cells .= wf_TableCell($fc . $each['event'] . $efc);
  279. $cells .= wf_TableCell($params);
  280. $rows .= wf_TableRow($cells, 'row3');
  281. }
  282. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  283. } else {
  284. $result .= wf_tag('span', false, 'alert_warning') . __('Nothing found') . wf_tag('span', true);
  285. }
  286. return ($result);
  287. }
  288. }
  289. ?>