api.ipchange.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. /**
  3. * User IP changing/management implementation
  4. */
  5. class IpChange {
  6. /**
  7. * Contains current user login
  8. *
  9. * @var string
  10. */
  11. protected $login = '';
  12. /**
  13. * Contains current user IP
  14. *
  15. * @var string
  16. */
  17. protected $currentIp = '';
  18. /**
  19. * Contains current user MAC
  20. *
  21. * @var string
  22. */
  23. protected $currentMac = '';
  24. /**
  25. * Contains system billing.ini config as key=>value
  26. *
  27. * @var array
  28. */
  29. protected $billingCfg = array();
  30. /**
  31. * Contains system alter.ini config as key=>value
  32. *
  33. * @var array
  34. */
  35. protected $altCfg = array();
  36. /**
  37. * Contains available network services as id=>name
  38. *
  39. * @var array
  40. */
  41. protected $allServices = array();
  42. /**
  43. * Flag of IP_CUSTOM_SELECT optional option state
  44. *
  45. * @var bool
  46. */
  47. protected $customFlag = false;
  48. /**
  49. * message helper object placeholder
  50. *
  51. * @var object
  52. */
  53. protected $messages = '';
  54. /**
  55. * is database locking feature enabled
  56. *
  57. * @var bool
  58. */
  59. protected $dbLockEnabled = false;
  60. const URL_ME = '?module=pl_ipchange';
  61. public function __construct() {
  62. $this->loadConfigs();
  63. $this->initMessages();
  64. $this->loadServices();
  65. }
  66. /**
  67. * Loads system configs and inits customFlag property
  68. *
  69. * @global object $ubillingConfig
  70. *
  71. * @return void
  72. */
  73. protected function loadConfigs() {
  74. global $ubillingConfig;
  75. $this->billingCfg = $ubillingConfig->getBilling();
  76. $this->altCfg = $ubillingConfig->getAlter();
  77. if (isset($this->altCfg['IP_CUSTOM_SELECT'])) {
  78. if ($this->altCfg['IP_CUSTOM_SELECT']) {
  79. $this->customFlag = true;
  80. }
  81. }
  82. if (isset($this->altCfg['DB_LOCK_ENABLED'])) {
  83. $this->dbLockEnabled = $this->altCfg['DB_LOCK_ENABLED'];
  84. }
  85. }
  86. /**
  87. * Inits message helper object for further usage
  88. *
  89. * @return void
  90. */
  91. protected function initMessages() {
  92. $this->messages = new UbillingMessageHelper();
  93. }
  94. /**
  95. * Loads available network services
  96. *
  97. * @return void
  98. */
  99. protected function loadServices() {
  100. $rawServices = multinet_get_services();
  101. if (!empty($rawServices)) {
  102. foreach ($rawServices as $io => $each) {
  103. $this->allServices[$each['id']] = $each['desc'];
  104. }
  105. }
  106. }
  107. /**
  108. * Sets current user login
  109. *
  110. * @param string $login
  111. *
  112. * @return void
  113. */
  114. public function setLogin($login) {
  115. $this->login = mysql_real_escape_string($login);
  116. }
  117. /**
  118. * Inits user params as current IP and MAC
  119. *
  120. * @return void
  121. */
  122. public function initUserParams() {
  123. if (!empty($this->login)) {
  124. $this->currentIp = zb_UserGetIP($this->login);
  125. $this->currentMac = zb_MultinetGetMAC($this->currentIp);
  126. }
  127. }
  128. /**
  129. * Renders service and IP selection dialog
  130. *
  131. * @return string
  132. */
  133. public function renderMainForm() {
  134. if ($this->altCfg['BRANCHES_ENABLED']) {
  135. global $branchControl;
  136. $branchControl->loadServices();
  137. }
  138. $result = '';
  139. $servSelector = array();
  140. if (!empty($this->allServices)) {
  141. foreach ($this->allServices as $serviceId => $serviceName) {
  142. if ($this->altCfg['BRANCHES_ENABLED']) {
  143. if ($branchControl->isMyService($serviceId)) {
  144. $servSelector[self::URL_ME . '&ajserviceid=' . $serviceId] = $serviceName;
  145. }
  146. } else {
  147. $servSelector[self::URL_ME . '&ajserviceid=' . $serviceId] = $serviceName;
  148. }
  149. }
  150. //getting firs service ID
  151. reset($this->allServices);
  152. $defaultService = key($this->allServices);
  153. $result = wf_AjaxLoader();
  154. $inputs = wf_AjaxSelectorAC('ajcontainer', $servSelector, __('Select User new service'), '', false);
  155. $inputs .= wf_tag('br') . wf_tag('br');
  156. $inputs .= wf_AjaxContainer('ajcontainer', '', $this->ajIpSelector($defaultService));
  157. $inputs .= wf_delimiter();
  158. $inputs .= wf_Submit(__('Save'));
  159. $result .= wf_Form("", 'POST', $inputs, 'floatpanels');
  160. } else {
  161. $result = $this->messages->getStyledMessage(__('No available services'), 'error');
  162. }
  163. return ($result);
  164. }
  165. /**
  166. * Returns IP selector ajax container content
  167. *
  168. * @param int $serviceId
  169. *
  170. * @return string
  171. */
  172. public function ajIpSelector($serviceId) {
  173. $serviceId = vf($serviceId, 3);
  174. $result = '';
  175. if (isset($this->allServices[$serviceId])) {
  176. $networkId = multinet_get_service_networkid($serviceId);
  177. //default IP selection - first free
  178. if (!$this->customFlag) {
  179. @$nextFreeIp = multinet_get_next_freeip('nethosts', 'ip', $networkId);
  180. if (!empty($nextFreeIp)) {
  181. $result = wf_HiddenInput('ipselector', $nextFreeIp) . ' ' . $nextFreeIp . ' (' . __('first free for this service') . ')';
  182. $result .= wf_HiddenInput('serviceselector', $serviceId);
  183. } else {
  184. $result = __('No free IP available in selected pool. Please fix it in networks and services module.');
  185. }
  186. } else {
  187. //custom IP selection box
  188. $allFreeIpsRaw = multinet_get_all_free_ip('nethosts', 'ip', $networkId);
  189. if (!empty($allFreeIpsRaw)) {
  190. $allFreeIpsSelector = array();
  191. foreach ($allFreeIpsRaw as $io => $each) {
  192. $allFreeIpsSelector[$each] = $each;
  193. }
  194. $result = wf_Selector('ipselector', $allFreeIpsSelector, '', '', true);
  195. $result .= wf_HiddenInput('serviceselector', $serviceId);
  196. } else {
  197. $result = __('No free IP available in selected pool. Please fix it in networks and services module.');
  198. }
  199. }
  200. }
  201. return ($result);
  202. }
  203. /**
  204. * Renders current IP styled notification
  205. *
  206. * @return string
  207. */
  208. public function renderCurrentIp() {
  209. $result = wf_tag('h2', false, 'floatpanels', '') . ' ' . $this->currentIp . wf_tag('h2', true);
  210. $result = $this->messages->getStyledMessage(wf_tag('h2', false, '', '') . __('Current user IP') . ': ' . $this->currentIp . wf_tag('h2', true), 'info');
  211. $result .= wf_CleanDiv();
  212. return ($result);
  213. }
  214. /**
  215. * Renders IP usage stats in existing networks
  216. *
  217. * @return string
  218. */
  219. public function renderFreeIpStats() {
  220. $result = '';
  221. $controls = wf_Link(self::URL_ME . '&username=' . $this->login, wf_img('skins/done_icon.png') . ' ' . __('Services'), false, 'ubButton');
  222. $controls .= wf_Link(self::URL_ME . '&username=' . $this->login . '&allnets=true', wf_img('skins/categories_icon.png') . ' ' . __('All networks'), false, 'ubButton');
  223. $result .= web_FreeIpStats();
  224. $result .= $controls;
  225. return ($result);
  226. }
  227. /**
  228. * Checks have current user IP existing network host
  229. *
  230. * @return bool
  231. */
  232. protected function isNethostExists() {
  233. $result = true;
  234. $query = "SELECT * from `nethosts` WHERE `ip`='" . $this->currentIp . "'";
  235. $data = simple_query($query);
  236. if (empty($data)) {
  237. $result = false;
  238. }
  239. return ($result);
  240. }
  241. /**
  242. * Performs user changing IP subroutine
  243. *
  244. * @global object $billing
  245. * @param int $serviceId
  246. * @param string $newIp
  247. *
  248. * @return void/string
  249. */
  250. public function changeUserIp($serviceId, $newIp) {
  251. //set lock or wait until previous lock will be released
  252. //lock name "ipBind" is shared between userreg and pl_ipchange
  253. if ($this->dbLockEnabled) {
  254. $dbLockQuery = 'SELECT GET_LOCK("ipBind",1) AS result';
  255. $dbLock = false;
  256. while (!$dbLock) {
  257. $dbLockCheck = simple_query($dbLockQuery);
  258. $dbLock = $dbLockCheck['result'];
  259. }
  260. }
  261. global $billing;
  262. $result = '';
  263. $serviceId = vf($serviceId, 3);
  264. $networkId = multinet_get_service_networkid($serviceId);
  265. if (isset($this->allServices[$serviceId])) {
  266. if (zb_ip_unique($newIp)) {
  267. if (!empty($this->currentIp)) {
  268. if ($this->isNethostExists()) {
  269. //force user disconnect
  270. if ($this->billingCfg['RESET_AO']) {
  271. $billing->setao($this->login, 0);
  272. } else {
  273. $billing->setdown($this->login, 1);
  274. }
  275. $billing->setip($this->login, $newIp);
  276. multinet_delete_host($this->currentIp);
  277. multinet_add_host($networkId, $newIp, $this->currentMac);
  278. log_register("CHANGE MultiNetIP (" . $this->login . ") FROM " . $this->currentIp . " ON " . $newIp . "");
  279. multinet_rebuild_all_handlers();
  280. multinet_RestartDhcp();
  281. zb_UserGetAllDataCacheClean();
  282. if ($this->altCfg['MULTIGEN_ENABLED']) {
  283. if ($this->altCfg['MULTIGEN_POD_ON_IP_CHANGE']) {
  284. $newUserData = zb_UserGetAllData($this->login);
  285. $newUserData = $newUserData[$this->login];
  286. $userData = $newUserData;
  287. $userData['ip'] = $this->currentIp;
  288. $mlg = new MultiGen();
  289. if ($this->altCfg['MULTIGEN_POD_ON_IP_CHANGE'] == 2) {
  290. $mlg->podOnExternalEvent($this->login, $userData, $newUserData);
  291. $mlg->podOnExternalEvent($this->login, $newUserData);
  292. }
  293. if ($this->altCfg['MULTIGEN_POD_ON_IP_CHANGE'] == 1) {
  294. $mlg->podOnExternalEvent($this->login, $newUserData);
  295. }
  296. }
  297. }
  298. //back teh user online
  299. if ($this->billingCfg['RESET_AO']) {
  300. $billing->setao($this->login, 1);
  301. } else {
  302. $billing->setdown($this->login, 0);
  303. }
  304. //optional arp cleanup here
  305. if (@$this->altCfg['IPCHANGE_ARP_CLEANUP']) {
  306. $command = $this->billingCfg['SUDO'] . ' arp -d ' . $this->currentIp;
  307. shell_exec($command);
  308. }
  309. } else {
  310. log_register("CHANGE FAIL MultiNetIP (" . $this->login . ") FROM " . $this->currentIp . " ON " . $newIp . " NO_NETHOST");
  311. $result = __('No existing nethost for current IP');
  312. }
  313. } else {
  314. log_register("CHANGE FAIL MultiNetIP (" . $this->login . ") FROM " . $this->currentIp . " ON " . $newIp . " EMPTY_IP");
  315. $result = __('Something went wrong') . ': ' . __('empty current IP');
  316. }
  317. } else {
  318. log_register("CHANGE FAIL MultiNetIP (" . $this->login . ") FROM " . $this->currentIp . " ON " . $newIp . " IP_DUPLICATE");
  319. $result = __('This IP is already used by another user');
  320. }
  321. } else {
  322. log_register("CHANGE FAIL MultiNetIP (" . $this->login . ") FROM " . $this->currentIp . " ON " . $newIp . " NO_SERVICE");
  323. $result = __('Unexistent service');
  324. }
  325. //release lock
  326. if ($this->dbLockEnabled) {
  327. $dbUnlockQuery = 'SELECT RELEASE_LOCK("ipBind")';
  328. nr_query($dbUnlockQuery);
  329. }
  330. return ($result);
  331. }
  332. }
  333. ?>