api.arpdiag.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. /**
  3. * ARP diag report implementation
  4. */
  5. class ArpDiag {
  6. /**
  7. * Contains system alter config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * Contains system billing config as key=>value
  14. *
  15. * @var array
  16. */
  17. protected $billCfg = array();
  18. /**
  19. * Message helper placeholder
  20. *
  21. * @var object
  22. */
  23. protected $messages = '';
  24. /**
  25. * Contains default module URL
  26. */
  27. const URL_ME = '?module=arpdiag';
  28. /**
  29. * Creates new instance of object
  30. */
  31. public function __construct() {
  32. $this->loadConfigs();
  33. $this->initMessages();
  34. }
  35. /**
  36. * Loads system configs into protected property
  37. *
  38. * @global object $ubillingConfig
  39. *
  40. * @return void
  41. */
  42. protected function loadConfigs() {
  43. global $ubillingConfig;
  44. $this->altCfg = $ubillingConfig->getAlter();
  45. $this->billCfg = $ubillingConfig->getBilling();
  46. }
  47. /**
  48. * Inits new instance of message helper object
  49. *
  50. * @return void
  51. */
  52. protected function initMessages() {
  53. $this->messages = new UbillingMessageHelper();
  54. }
  55. /**
  56. * Renders control panel
  57. *
  58. * @return string
  59. */
  60. public function renderPanel() {
  61. $result = '';
  62. $result .= wf_Link(self::URL_ME, wf_img('skins/log_icon_small.png') . ' ' . __('Last events'), false, 'ubButton');
  63. $result .= wf_Link(self::URL_ME . '&arptable=true', wf_img('skins/icon_search_small.gif') . ' ' . __('Local ARP table'), false, 'ubButton');
  64. return ($result);
  65. }
  66. /**
  67. * Returns all switches IP as ip=>array(location,id)
  68. *
  69. * @return array
  70. */
  71. protected function getAllSwitchesIps() {
  72. $result = array();
  73. $switchesDb = new NyanORM('switches');
  74. $switchesDb->selectable(array('`id`', '`ip`', '`location`', '`swid`'));
  75. $allSwitches = $switchesDb->getAll();
  76. if (!empty($allSwitches)) {
  77. foreach ($allSwitches as $io => $each) {
  78. $result[$each['ip']]['location'] = $each['location'];
  79. $result[$each['ip']]['id'] = $each['id'];
  80. $result[$each['ip']]['mac'] = $each['swid'];
  81. }
  82. }
  83. return ($result);
  84. }
  85. /**
  86. * Returns row class for some event
  87. *
  88. * @param string $event
  89. *
  90. * @return string
  91. */
  92. protected function getEventClass($event) {
  93. $result = 'row3';
  94. if (ispos($event, 'attemp')) {
  95. $result = 'ukvbankstadup';
  96. }
  97. if (ispos($event, 'moved from')) {
  98. $result = 'undone';
  99. }
  100. if (ispos($event, 'new station')) {
  101. $result = 'todaysig';
  102. }
  103. if (ispos($event, 'ETHERTYPE')) {
  104. $result = 'sigcemeteryuser';
  105. }
  106. if (ispos($event, 'hardware')) {
  107. $result = 'donetask';
  108. }
  109. if (ispos($event, 'flip flop')) {
  110. $result = 'undone';
  111. }
  112. if (ispos($event, 'using my IP')) {
  113. $result = 'rowerror';
  114. }
  115. return ($result);
  116. }
  117. /**
  118. * Renders arp events report
  119. *
  120. * @return string
  121. */
  122. public function renderReport() {
  123. $log_path = $this->altCfg['ARPDIAG_LOG'];
  124. $sudo_path = $this->billCfg['SUDO'];
  125. $cat_path = $this->billCfg['CAT'];
  126. $grep_path = $this->billCfg['GREP'];
  127. $command = $sudo_path . ' ' . $cat_path . ' ' . $log_path . ' | ' . $grep_path . ' "arp"';
  128. $rawdata = shell_exec($command);
  129. $tablerows = '';
  130. if (!empty($rawdata)) {
  131. $splitdata = explodeRows($rawdata);
  132. if (!empty($splitdata)) {
  133. foreach ($splitdata as $eachrow) {
  134. if (!empty($eachrow)) {
  135. $rowclass = $this->getEventClass($eachrow);
  136. $tablecells = wf_TableCell($eachrow);
  137. $tablerows .= wf_TableRow($tablecells, $rowclass);
  138. }
  139. }
  140. }
  141. $result = wf_TableBody($tablerows, '100%', '0', '');
  142. } else {
  143. $result = $this->messages->getStyledMessage(__('It seems there is nothing unusual'), 'info');
  144. }
  145. return ($result);
  146. }
  147. /**
  148. * Returns clickable if possible host link by its IP
  149. *
  150. * @param array $allUserIps
  151. * @param array $allUserAddress
  152. * @param array $allSwitchesIps
  153. * @param string $ip
  154. *
  155. * @return string
  156. */
  157. protected function getHostLink($allUserIps, $allUserAddress, $allSwitchesIps, $ip) {
  158. $result = '';
  159. $userUrl = '?module=userprofile&username=';
  160. $switchUrl = '?module=switches&edit=';
  161. if (isset($allUserIps[$ip])) {
  162. $result = wf_Link($userUrl . $allUserIps[$ip], web_profile_icon(__('User')) . ' ' . @$allUserAddress[$allUserIps[$ip]], false);
  163. } else {
  164. if (isset($allSwitchesIps[$ip])) {
  165. $result = wf_Link($switchUrl . $allSwitchesIps[$ip]['id'], wf_img_sized('skins/menuicons/switches.png', __('Switch'), '11', '13') . ' ' . $allSwitchesIps[$ip]['location'], false);
  166. }
  167. }
  168. return ($result);
  169. }
  170. /**
  171. * Returns device MAC controls if required
  172. *
  173. * @param string $ip
  174. * @param string $mac
  175. * @param array $allUserIps
  176. * @param array $allUserIpMacs
  177. * @param array $allSwitchesIps
  178. *
  179. * @return string
  180. */
  181. protected function getMacControls($ip, $mac, $allUserIps, $allUserIpMacs, $allSwitchesIps) {
  182. $result = '';
  183. //normal user
  184. if (isset($allUserIps[$ip])) {
  185. if (isset($allUserIpMacs[$ip])) {
  186. if (($allUserIpMacs[$ip] != $mac) AND ( !empty($mac))) {
  187. $result = wf_img('skins/createtask.gif', __('MAC mismatch')) . ' ' . __('Oh no');
  188. } else {
  189. $result = wf_img_sized('skins/icon_ok.gif', '', 10, 10) . ' ' . __('Ok');
  190. }
  191. }
  192. } else {
  193. //Switches ID management enabled
  194. if ($this->altCfg['SWITCHES_EXTENDED']) {
  195. //registered switches directory device
  196. if (isset($allSwitchesIps[$ip])) {
  197. if (empty($allSwitchesIps[$ip]['mac'])) {
  198. if (check_mac_format($mac)) {
  199. if (cfr('SWITCHESEDIT')) {
  200. $result = wf_Link(self::URL_ME . '&arptable=true&swassign=' . $allSwitchesIps[$ip]['id'] . '&swmac=' . $mac, wf_img_sized('skins/add_icon.png', '', 10, 10) . ' ' . __('Assign'));
  201. }
  202. }
  203. } else {
  204. //switch already have mac
  205. if (!empty($mac) AND ( $mac != $allSwitchesIps[$ip]['mac'])) {
  206. $result = wf_img('skins/createtask.gif', __('MAC mismatch')) . ' ' . __('Oh no');
  207. } else {
  208. if ($mac) {
  209. $result = wf_img_sized('skins/icon_ok.gif', '', 10, 10) . ' ' . __('Normal');
  210. } else {
  211. $result = wf_img_sized('skins/icon_ok.gif', '', 10, 10) . ' ' . __('Normal') . ' (?)';
  212. }
  213. }
  214. }
  215. }
  216. }
  217. }
  218. return ($result);
  219. }
  220. /**
  221. * Assigns some switch MAC if required
  222. *
  223. * @param int $switchId
  224. * @param string $mac
  225. *
  226. * @return void
  227. */
  228. public function assignSwitchMac($switchId, $mac) {
  229. $switchId = vf($switchId, 3);
  230. $macF = mysql_real_escape_string($mac);
  231. if (cfr('SWITCHESEDIT')) {
  232. $where = "WHERE `id`='" . $switchId . "';";
  233. simple_update_field('switches', 'swid', $macF, $where);
  234. log_register('SWITCH CHANGE [' . $switchId . '] MAC `' . $mac . '`');
  235. }
  236. }
  237. /**
  238. * Returns JSON for actual ARP table
  239. *
  240. * @return void
  241. */
  242. public function ajaxReplyArp() {
  243. $json = new wf_JqDtHelper();
  244. $command = 'arp -a';
  245. $raw = shell_exec($command);
  246. if (!empty($raw)) {
  247. $allUserAddress = zb_AddressGetFulladdresslistCached();
  248. $allUserIps = zb_UserGetAllIPs();
  249. $allUserIps = array_flip($allUserIps);
  250. $allUserIpMacs = zb_UserGetAllIpMACs();
  251. $allSwitchesIps = $this->getAllSwitchesIps();
  252. $allSwitchesMac = array();
  253. $raw = explodeRows($raw);
  254. if (!empty($raw)) {
  255. foreach ($raw as $io => $each) {
  256. if (!empty($each)) {
  257. /**
  258. * And all you see are the cards that you can play
  259. * And you thought you'd get away with these games
  260. * And all you see are the cards there in your hand
  261. * And you thought that you have everything planned
  262. */
  263. $ip = zb_ExtractIpAddress($each);
  264. $mac = zb_ExtractMacAddress($each);
  265. $hostType = $this->getHostLink($allUserIps, $allUserAddress, $allSwitchesIps, $ip, $mac);
  266. $jsonItem[] = $ip;
  267. $jsonItem[] = $mac;
  268. $jsonItem[] = $hostType;
  269. $jsonItem[] = $this->getMacControls($ip, $mac, $allUserIps, $allUserIpMacs, $allSwitchesIps);
  270. $json->addRow($jsonItem);
  271. unset($jsonItem);
  272. }
  273. }
  274. }
  275. }
  276. $json->getJson();
  277. }
  278. /**
  279. * Renders localhost ARP table placeholder
  280. *
  281. * @return string
  282. */
  283. public function renderArpTable() {
  284. $result = wf_JqDtLoader(array('IP', 'MAC', 'Host', 'Actions'), self::URL_ME . '&ajaxarp=true', true, __('Host'), '100');
  285. return ($result);
  286. }
  287. }