api.realipcontrol.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * Inactive users with white IP report
  4. */
  5. class RealIPControl {
  6. /**
  7. * Contains mask of gray IP networks
  8. *
  9. * @var string
  10. */
  11. protected $grayIpMask = 'RFC';
  12. /**
  13. * Months debt count to free unused IP addresses
  14. *
  15. * @var int
  16. */
  17. protected $debtLimit = 3;
  18. /**
  19. * Contains all available users userdata
  20. *
  21. * @var array
  22. */
  23. protected $allUserData = array();
  24. /**
  25. * Contains all available tariff prices
  26. *
  27. * @var array
  28. */
  29. protected $allTrariffPrices = array();
  30. /**
  31. * Local message helper object placeholder
  32. *
  33. * @var object
  34. */
  35. protected $messages = '';
  36. /**
  37. * Some predefined keys for further usage
  38. */
  39. const OPT_GRAYIP = 'RIC_GRAYMASK';
  40. const OPT_DEBTLIM = 'RIC_DEBTLIMIT';
  41. const PROUTE_GRAYMASK = 'newgrayipmask';
  42. const PROUTE_DEBTLIM = 'newdebtlimit';
  43. const URL_ME = '?module=realipcontrol';
  44. public function __construct() {
  45. $this->initMessages();
  46. $this->loadSettings();
  47. $this->loadUserData();
  48. $this->loadTariffPrices();
  49. }
  50. /**
  51. * Inits message helper object instance
  52. *
  53. * @return void
  54. */
  55. protected function initMessages() {
  56. $this->messages = new UbillingMessageHelper();
  57. }
  58. /**
  59. * Loads all available users data
  60. *
  61. * @return void
  62. */
  63. protected function loadUserData() {
  64. $this->allUserData = zb_UserGetAllStargazerData();
  65. }
  66. protected function loadTariffPrices() {
  67. $this->allTrariffPrices = zb_TariffGetPricesAll();
  68. if (!empty($this->allTrariffPrices)) {
  69. $tariffPeriods = zb_TariffGetPeriodsAll();
  70. foreach ($this->allTrariffPrices as $tariffName => $tariffFee) {
  71. if (isset($tariffPeriods[$tariffName])) {
  72. $tariffPeriod = $tariffPeriods[$tariffName];
  73. if ($tariffPeriod == 'day') {
  74. // I'm too lazy to think, so we'll have like 30 days in a month.
  75. // And I don't care if you think otherwise.
  76. $tariffMontlyFee = $tariffFee * 30;
  77. // Yeah.. now we just updating tariff fees property with approximate monthly fee
  78. $this->allTrariffPrices[$tariffName] = $tariffMontlyFee;
  79. }
  80. }
  81. }
  82. }
  83. }
  84. /**
  85. * Loads some settings from database or sets some default values
  86. *
  87. * @return void
  88. */
  89. protected function loadSettings() {
  90. $optionGrayIpMask = zb_StorageGet(self::OPT_GRAYIP);
  91. if (empty($optionGrayIpMask)) {
  92. //initial settings on 1st usage
  93. zb_StorageSet(self::OPT_GRAYIP, $this->grayIpMask);
  94. } else {
  95. $this->grayIpMask = $optionGrayIpMask;
  96. }
  97. $optionFeeLimit = zb_StorageGet(self::OPT_DEBTLIM);
  98. if (empty($optionFeeLimit)) {
  99. zb_StorageSet(self::OPT_DEBTLIM, $this->debtLimit);
  100. } else {
  101. $this->debtLimit = $optionFeeLimit;
  102. }
  103. }
  104. /**
  105. * Renders module configuration interface
  106. *
  107. * @return string
  108. */
  109. public function renderConfigForm() {
  110. $result = '';
  111. $helpLabel = __('For example') . ': 192.168.0.,172.16.,10.14.88. ' . __('(separator - comma)') . '. ';
  112. $helpLabel .= __('Or') . ' RFC ' . __('for all');
  113. $helpLabel = wf_tag('abbr', false, '', 'title="' . $helpLabel . '"') . wf_img('skins/question.png') . wf_tag('abbr', true);
  114. $inputs = $helpLabel . ' ';
  115. $inputs .= wf_TextInput(self::PROUTE_GRAYMASK, __('Mask for non real IP in your network'), $this->grayIpMask, false, 20) . ' ';
  116. $inputs .= wf_TextInput(self::PROUTE_DEBTLIM, __('Month limit to withdraw real IP'), $this->debtLimit, false, 3);
  117. $inputs .= wf_Submit(__('Save'));
  118. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  119. return($result);
  120. }
  121. /**
  122. * Updates module configuration in database if required
  123. *
  124. * @return void
  125. */
  126. public function saveSettings() {
  127. if (ubRouting::checkPost(array(self::PROUTE_GRAYMASK, self::PROUTE_DEBTLIM))) {
  128. $newMask = ubRouting::post(self::PROUTE_GRAYMASK, 'mres');
  129. if ($newMask != $this->grayIpMask) {
  130. zb_StorageSet(self::OPT_GRAYIP, $newMask);
  131. }
  132. $newDebtLimit = ubRouting::post(self::PROUTE_DEBTLIM, 'int');
  133. if ($newDebtLimit != $this->debtLimit) {
  134. zb_StorageSet(self::OPT_DEBTLIM, $newDebtLimit);
  135. }
  136. }
  137. }
  138. /**
  139. * Checks is IP private or public
  140. *
  141. * @param string $ip
  142. *
  143. * @return int 0/1
  144. */
  145. public function isPrivateIp($ip) {
  146. $pattern = '$(10(\.(25[0-5]|2[0-4][0-9]|1[0-9]{1,2}|[0-9]{1,2})){3}|((172\.(1[6-9]|2[0-9]|3[01]))|192\.168)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{1,2}|[0-9]{1,2})){2})$';
  147. $result = preg_match($pattern, $ip);
  148. return($result);
  149. }
  150. /**
  151. * Renders report of users which need withdraw real IP
  152. *
  153. * @return void
  154. */
  155. public function renderReport() {
  156. $result = '';
  157. $tmpArr = array();
  158. $tmpMbAlive = array();
  159. if (!empty($this->allUserData)) {
  160. foreach ($this->allUserData as $io => $each) {
  161. if (!empty($each['IP'])) {
  162. $userLogin = $each['login'];
  163. $userTariff = $each['Tariff'];
  164. //Real IP?
  165. if ($this->grayIpMask == 'RFC') {
  166. $realIpFlag = ($this->isPrivateIp($each['IP'])) ? false : true;
  167. } else {
  168. $realIpFlag = true;
  169. $wildcardTmp = explode(',', $this->grayIpMask);
  170. if (!empty($wildcardTmp)) {
  171. foreach ($wildcardTmp as $maskIndex => $eachGrayIpMask) {
  172. $eachGrayIpMask = trim($eachGrayIpMask);
  173. if (!empty($eachGrayIpMask)) {
  174. if (ispos($each['IP'], $eachGrayIpMask)) {
  175. $realIpFlag = false;
  176. }
  177. }
  178. }
  179. }
  180. }
  181. if ($realIpFlag) {
  182. //Tariff exists
  183. if (isset($this->allTrariffPrices[$userTariff])) {
  184. $tariffPrice = $this->allTrariffPrices[$userTariff];
  185. //Tariff isnt free
  186. if ($tariffPrice > 0) {
  187. $maxDebt = '-' . ($this->debtLimit * $tariffPrice);
  188. $curMoneyLimit = $each['Cash'] + $each['Credit'];
  189. if ($curMoneyLimit <= $maxDebt) {
  190. if ($each['U0'] == 0) {
  191. $tmpArr[$userLogin] = $userLogin;
  192. } else {
  193. $tmpMbAlive[$userLogin] = $userLogin;
  194. }
  195. }
  196. }
  197. }
  198. }
  199. }
  200. }
  201. show_window(__('May be withdraw some IPs from this users'), web_UserArrayShower($tmpArr));
  202. show_window(__('This users is debtors but seems it alive'), web_UserArrayShower($tmpMbAlive));
  203. } else {
  204. show_window('', $this->messages->getStyledMessage(__('Nothing to show'), 'warning'));
  205. }
  206. return($result);
  207. }
  208. }