IP.class.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. if(!VestaSession::getUserRole()){
  21. return self::getListUserIpsExecute($request);
  22. }
  23. $reply = array();
  24. $result = Vesta::execute(Vesta::V_LIST_SYS_IPS, array(Config::get('response_type')));
  25. foreach ($result['data'] as $ip => $details) {
  26. $reply[] = array_merge(
  27. array(
  28. 'IP_ADDRESS' => $ip,
  29. 'DATE' => date(Config::get('ui_date_format', strtotime($details['DATE'])))
  30. ), $details);
  31. }
  32. if (!$result['status']) {
  33. $this->errors[] = array($result['error_code'] => $result['error_message']);
  34. }
  35. return $this->reply($result['status'], $reply);
  36. }
  37. /**
  38. * Get user's IPs
  39. *
  40. * @param Request $request
  41. * @return string - Ajax Reply
  42. */
  43. public function getListUserIpsExecute(Request $request)
  44. {
  45. $user = $this->getLoggedUser();
  46. $reply = array();
  47. $result = Vesta::execute(Vesta::V_LIST_USER_IPS, array($user['uid'], Config::get('response_type')));
  48. foreach ($result['data'] as $ip => $details) {
  49. $reply[] = array_merge(
  50. array(
  51. 'IP_ADDRESS' => $ip,
  52. 'DATE' => date(Config::get('ui_date_format', strtotime($details['DATE'])))
  53. ), $details);
  54. }
  55. if (!$result['status']) {
  56. $this->errors[] = array($result['error_code'] => $result['error_message']);
  57. }
  58. return $this->reply($result['status'], $reply);
  59. }
  60. /**
  61. * Add IP entry
  62. *
  63. * @param Request $request
  64. * @return string - Ajax Reply
  65. */
  66. public function addExecute(Request $request)
  67. {
  68. $user = $this->getLoggedUser();
  69. $spell = $request->getParameter('spell');
  70. $params = array(
  71. 'IP_ADDRESS' => $spell['IP_ADDRESS'],
  72. 'MASK' => $spell['NETMASK'],
  73. 'INTERFACE' => $spell['INTERFACE'],
  74. 'OWNER' => $spell['OWNER'],
  75. 'STATUS' => $spell['STATUS']
  76. );
  77. if ($spell['NAME']) {
  78. $params['NAME'] = $spell['NAME'];
  79. }
  80. $result = Vesta::execute(Vesta::V_ADD_SYS_IP, $params);
  81. if (!$result['status']) {
  82. $this->errors[] = array($result['error_code'] => $result['error_message']);
  83. }
  84. return $this->reply($result['status'], $result['data']);
  85. }
  86. /**
  87. * Delete IP entry
  88. *
  89. * @param Request $request
  90. * @return string - Ajax Reply
  91. */
  92. public function deleteExecute(Request $request)
  93. {
  94. $spell = $request->getParameter('spell');
  95. $user = $this->getLoggedUser();
  96. $params = array(
  97. 'IP_ADDRESS' => $spell['IP_ADDRESS']
  98. );
  99. $result = Vesta::execute(Vesta::V_DEL_SYS_IP, $params);
  100. if (!$result['status']) {
  101. $this->errors[] = array($result['error_code'] => $result['error_message']);
  102. }
  103. return $this->reply($result['status'], $result['data']);
  104. }
  105. /**
  106. * Change IP entry
  107. *
  108. * @param Request $request
  109. * @return string - Ajax Reply
  110. */
  111. public function changeExecute(Request $request)
  112. {
  113. $user = $this->getLoggedUser();
  114. $_old = $request->getParameter('old');
  115. $_new = $request->getParameter('new');
  116. $this->status = TRUE;
  117. $this->errors = array();
  118. if ($_old['OWNER'] != $_new['OWNER']) {
  119. $result = array();
  120. $result = Vesta::execute(Vesta::V_CHANGE_SYS_IP_OWNER, array('IP' => $_new['IP_ADDRESS'], 'OWNER' => $_new['OWNER']));
  121. if (!$result['status']) {
  122. $this->status = FALSE;
  123. $this->errors['OWNER'] = array($result['error_code'] => $result['error_message']);
  124. }
  125. }
  126. // TODO: Handle NAME parameter
  127. if ($_old['NAME'] != $_new['NAME']) {
  128. $result = array();
  129. $result = Vesta::execute(Vesta::V_CHANGE_SYS_IP_NAME, array('IP' => $_new['IP_ADDRESS'], 'NAME' => $_new['NAME']));
  130. if (!$result['status']) {
  131. $this->status = FALSE;
  132. $this->errors['NAME'] = array($result['error_code'] => $result['error_message']);
  133. }
  134. }
  135. if ($_old['STATUS'] != $_new['STATUS']) {
  136. $result = array();
  137. $result = Vesta::execute(Vesta::V_CHANGE_SYS_IP_STATUS, array('IP' => $_new['IP_ADDRESS'], 'STATUS' => $_new['STATUS']));
  138. if (!$result['status']) {
  139. $this->status = FALSE;
  140. $this->errors['STATUS'] = array($result['error_code'] => $result['error_message']);
  141. }
  142. }
  143. if (!$result['status']) {
  144. $this->errors[] = array($result['error_code'] => $result['error_message']);
  145. }
  146. return $this->reply($this->status, $this->errors);
  147. }
  148. /**
  149. * Get Sys interfaces
  150. *
  151. * @param Request $request
  152. * @return string - Ajax Reply
  153. */
  154. public function getSysInterfacesExecute(Request $request)
  155. {
  156. $reply = array();
  157. $result = Vesta::execute(Vesta::V_LIST_SYS_INTERFACES, array(Config::get('response_type')));
  158. foreach ($result['data'] as $iface) {
  159. $reply[$iface] = $iface;
  160. }
  161. if (!$result['status']) {
  162. $this->errors[] = array($result['error_code'] => $result['error_message']);
  163. }
  164. return $this->reply($result['status'], $reply);
  165. }
  166. public function massiveDeleteExecute(Request $request)
  167. {
  168. $user = $this->getLoggedUser();
  169. $_entities = $request->getParameter('entities');
  170. foreach($_entities as $entity){
  171. $result = Vesta::execute(Vesta::V_DEL_SYS_IP, array($entity['IP_ADDRESS']));
  172. }
  173. return $this->reply($result['status'], $result['data']);
  174. }
  175. }