IP.class.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * IP
  4. *
  5. * @author vesta, http://vestacp.com/
  6. * @author Dmitry Malishev <dima.malishev@gmail.com>
  7. * @author Dmitry Naumov-Socolov <naumov.socolov@gmail.com>
  8. * @copyright vesta 2010-2011
  9. */
  10. class IP extends AjaxHandler
  11. {
  12. /**
  13. * Get IP entries
  14. *
  15. * @param Request $request
  16. * @return string - Ajax Reply
  17. */
  18. public function getListExecute(Request $request)
  19. {
  20. $reply = array();
  21. $result = Vesta::execute(Vesta::V_LIST_SYS_IPS, array(Config::get('response_type')));
  22. foreach ($result['data'] as $ip => $details) {
  23. $reply[] = array_merge(
  24. array(
  25. 'IP_ADDRESS' => $ip,
  26. 'DATE' => date(Config::get('ui_date_format', strtotime($details['DATE'])))
  27. ), $details);
  28. }
  29. if (!$result['status']) {
  30. $this->errors[] = array($result['error_code'] => $result['error_message']);
  31. }
  32. return $this->reply($result['status'], $reply);
  33. }
  34. /**
  35. * Get user's IPs
  36. *
  37. * @param Request $request
  38. * @return string - Ajax Reply
  39. */
  40. public function getListUserIpsExecute(Request $request)
  41. {
  42. $reply = array();
  43. $result = Vesta::execute(Vesta::V_LIST_SYS_IPS, array(Config::get('response_type')));
  44. foreach ($result['data'] as $ip => $details) {
  45. $reply[] = array_merge(
  46. array(
  47. 'IP_ADDRESS' => $ip,
  48. 'DATE' => date(Config::get('ui_date_format', strtotime($details['DATE'])))
  49. ), $details);
  50. }
  51. if (!$result['status']) {
  52. $this->errors[] = array($result['error_code'] => $result['error_message']);
  53. }
  54. return $this->reply($result['status'], $reply);
  55. }
  56. /**
  57. * Add IP entry
  58. *
  59. * @param Request $request
  60. * @return string - Ajax Reply
  61. */
  62. public function addExecute(Request $request)
  63. {
  64. $user = $this->getLoggedUser();
  65. $spell = $request->getParameter('spell');
  66. $params = array(
  67. 'IP_ADDRESS' => $spell['IP_ADDRESS'],
  68. 'MASK' => $spell['NETMASK'],
  69. 'INTERFACE' => $spell['INTERFACE'],
  70. 'OWNER' => $spell['OWNER'],
  71. 'STATUS' => $spell['STATUS']
  72. );
  73. if ($spell['NAME']) {
  74. $params['NAME'] = $spell['NAME'];
  75. }
  76. $result = Vesta::execute(Vesta::V_ADD_SYS_IP, $params);
  77. if (!$result['status']) {
  78. $this->errors[] = array($result['error_code'] => $result['error_message']);
  79. }
  80. return $this->reply($result['status'], $result['data']);
  81. }
  82. /**
  83. * Delete IP entry
  84. *
  85. * @param Request $request
  86. * @return string - Ajax Reply
  87. */
  88. public function deleteExecute(Request $request)
  89. {
  90. $spell = $request->getParameter('spell');
  91. $user = $this->getLoggedUser();
  92. $params = array(
  93. 'IP_ADDRESS' => $spell['IP_ADDRESS']
  94. );
  95. $result = Vesta::execute(Vesta::V_DEL_SYS_IP, $params);
  96. if (!$result['status']) {
  97. $this->errors[] = array($result['error_code'] => $result['error_message']);
  98. }
  99. return $this->reply($result['status'], $result['data']);
  100. }
  101. /**
  102. * Change IP entry
  103. *
  104. * @param Request $request
  105. * @return string - Ajax Reply
  106. */
  107. public function changeExecute(Request $request)
  108. {
  109. $user = $this->getLoggedUser();
  110. $_old = $request->getParameter('old');
  111. $_new = $request->getParameter('new');
  112. $this->status = TRUE;
  113. $this->errors = array();
  114. if ($_old['OWNER'] != $_new['OWNER']) {
  115. $result = array();
  116. $result = Vesta::execute(Vesta::V_CHANGE_SYS_IP_OWNER, array('IP' => $_new['IP_ADDRESS'], 'OWNER' => $_new['OWNER']));
  117. if (!$result['status']) {
  118. $this->status = FALSE;
  119. $this->errors['OWNER'] = array($result['error_code'] => $result['error_message']);
  120. }
  121. }
  122. // TODO: Handle NAME parameter
  123. if ($_old['NAME'] != $_new['NAME']) {
  124. $result = array();
  125. $result = Vesta::execute(Vesta::V_CHANGE_SYS_IP_NAME, array('IP' => $_new['IP_ADDRESS'], 'NAME' => $_new['NAME']));
  126. if (!$result['status']) {
  127. $this->status = FALSE;
  128. $this->errors['NAME'] = array($result['error_code'] => $result['error_message']);
  129. }
  130. }
  131. if ($_old['STATUS'] != $_new['STATUS']) {
  132. $result = array();
  133. $result = Vesta::execute(Vesta::V_CHANGE_SYS_IP_STATUS, array('IP' => $_new['IP_ADDRESS'], 'STATUS' => $_new['STATUS']));
  134. if (!$result['status']) {
  135. $this->status = FALSE;
  136. $this->errors['STATUS'] = array($result['error_code'] => $result['error_message']);
  137. }
  138. }
  139. if (!$result['status']) {
  140. $this->errors[] = array($result['error_code'] => $result['error_message']);
  141. }
  142. return $this->reply($this->status, $this->errors);
  143. }
  144. /**
  145. * Get Sys interfaces
  146. *
  147. * @param Request $request
  148. * @return string - Ajax Reply
  149. */
  150. public function getSysInterfacesExecute(Request $request)
  151. {
  152. $reply = array();
  153. $result = Vesta::execute(Vesta::V_LIST_SYS_INTERFACES, array(Config::get('response_type')));
  154. foreach ($result['data'] as $iface) {
  155. $reply[$iface] = $iface;
  156. }
  157. if (!$result['status']) {
  158. $this->errors[] = array($result['error_code'] => $result['error_message']);
  159. }
  160. return $this->reply($result['status'], $reply);
  161. }
  162. }