api.envy.php 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  1. <?php
  2. /**
  3. * Equipment configuration backup aka Envy implementation
  4. */
  5. class Envy {
  6. /**
  7. * Contains system billing.ini config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $billCfg = array();
  12. /**
  13. * System alter.ini config stored as key=>value
  14. *
  15. * @var array
  16. */
  17. protected $altCfg = array();
  18. /**
  19. * Contains all available devices models
  20. *
  21. * @var array
  22. */
  23. protected $allModels = array();
  24. /**
  25. * Contains filtered devices which need to be backuped as switchid=>data
  26. *
  27. * @var array
  28. */
  29. protected $allDevices = array();
  30. /**
  31. * Contains available envy-scripts as modelid=>data
  32. *
  33. * @var array
  34. */
  35. protected $allScripts = array();
  36. /**
  37. * Contain all available switches as switchid=>data
  38. *
  39. * @var arrays
  40. */
  41. protected $allSwitches = array();
  42. /**
  43. * Contains all available previously stored device configs from db as id=>recordData
  44. *
  45. * @var array
  46. */
  47. protected $allConfigs = array();
  48. /**
  49. * Envy devices data model placeholder
  50. *
  51. * @var object
  52. */
  53. protected $devices = '';
  54. /**
  55. * Envy scripts data model placeholder
  56. *
  57. * @var object
  58. */
  59. protected $scripts = '';
  60. /**
  61. * Envy archive data model placeholder
  62. *
  63. * @var object
  64. */
  65. protected $archive = '';
  66. /**
  67. * System messages helper object placeholder
  68. *
  69. * @var object
  70. */
  71. protected $messages = '';
  72. /**
  73. * Contains process manager instance
  74. *
  75. * @var object
  76. */
  77. protected $stardust = '';
  78. /**
  79. * Some other required consts for routing etc
  80. */
  81. const URL_ME = '?module=envy';
  82. const TMP_PATH = 'exports/';
  83. const SCRIPT_PREFIX = 'ENVYSCRIPT_';
  84. const DL_PREFIX = 'ENVYCONFIG_';
  85. const ROUTE_SCRIPTS = 'scriptsmgr';
  86. const ROUTE_DEVICES = 'devicesmgr';
  87. const ROUTE_DIFF = 'diff';
  88. const ROUTE_ARCHVIEW = 'viewarchiveid';
  89. const ROUTE_ARCHALL = 'archiveall';
  90. const ROUTE_ARCHIVE_AJ = 'ajarchive';
  91. const ROUTE_FILTER = 'devicefilter';
  92. const ROUTE_CLEANUP = 'cleanuparchive';
  93. const ENVYPROC_PID = 'ENVYPROC_';
  94. /**
  95. * ___ _ ____ ___ _
  96. * / _ \ '_ \ \ / / | | |
  97. * | __/ | | \ V /| |_| |
  98. * \___|_| |_|\_/ \__, |
  99. * __/ |
  100. * |___/
  101. */
  102. /**
  103. * Creates new envy sin instance
  104. */
  105. public function __construct() {
  106. $this->initMessages();
  107. $this->loadConfigs();
  108. $this->loadAlter();
  109. $this->loadDeviceModels();
  110. $this->loadSwitches();
  111. $this->initScrips();
  112. $this->initStarDust();
  113. $this->loadScripts();
  114. $this->initDevices();
  115. $this->loadDevices();
  116. $this->initArchive();
  117. $this->loadArchive();
  118. }
  119. /**
  120. * Loads all required configs into protected props
  121. *
  122. * @global object $ubillingConfig
  123. *
  124. * @return
  125. */
  126. protected function loadConfigs() {
  127. global $ubillingConfig;
  128. $this->billCfg = $ubillingConfig->getBilling();
  129. $this->ubConfig = $ubillingConfig;
  130. }
  131. /**
  132. * Loads system alter.ini config into private data property
  133. *
  134. * @return void
  135. */
  136. protected function loadAlter() {
  137. $this->altCfg = $this->ubConfig->getAlter();
  138. }
  139. /**
  140. * Creates new message helper instance
  141. *
  142. * @return void
  143. */
  144. protected function initMessages() {
  145. $this->messages = new UbillingMessageHelper();
  146. }
  147. /**
  148. * Creates new devices data model instance
  149. *
  150. * @return void
  151. */
  152. protected function initDevices() {
  153. $this->devices = new NyanORM('envydevices');
  154. }
  155. /**
  156. * Creates new scrips data model instance
  157. *
  158. * @return void
  159. */
  160. protected function initScrips() {
  161. $this->scripts = new NyanORM('envyscripts');
  162. }
  163. /**
  164. * Creates new archive data model instance
  165. *
  166. * @return void
  167. */
  168. protected function initArchive() {
  169. $this->archive = new NyanORM('envydata');
  170. }
  171. /**
  172. * Inits process manager
  173. *
  174. * @return void
  175. */
  176. protected function initStarDust() {
  177. $this->stardust = new StarDust();
  178. }
  179. /**
  180. * Performs check of Switch envy process lock via DB.
  181. * Using this only for checks of possibility real collector runs.
  182. *
  183. * @param int $swId
  184. *
  185. * @return bool
  186. */
  187. protected function isProcessLocked($swIP) {
  188. $this->stardust->setProcess(self::ENVYPROC_PID . $swIP);
  189. $result = $this->stardust->isRunning();
  190. return($result);
  191. }
  192. /**
  193. * Updates some Switch process stats
  194. *
  195. * @param int $swIP Existing Switch IP
  196. * @param int $processStartTime process start timestame
  197. * @param int $processEndTime process end timestamp
  198. * @param bool $finished process finished or not flag
  199. *
  200. * @return void
  201. */
  202. protected function processStatsUpdate($swIP, $finished = false) {
  203. //collector process locking and releasing of locks here
  204. if ($finished) {
  205. //release lock
  206. $this->stardust->setProcess(self::ENVYPROC_PID . $swIP);
  207. $this->stardust->stop();
  208. } else {
  209. //set lock for process of some DevID
  210. $this->stardust->setProcess(self::ENVYPROC_PID . $swIP);
  211. $this->stardust->start();
  212. }
  213. }
  214. /**
  215. * Loads all available envy devices from database for further usage
  216. *
  217. * @return void
  218. */
  219. protected function loadDevices() {
  220. $this->allDevices = $this->devices->getAll('switchid');
  221. }
  222. /**
  223. * Loads all available envy scripts from database for further usage
  224. *
  225. * @return void
  226. */
  227. protected function loadScripts() {
  228. $this->allScripts = $this->scripts->getAll('modelid');
  229. }
  230. /**
  231. * Loads available device models from database
  232. *
  233. * @return void
  234. */
  235. protected function loadDeviceModels() {
  236. $this->allModels = zb_SwitchModelsGetAll();
  237. }
  238. /**
  239. * Loads all existing swithes directory from database into protected prop
  240. *
  241. * @return void
  242. */
  243. protected function loadSwitches() {
  244. $switchesTmp = zb_SwitchesGetAll();
  245. if (!empty($switchesTmp)) {
  246. foreach ($switchesTmp as $io => $each) {
  247. $this->allSwitches[$each['id']] = $each;
  248. }
  249. }
  250. }
  251. /**
  252. * Loads existing archive records from database
  253. *
  254. * @return void
  255. */
  256. protected function loadArchive() {
  257. $this->archive->selectable(array('id', 'date', 'switchid'));
  258. $this->archive->orderBy('id', 'ASC'); // must be from old to new due the getLastDate mechanics
  259. $this->allConfigs = $this->archive->getAll('id');
  260. }
  261. /**
  262. * Renders envy-script creation form
  263. *
  264. * @return string
  265. */
  266. protected function renderScriptCreateForm() {
  267. $result = '';
  268. if (!empty($this->allModels)) {
  269. $inputs = '';
  270. $modelsTmp = array();
  271. foreach ($this->allModels as $io => $each) {
  272. if (!isset($this->allScripts[$each['id']])) {
  273. $modelsTmp[$each['id']] = $each['modelname'];
  274. }
  275. }
  276. /**
  277. * I am the way and the truth and the life. No one comes to the Father except through me
  278. */
  279. $inputs .= wf_Selector('newscriptmodel', $modelsTmp, __('Model'), '', true);
  280. $inputs .= __('Script') . wf_tag('br');
  281. $inputs .= wf_TextArea('newscriptdata', '', '', true, '60x20');
  282. $inputs .= wf_Submit(__('Create'));
  283. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  284. } else {
  285. show_error(__('Available switch models') . ': ' . __('No'));
  286. }
  287. return($result);
  288. }
  289. /**
  290. * Renders envy-script editing form
  291. *
  292. * @return string
  293. */
  294. protected function renderScriptEditForm($modelId) {
  295. $result = '';
  296. $modelId = ubRouting::filters($modelId, 'int');
  297. if (isset($this->allScripts[$modelId])) {
  298. $scriptData = $this->allScripts[$modelId];
  299. $inputs = '';
  300. $inputs .= wf_HiddenInput('editscriptid', $scriptData['id']);
  301. $inputs .= wf_HiddenInput('editscriptmodel', $scriptData['modelid'], __('Model'), '', true);
  302. $inputs .= __('Script') . wf_tag('br');
  303. $inputs .= wf_TextArea('editscriptdata', '', $scriptData['data'], true, '60x20');
  304. $inputs .= wf_Submit(__('Save'));
  305. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  306. }
  307. return($result);
  308. }
  309. /**
  310. * Creates new envy script preset in database
  311. *
  312. * @param int $modelId
  313. * @param string $scriptData
  314. *
  315. * @return void/string on error
  316. */
  317. public function createScript($modelId, $scriptData) {
  318. $result = '';
  319. $modelId = ubRouting::filters($modelId, 'int');
  320. $scriptData = ubRouting::filters($scriptData, 'mres');
  321. if (!empty($modelId)) {
  322. if (!isset($this->allScripts[$modelId])) {
  323. $this->scripts->data('modelid', $modelId);
  324. $this->scripts->data('data', $scriptData);
  325. $this->scripts->create();
  326. $newId = $this->scripts->getLastId();
  327. log_register('ENVY CREATE SCRIPT [' . $newId . '] MODEL [' . $modelId . ']');
  328. } else {
  329. $result .= __('Something went wrong') . ': EX_ALREADY_EXISTS';
  330. }
  331. } else {
  332. $result .= __('Something went wrong') . ': EX_NOMODELID';
  333. }
  334. return($result);
  335. }
  336. /**
  337. * Saves changes in envy script
  338. *
  339. * @return void/string on error
  340. */
  341. public function saveScript() {
  342. $result = '';
  343. if (ubRouting::checkPost(array('editscriptid', 'editscriptmodel'))) {
  344. $scriptId = ubRouting::post('editscriptid', 'int');
  345. $modelId = ubRouting::post('editscriptmodel', 'int');
  346. $scriptData = ubRouting::post('editscriptdata', 'mres');
  347. if (isset($this->allScripts[$modelId])) {
  348. $this->scripts->where('id', '=', $scriptId);
  349. $this->scripts->data('data', $scriptData);
  350. $this->scripts->save();
  351. log_register('ENVY CHANGE SCRIPT [' . $scriptId . '] MODEL [' . $modelId . ']');
  352. } else {
  353. $result .= __('Something went wrong') . ': EX_WRONGMODELID';
  354. }
  355. }
  356. return($result);
  357. }
  358. /**
  359. * Deletes existing envy script from database
  360. *
  361. * @param int $modelId
  362. *
  363. * @return void/string on result
  364. */
  365. public function deleteScript($modelId) {
  366. $result = '';
  367. $modelId = ubRouting::filters($modelId, 'int');
  368. if (!empty($modelId)) {
  369. if (isset($this->allScripts[$modelId])) {
  370. $scriptData = $this->allScripts[$modelId];
  371. $this->scripts->where('modelid', '=', $modelId);
  372. $this->scripts->delete();
  373. log_register('ENVY DELETE SCRIPT [' . $scriptData['id'] . '] MODEL [' . $modelId . ']');
  374. } else {
  375. $result .= __('Something went wrong') . ': EX_WRONGMODELID';
  376. }
  377. } else {
  378. $result .= __('Something went wrong') . ': EX_NOMODELID';
  379. }
  380. return($result);
  381. }
  382. /**
  383. * Renders available envy scripts and some controls
  384. *
  385. * @return string
  386. */
  387. public function renderScriptsList() {
  388. $result = '';
  389. $allModelNames = array();
  390. if (!empty($this->allModels)) {
  391. foreach ($this->allModels as $io => $each) {
  392. $allModelNames[$each['id']] = $each['modelname'];
  393. }
  394. }
  395. if (!empty($this->allScripts)) {
  396. $cells = wf_TableCell(__('Equipment models'));
  397. $cells .= wf_TableCell(__('Actions'));
  398. $rows = wf_TableRow($cells, 'row1');
  399. foreach ($this->allScripts as $io => $each) {
  400. $cells = wf_TableCell(@$allModelNames[$each['modelid']]);
  401. $scriptControls = '';
  402. $scriptControls .= wf_JSAlert(self::URL_ME . '&deletescript=' . $each['modelid'], web_delete_icon(), $this->messages->getDeleteAlert()) . ' ';
  403. $scriptControls .= wf_modalAuto(web_edit_icon(), __('Edit') . ' ' . @$allModelNames[$each['modelid']], $this->renderScriptEditForm($each['modelid']));
  404. $cells .= wf_TableCell($scriptControls);
  405. $rows .= wf_TableRow($cells, 'row5');
  406. }
  407. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  408. } else {
  409. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'warning');
  410. }
  411. return($result);
  412. }
  413. /**
  414. * Renders default controls panel for module
  415. *
  416. * @return string
  417. */
  418. public function renderControls() {
  419. $result = '';
  420. if (ubRouting::checkGet(self::ROUTE_SCRIPTS) OR ubRouting::checkGet(self::ROUTE_DEVICES) OR ubRouting::checkGet(self::ROUTE_DIFF)) {
  421. if (!ubRouting::checkGet('devfilter')) {
  422. $result .= wf_BackLink(self::URL_ME) . ' '; //default back control
  423. } else {
  424. $result .= wf_BackLink(self::URL_ME . '&' . self::ROUTE_DEVICES . '=true') . ' '; //devfiltered diff backs to device manager
  425. }
  426. } else {
  427. $result .= wf_Link(self::URL_ME . '&' . self::ROUTE_SCRIPTS . '=true', wf_img('skins/switch_models.png') . ' ' . __('Scripts'), false, 'ubButton') . ' ';
  428. $result .= wf_Link(self::URL_ME . '&' . self::ROUTE_DEVICES . '=true', wf_img('skins/ymaps/switchdir.png') . ' ' . __('Devices'), false, 'ubButton') . ' ';
  429. $result .= wf_Link(self::URL_ME . '&' . self::ROUTE_DIFF . '=true', wf_img('skins/diff_icon.png') . ' ' . __('Changes'), false, 'ubButton') . ' ';
  430. $cleanupAlert = __('All configs for each of devices will be deletet from the archive except the last one');
  431. $result .= wf_ConfirmDialog(self::URL_ME . '&' . self::ROUTE_CLEANUP . '=true', wf_img('skins/icon_cleanup.png') . ' ' . __('Cleanup'), $cleanupAlert, 'ubButton', self::URL_ME);
  432. }
  433. if (ubRouting::checkGet(self::ROUTE_SCRIPTS)) {
  434. $result .= wf_modalAuto(web_icon_create() . ' ' . __('Create new script'), __('Create new script'), $this->renderScriptCreateForm(), 'ubButton') . ' ';
  435. }
  436. if (ubRouting::checkGet(self::ROUTE_DEVICES)) {
  437. $result .= wf_modalAuto(web_icon_create() . ' ' . __('Create new device'), __('Create new device'), $this->renderDeviceCreateForm(), 'ubButton') . ' ';
  438. $saveAllNotice = $this->messages->getEditAlert() . ' ' . __('Store all devices configs into archive') . '?';
  439. $result .= wf_JSAlert(self::URL_ME . '&' . self::ROUTE_ARCHALL . '=true', wf_img('skins/icon_restoredb.png') . ' ' . __('Store all'), $saveAllNotice, '', 'ubButton');
  440. }
  441. return($result);
  442. }
  443. /**
  444. * Renders new device creation form
  445. *
  446. * @return string
  447. */
  448. public function renderDeviceCreateForm() {
  449. $result = '';
  450. $switchesTmp = array();
  451. if (!empty($this->allSwitches)) {
  452. if (!empty($this->allScripts)) {
  453. foreach ($this->allSwitches as $io => $each) {
  454. if (!isset($this->allDevices[$each['id']])) {
  455. if (isset($this->allScripts[$each['modelid']])) {
  456. $switchesTmp[$each['id']] = $each['ip'] . ' - ' . $each['location'];
  457. }
  458. }
  459. }
  460. $inputs = '';
  461. $inputs .= wf_Selector('newdeviceswitchid', $switchesTmp, __('Switch'), '', true);
  462. $inputs .= wf_TextInput('newdevicelogin', __('Login'), '', true, '');
  463. $inputs .= wf_PasswordInput('newdevicepassword', __('Password'), '', true, '');
  464. $inputs .= wf_PasswordInput('newdeviceenablepassword', __('Enable password'), '', true, '');
  465. $inputs .= wf_TextInput('newdevicecustom1', __('Custom field'), '', true, '');
  466. $inputs .= wf_TextInput('newdevicecutstart', __('Lines to cut at start'), '0', true, '');
  467. $inputs .= wf_TextInput('newdevicecutend', __('Lines to cut at end'), '0', true, '');
  468. $inputs .= wf_TextInput('newdeviceport', __('Port'), '', true, '');
  469. $inputs .= wf_CheckInput('newdeviceactive', __('Active'), true, true);
  470. $inputs .= wf_delimiter(0);
  471. $inputs .= wf_Submit(__('Create'));
  472. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  473. } else {
  474. $result .= $this->messages->getStyledMessage(__('Available envy scripts') . ': ' . __('No'), 'error');
  475. }
  476. } else {
  477. $result .= $this->messages->getStyledMessage(__('Available switches') . ': ' . __('No'), 'error');
  478. }
  479. return($result);
  480. }
  481. /**
  482. * Renders device editing form for existing envy-device
  483. *
  484. * @param int $switchId
  485. *
  486. * @return string
  487. */
  488. protected function renderDeviceEditForm($switchId) {
  489. $result = '';
  490. $deviceId = ubRouting::filters($switchId, 'int');
  491. if (isset($this->allDevices[$switchId])) {
  492. $deviceData = $this->allDevices[$switchId];
  493. $inputs = '';
  494. $inputs .= wf_HiddenInput('editdeviceid', $deviceData['id']);
  495. $inputs .= wf_HiddenInput('editdeviceswitchid', $deviceData['switchid']);
  496. $inputs .= wf_TextInput('editdevicelogin', __('Login'), $deviceData['login'], true, '');
  497. $inputs .= wf_PasswordInput('editdevicepassword', __('Password'), $deviceData['password'], true, '');
  498. $inputs .= wf_PasswordInput('editdeviceenablepassword', __('Enable password'), $deviceData['enablepassword'], true, '');
  499. $inputs .= wf_TextInput('editdevicecustom1', __('Custom field'), $deviceData['custom1'], true, '');
  500. $inputs .= wf_TextInput('editdevicecutstart', __('Lines to cut at start'), $deviceData['cutstart'], true, '');
  501. $inputs .= wf_TextInput('editdevicecutend', __('Lines to cut at end'), $deviceData['cutend'], true, '');
  502. $inputs .= wf_TextInput('editdeviceport', __('Port'), $deviceData['port'], true, '');
  503. $inputs .= wf_CheckInput('editdeviceactive', __('Active'), true, $deviceData['active']);
  504. $inputs .= wf_delimiter(0);
  505. $inputs .= wf_Submit(__('Save'));
  506. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  507. } else {
  508. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': EX_NODEVICE', 'error');
  509. }
  510. return($result);
  511. }
  512. /**
  513. * Creates new device in database
  514. *
  515. * @return string
  516. */
  517. public function createDevice() {
  518. $result = '';
  519. if (ubRouting::checkPost(array('newdeviceswitchid'))) {
  520. $switchId = ubRouting::post('newdeviceswitchid', 'int');
  521. $active = (ubRouting::post('newdeviceactive')) ? 1 : 0;
  522. $login = ubRouting::post('newdevicelogin', 'mres');
  523. $password = ubRouting::post('newdevicepassword', 'mres');
  524. $enablepassword = ubRouting::post('newdeviceenablepassword', 'mres');
  525. $custom1 = ubRouting::post('newdevicecustom1', 'mres');
  526. $cutstart = ubRouting::post('newdevicecutstart', 'int');
  527. $cutend = ubRouting::post('newdevicecutend', 'int');
  528. $port = ubRouting::post('newdeviceport', 'int');
  529. if (!empty($switchId)) {
  530. if (!isset($this->allDevices[$switchId])) {
  531. if (isset($this->allSwitches[$switchId])) {
  532. $this->devices->data('switchid', $switchId);
  533. $this->devices->data('active', $active);
  534. $this->devices->data('login', $login);
  535. $this->devices->data('password', $password);
  536. $this->devices->data('enablepassword', $enablepassword);
  537. $this->devices->data('custom1', $custom1);
  538. $this->devices->data('cutstart', $cutstart);
  539. $this->devices->data('cutend', $cutend);
  540. $this->devices->data('port', $port);
  541. $this->devices->create();
  542. $newId = $this->devices->getLastId();
  543. log_register('ENVY CREATE DEVICE [' . $newId . '] SWITCHID [' . $switchId . ']');
  544. } else {
  545. $result .= __('Something went wrong') . ': EX_WRONGSWITCHID [' . $switchId . ']';
  546. }
  547. } else {
  548. $result .= __('Something went wrong') . ': EX_DEVICEALREADYEXISTS';
  549. }
  550. } else {
  551. $result .= __('Something went wrong') . ': EX_EMPTYSWITCHID';
  552. }
  553. }
  554. return($result);
  555. }
  556. /**
  557. * Catches device editing request and saves changes in database
  558. *
  559. * @return void/string on error
  560. */
  561. public function saveDevice() {
  562. $result = '';
  563. if (ubRouting::checkPost(array('editdeviceswitchid', 'editdeviceid'))) {
  564. $deviceId = ubRouting::post('editdeviceid', 'int');
  565. $switchId = ubRouting::post('editdeviceswitchid', 'int');
  566. $active = (ubRouting::post('editdeviceactive')) ? 1 : 0;
  567. $login = ubRouting::post('editdevicelogin', 'mres');
  568. $password = ubRouting::post('editdevicepassword', 'mres');
  569. $enablepassword = ubRouting::post('editdeviceenablepassword', 'mres');
  570. $custom1 = ubRouting::post('editdevicecustom1', 'mres');
  571. $cutstart = ubRouting::post('editdevicecutstart', 'int');
  572. $cutend = ubRouting::post('editdevicecutend', 'int');
  573. $port = ubRouting::post('editdeviceport', 'int');
  574. if (!empty($switchId)) {
  575. if (isset($this->allDevices[$switchId])) {
  576. if (isset($this->allSwitches[$switchId])) {
  577. $this->devices->where('id', '=', $deviceId);
  578. $this->devices->data('active', $active);
  579. $this->devices->data('login', $login);
  580. $this->devices->data('password', $password);
  581. $this->devices->data('enablepassword', $enablepassword);
  582. $this->devices->data('custom1', $custom1);
  583. $this->devices->data('cutstart', $cutstart);
  584. $this->devices->data('cutend', $cutend);
  585. $this->devices->data('port', $port);
  586. $this->devices->save();
  587. log_register('ENVY EDIT DEVICE [' . $deviceId . '] SWITCHID [' . $switchId . ']');
  588. } else {
  589. $result .= __('Something went wrong') . ': EX_WRONGSWITCHID [' . $switchId . ']';
  590. }
  591. } else {
  592. $result .= __('Something went wrong') . ': EX_DEVICENOTEXISTS';
  593. }
  594. } else {
  595. $result .= __('Something went wrong') . ': EX_EMPTYSWITCHID';
  596. }
  597. }
  598. return($result);
  599. }
  600. /**
  601. * Deletes existing envy device from database
  602. *
  603. * @param int $switchId
  604. *
  605. * @return void/string on error
  606. */
  607. public function deleteDevice($switchId) {
  608. $result = '';
  609. $switchId = ubRouting::filters($switchId, 'int');
  610. if (!empty($switchId)) {
  611. if (isset($this->allDevices[$switchId])) {
  612. $devData = $this->allDevices[$switchId];
  613. $this->devices->where('switchid', '=', $switchId);
  614. $this->devices->delete();
  615. log_register('ENVY DELETE DEVICE [' . $devData['id'] . '] SWITCHID [' . $switchId . ']');
  616. } else {
  617. $result .= __('Something went wrong') . ': EX_WRONGSWITCHID [' . $switchId . ']';
  618. }
  619. } else {
  620. $result .= __('Something went wrong') . ': EX_EMPTYSWITCHID';
  621. }
  622. return($result);
  623. }
  624. /**
  625. * Returns last envy-device configuration date
  626. *
  627. * @param int $switchId
  628. *
  629. * @return string
  630. */
  631. protected function getLastConfigDate($switchId) {
  632. $result = '';
  633. if (!empty($this->allConfigs)) {
  634. foreach ($this->allConfigs as $io => $each) {
  635. if ($each['switchid'] == $switchId) {
  636. $result = $each['date'];
  637. }
  638. }
  639. }
  640. return($result);
  641. }
  642. /**
  643. * Renders available envy-devices with some their params
  644. *
  645. * @return string
  646. */
  647. public function renderDevicesList() {
  648. $result = '';
  649. if (!empty($this->allDevices)) {
  650. $countActive = 0;
  651. $countInactive = 0;
  652. $allModelNames = array();
  653. if (!empty($this->allModels)) {
  654. foreach ($this->allModels as $io => $each) {
  655. $allModelNames[$each['id']] = $each['modelname'];
  656. }
  657. }
  658. $cells = wf_TableCell(__('ID'));
  659. $cells .= wf_TableCell(__('Latest config'));
  660. $cells .= wf_TableCell(__('IP'));
  661. $cells .= wf_TableCell(__('Switch'));
  662. $cells .= wf_TableCell(__('Model'));
  663. $cells .= wf_TableCell(__('Active'));
  664. $cells .= wf_TableCell(__('Actions'));
  665. $rows = wf_TableRow($cells, 'row1');
  666. foreach ($this->allDevices as $io => $each) {
  667. $switchData = @$this->allSwitches[$each['switchid']];
  668. $rowClass = 'row5';
  669. //this switch may be deleted
  670. if (empty($switchData)) {
  671. $switchData = array(
  672. 'id' => $each['switchid'],
  673. 'modelid' => 0,
  674. 'ip' => __('Deleted'),
  675. 'desc' => '',
  676. 'location' => __('Deleted'),
  677. 'snmp' => '',
  678. 'geo' => '',
  679. 'parentid' => '',
  680. 'swid' => '',
  681. 'snmpwrite' => '',
  682. );
  683. $rowClass = 'sigdeleteduser';
  684. }
  685. $scriptAvailable = (isset($this->allScripts[$switchData['modelid']])) ? true : false;
  686. $modelLabel = ($scriptAvailable) ? wf_img_sized('skins/icon_ok.gif', __('Envy script available'), '12') : wf_img_sized('skins/delete_small.png', __('Envy script unavailable'), '12');
  687. $cells = wf_TableCell($each['switchid']);
  688. $cells .= wf_TableCell($this->getLastConfigDate($each['switchid']));
  689. $cells .= wf_TableCell($switchData['ip']);
  690. $cells .= wf_TableCell($switchData['location']);
  691. $cells .= wf_TableCell($modelLabel . ' ' . @$allModelNames[$switchData['modelid']]);
  692. $cells .= wf_TableCell(web_bool_led($each['active']));
  693. $devControls = '';
  694. $devControls .= wf_JSAlert(self::URL_ME . '&deletedevice=' . $each['switchid'], web_delete_icon(), $this->messages->getDeleteAlert()) . ' ';
  695. $devControls .= wf_modalAuto(web_edit_icon(), __('Edit') . ' ' . $switchData['ip'], $this->renderDeviceEditForm($each['switchid'])) . ' ';
  696. $devControls .= wf_Link(self::URL_ME . '&previewdevice=' . $each['switchid'], web_icon_search('Preview')) . ' ';
  697. $storeAlert = $this->messages->getEditAlert() . ' ' . __('Backup device configuration to archive') . '?';
  698. $devControls .= wf_JSAlert(self::URL_ME . '&' . self::ROUTE_DEVICES . '&=true' . '&storedevice=' . $each['switchid'], wf_img('skins/icon_restoredb.png', __('Backup device configuration to archive')), $storeAlert) . ' ';
  699. $devControls .= wf_Link('?module=switches&edit=' . $each['switchid'], wf_img('skins/menuicons/switches.png', __('Go to switch'))) . ' ';
  700. $devControls .= wf_Link(self::URL_ME . '&' . self::ROUTE_DIFF . '=true' . '&devfilter=' . $each['switchid'], wf_img('skins/diff_icon.png', __('Changes')));
  701. $cells .= wf_TableCell($devControls);
  702. $rows .= wf_TableRow($cells, $rowClass);
  703. if ($each['active']) {
  704. $countActive++;
  705. } else {
  706. $countInactive++;
  707. }
  708. }
  709. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  710. $countersLabel = __('Total') . ': ' . ($countActive + $countInactive) . ' ' . __('Active') . ': ' . $countActive . ' ' . __('Inactive') . ': ' . $countInactive;
  711. $result .= wf_tag('br') . wf_tag('b', false) . $countersLabel . wf_tag('b', true);
  712. } else {
  713. $result .= $this->messages->getStyledMessage(__('No envy devices available'), 'warning');
  714. }
  715. return($result);
  716. }
  717. /**
  718. * Returns deivice polling script with preprocessed macro data
  719. *
  720. * @param int $switchId
  721. *
  722. * @return string
  723. */
  724. protected function parseMacro($switchId) {
  725. $result = '';
  726. if (isset($this->allDevices[$switchId])) {
  727. if (isset($this->allSwitches[$switchId])) {
  728. if (isset($this->allScripts[$this->allSwitches[$switchId]['modelid']])) {
  729. $result = $this->allScripts[$this->allSwitches[$switchId]['modelid']]['data'];
  730. $deviceData = $this->allDevices[$switchId];
  731. $macroLogin = (!empty($deviceData['login'])) ? $deviceData['login'] : 'empty_login';
  732. $macroPass = (!empty($deviceData['password'])) ? $deviceData['password'] : 'empty_password';
  733. $macroEnPass = (!empty($deviceData['enablepassword'])) ? $deviceData['enablepassword'] : 'empty_enablepassword';
  734. $macroCust1 = (!empty($deviceData['custom1'])) ? $deviceData['custom1'] : 'empty_custom1';
  735. $macroPort = (!empty($deviceData['port'])) ? $deviceData['port'] : 'empty_port';
  736. //some macro replacing here
  737. $result = str_replace('{IP}', $this->allSwitches[$switchId]['ip'], $result);
  738. $result = str_replace('{LOGIN}', $macroLogin, $result);
  739. $result = str_replace('{PASSWORD}', $macroPass, $result);
  740. $result = str_replace('{ENABLEPASSWORD}', $macroEnPass, $result);
  741. $result = str_replace('{CUSTOM1}', $macroCust1, $result);
  742. $result = str_replace('{PORT}', $macroPort, $result);
  743. }
  744. }
  745. }
  746. return($result);
  747. }
  748. /**
  749. * Runs envy script for some envy device and returns script result
  750. *
  751. * @param int $switchId
  752. *
  753. * @return string
  754. */
  755. public function runDeviceScript($switchId) {
  756. $result = '';
  757. if (isset($this->allDevices[$switchId])) {
  758. $scriptData = $this->parseMacro($switchId);
  759. if (!empty($scriptData)) {
  760. $filePath = self::TMP_PATH . self::SCRIPT_PREFIX . $switchId;
  761. file_put_contents($filePath, $scriptData);
  762. $result .= shell_exec($this->billCfg['EXPECT_PATH'] . ' ' . $filePath);
  763. }
  764. }
  765. return($result);
  766. }
  767. /**
  768. * Renders script results preview
  769. *
  770. * @param string $data
  771. *
  772. * @return string
  773. */
  774. public function previewScriptsResult($data) {
  775. $result = '';
  776. if (!empty($data)) {
  777. $inputs = wf_tag('textarea', false, 'fileeditorarea', 'name="envypreview" cols="145" rows="30" spellcheck="false"');
  778. $inputs .= $data;
  779. $inputs .= wf_tag('textarea', true);
  780. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  781. } else {
  782. $result .= $this->messages->getStyledMessage(__('Empty reply received'), 'warning');
  783. }
  784. return($result);
  785. }
  786. /**
  787. * Renders form for filtering some envy-device in archive
  788. *
  789. * @return string
  790. */
  791. public function renderArchiveFilterForm() {
  792. $result = '';
  793. $devicesTmp = array('0' => __('All'));
  794. if (!empty($this->allDevices)) {
  795. foreach ($this->allDevices as $io => $each) {
  796. @$switchData = $this->allSwitches[$each['switchid']];
  797. $devicesTmp[$each['switchid']] = @$switchData['ip'] . ' - ' . @$switchData['location'];
  798. }
  799. $curDeviceId = ubRouting::checkPost('devicefilter') ? ubRouting::post('devicefilter', 'int') : 0;
  800. $inputs = wf_Selector('devicefilter', $devicesTmp, __('Device'), $curDeviceId, false, false) . ' ';
  801. $inputs .= wf_Submit(__('Show'));
  802. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  803. }
  804. return($result);
  805. }
  806. /**
  807. * Renders previously envy data arhive container
  808. *
  809. * @return string
  810. */
  811. public function renderArchive() {
  812. $result = '';
  813. $columns = array('Date', 'IP', 'Device', 'Actions');
  814. $opts = '"order": [[ 0, "desc" ]]';
  815. $devFilter = ubRouting::checkPost('devicefilter') ? ubRouting::post('devicefilter', 'int') : 0;
  816. $result .= wf_JqDtLoader($columns, self::URL_ME . '&' . self::ROUTE_ARCHIVE_AJ . '=true&' . self::ROUTE_FILTER . '=' . $devFilter, false, __('Config'), 100, $opts);
  817. $result .= wf_delimiter(0);
  818. $result .= $this->renderArchiveFilterForm();
  819. return($result);
  820. }
  821. /**
  822. * Renders background JSON data for existing configs archive
  823. *
  824. * @return void
  825. */
  826. public function getAjArchive() {
  827. $json = new wf_JqDtHelper();
  828. $devFilter = ubRouting::checkGet('devicefilter') ? ubRouting::get('devicefilter', 'int') : 0;
  829. if (!empty($this->allConfigs)) {
  830. foreach ($this->allConfigs as $io => $each) {
  831. if (!$devFilter OR $devFilter == $each['switchid']) {
  832. @$switchData = $this->allSwitches[$each['switchid']];
  833. //maybe deleted switch?
  834. if (empty($switchData)) {
  835. $switchData = array(
  836. 'ip' => __('Deleted'),
  837. 'location' => __('ID') . ' [' . $each['switchid'] . '] ' . __('Deleted'),
  838. );
  839. }
  840. $data[] = $each['date'];
  841. $data[] = @$switchData['ip'];
  842. $data[] = @$switchData['location'];
  843. $archControls = '';
  844. $archControls .= wf_JSAlert(self::URL_ME . '&deletearchiveid=' . $each['id'], web_delete_icon(), $this->messages->getDeleteAlert() . ' ' . $each['date']) . ' ';
  845. $archControls .= wf_Link(self::URL_ME . '&' . self::ROUTE_ARCHVIEW . '=' . $each['id'], web_icon_search('Config')) . ' ';
  846. $storeAlert = $this->messages->getEditAlert() . ' ' . __('Backup device configuration to archive') . '?';
  847. $archControls .= wf_JSAlert(self::URL_ME . '&' . self::ROUTE_DEVICES . '&=true' . '&storedevice=' . $each['switchid'] . '&resave=true', wf_img('skins/icon_envy_resave.png', __('Backup device configuration to archive')), $storeAlert) . ' ';
  848. $archControls .= wf_Link(self::URL_ME . '&downloadarchiveid=' . $each['id'], web_icon_download());
  849. $data[] = $archControls;
  850. $json->addRow($data);
  851. unset($data);
  852. }
  853. }
  854. }
  855. $json->getJson();
  856. }
  857. /**
  858. * Saves device config data into archive
  859. *
  860. * @param int $switchId
  861. * @param string $data
  862. *
  863. * @return void/string on error
  864. */
  865. protected function storeArchiveData($switchId, $data) {
  866. $result = '';
  867. $switchId = ubRouting::filters($switchId, 'int');
  868. if (!empty($switchId)) {
  869. if (isset($this->allDevices[$switchId])) {
  870. $curdate = curdatetime();
  871. $startOffset = ($this->allDevices[$switchId]['cutstart']) ? $this->allDevices[$switchId]['cutstart'] : 0;
  872. $endOffset = ($this->allDevices[$switchId]['cutend']) ? $this->allDevices[$switchId]['cutend'] : 0;
  873. //Optional data cutting on storing to archive
  874. if ($startOffset OR $endOffset) {
  875. $cutTmp = explodeRows($data);
  876. $cuttedData = '';
  877. if (!empty($cutTmp)) {
  878. $totalLines = sizeof($cutTmp);
  879. $lineCount = 0;
  880. foreach ($cutTmp as $lineIndex => $eachLineContent) {
  881. if ($lineCount >= $startOffset AND ( $lineCount < ($totalLines - $endOffset))) {
  882. $cuttedData .= $eachLineContent;
  883. }
  884. $lineCount++;
  885. }
  886. //now replacing initial data with cutted data
  887. $data = $cuttedData;
  888. }
  889. }
  890. $data = ubRouting::filters($data, 'mres');
  891. $this->archive->data('switchid', $switchId);
  892. $this->archive->data('date', $curdate);
  893. $this->archive->data('config', $data);
  894. $this->archive->create();
  895. log_register('ENVY STORE ARCHIVE SWITCHID [' . $switchId . ']');
  896. } else {
  897. $result .= __('Something went wrong') . ': EX_WRONGSWITCHID [' . $switchId . ']';
  898. }
  899. } else {
  900. $result .= __('Something went wrong') . ': EX_EMPTYSWITCHID';
  901. }
  902. return($result);
  903. }
  904. /**
  905. * Deletes existing archive record from daabase
  906. *
  907. * @param int $recordId
  908. *
  909. * @return void/string on error
  910. */
  911. public function deleteArchiveRecord($recordId) {
  912. $result = '';
  913. $recordId = ubRouting::filters($recordId, 'int');
  914. if (!empty($recordId)) {
  915. if (isset($this->allConfigs[$recordId])) {
  916. $recordData = $this->allConfigs[$recordId];
  917. $this->archive->where('id', '=', $recordId);
  918. $this->archive->delete();
  919. log_register('ENVY DELETE ARCHIVE RECORD [' . $recordId . '] SWITCHID [' . $recordData['switchid'] . '] DATE `' . $recordData['date'] . '`');
  920. } else {
  921. $result .= __('Something went wrong') . ': EX_WRONGRECORDID [' . $recordId . ']';
  922. }
  923. } else {
  924. $result .= __('Something went wrong') . ': EX_EMPTYRECORDID';
  925. }
  926. return($result);
  927. }
  928. /**
  929. * Returns device config saved in some archive record
  930. *
  931. * @param int $recordId
  932. *
  933. * @return string
  934. */
  935. public function renderArchiveRecordConfig($recordId) {
  936. $result = '';
  937. if (isset($this->allConfigs[$recordId])) {
  938. $this->archive->selectable('config');
  939. $this->archive->where('id', '=', $recordId);
  940. $rawConfig = $this->archive->getAll();
  941. $result = $rawConfig[0]['config'];
  942. }
  943. return($result);
  944. }
  945. /**
  946. * Downloads record file
  947. *
  948. * @param int $recordId
  949. *
  950. * @return void
  951. */
  952. public function downloadArchiveRecordConfig($recordId) {
  953. if (isset($this->allConfigs[$recordId])) {
  954. $recordData = $this->allConfigs[$recordId];
  955. $switchId = $recordData['switchid'];
  956. $switchIp = (isset($this->allSwitches[$switchId])) ? $this->allSwitches[$switchId]['ip'] : '';
  957. $this->archive->selectable('config');
  958. $this->archive->where('id', '=', $recordId);
  959. $rawConfig = $this->archive->getAll();
  960. $configContent = $rawConfig[0]['config'];
  961. $tmpFilePath = self::TMP_PATH . self::DL_PREFIX . $recordData['date'] . '_' . $switchId . '_' . $switchIp . '.txt';
  962. file_put_contents($tmpFilePath, $configContent);
  963. zb_DownloadFile($tmpFilePath);
  964. }
  965. }
  966. /**
  967. * Start process for get and store config data
  968. *
  969. * @param int $devId
  970. *
  971. * @return void
  972. */
  973. public function procStoreArchiveData($devId) {
  974. if (!$this->isProcessLocked($this->allSwitches[$devId]['ip'])) {
  975. //starting process
  976. $this->processStatsUpdate($this->allSwitches[$devId]['ip'], false);
  977. //polling device
  978. $this->storeArchiveData($devId, $this->runDeviceScript($devId));
  979. //finishing process
  980. $this->processStatsUpdate($this->allSwitches[$devId]['ip'], true);
  981. }
  982. }
  983. /**
  984. * Stores all available envy-devices configs into archive
  985. *
  986. * @return void
  987. */
  988. public function storeArchiveAllDevices() {
  989. if (!empty($this->allScripts)) {
  990. if (!empty($this->allDevices)) {
  991. if (!$this->isProcessLocked('ALL')) {
  992. //starting envy process
  993. $this->processStatsUpdate('ALL', false);
  994. foreach ($this->allDevices as $io => $each) {
  995. if ($each['active']) {
  996. if (@!$this->altCfg['MULTI_ENVY_PROC']) {
  997. $this->procStoreArchiveData($each['switchid']);
  998. } else {
  999. //starting herd of envy here!
  1000. $procTimeout = 0;
  1001. if ($this->altCfg['MULTI_ENVY_PROC'] > 1) {
  1002. $procTimeout = ubRouting::filters($this->altCfg['MULTI_ENVY_PROC'], 'int');
  1003. }
  1004. $this->stardust->runBackgroundProcess('/bin/ubapi "multienvy&devid=' . $each['switchid'] . '"', $procTimeout);
  1005. }
  1006. }
  1007. }
  1008. //finishing envy process
  1009. $this->processStatsUpdate('ALL', true);
  1010. }
  1011. }
  1012. }
  1013. }
  1014. /**
  1015. * Renders diff search form
  1016. *
  1017. * @return string
  1018. */
  1019. public function renderDiffForm() {
  1020. $result = '';
  1021. $devFilter = (ubRouting::checkGet('devfilter')) ? ubRouting::get('devfilter', 'int') : '';
  1022. if (!empty($this->allConfigs)) {
  1023. $confTmp = array();
  1024. foreach ($this->allConfigs as $io => $each) {
  1025. if (isset($this->allSwitches[$each['switchid']])) {
  1026. $switchData = $this->allSwitches[$each['switchid']];
  1027. $swichLabel = $switchData['ip'] . ' - ' . $switchData['location'];
  1028. } else {
  1029. $swichLabel = $each['switchid'] . ' - ' . __('Unknown');
  1030. }
  1031. if ($devFilter) {
  1032. if ($each['switchid'] == $devFilter) {
  1033. $confTmp[$each['id']] = $each['date'] . ' ' . $swichLabel;
  1034. }
  1035. } else {
  1036. $confTmp[$each['id']] = $each['date'] . ' ' . $swichLabel;
  1037. }
  1038. }
  1039. $currDiffOne = (ubRouting::checkPost('diffone')) ? ubRouting::post('diffone') : '';
  1040. $currDiffTwo = (ubRouting::checkPost('difftwo')) ? ubRouting::post('difftwo') : '';
  1041. $inputs = wf_HiddenInput('rundiff', 'true');
  1042. $inputs .= __('Compare') . ' ';
  1043. $inputs .= wf_Selector('diffone', $confTmp, '', $currDiffOne, false) . ' ';
  1044. $inputs .= __('and') . ' ';
  1045. $inputs .= wf_Selector('difftwo', $confTmp, '', $currDiffTwo, false) . ' ';
  1046. $inputs .= wf_Submit(__('Show'));
  1047. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  1048. } else {
  1049. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'info');
  1050. }
  1051. return($result);
  1052. }
  1053. /**
  1054. * Compares some two existing configs from archive and displays diff results
  1055. *
  1056. * @param int $configIdOne
  1057. * @param int $configIdTwo
  1058. *
  1059. * @return string
  1060. */
  1061. public function renderDiff($configIdOne, $configIdTwo) {
  1062. $result = '';
  1063. $configIdOne = ubRouting::filters($configIdOne, 'int');
  1064. $configIdTwo = ubRouting::filters($configIdTwo, 'int');
  1065. if (!empty($configIdOne) AND !empty($configIdTwo)) {
  1066. //same config check
  1067. if ($configIdOne != $configIdTwo) {
  1068. if (isset($this->allConfigs[$configIdOne]) AND isset($this->allConfigs[$configIdTwo])) {
  1069. //gettin both configs from database
  1070. $this->archive->selectable('config');
  1071. $this->archive->where('id', '=', $configIdOne);
  1072. $rawConfig = $this->archive->getAll();
  1073. $configOne = $rawConfig[0]['config'];
  1074. $this->archive->where('id', '=', $configIdTwo);
  1075. $rawConfig = $this->archive->getAll();
  1076. $configTwo = $rawConfig[0]['config'];
  1077. if ($configOne == $configTwo) {
  1078. $result .= $this->messages->getStyledMessage(__('No difference between this two configurations'), 'success');
  1079. } else {
  1080. $result .= $this->messages->getStyledMessage(__('Something is different in this two configurations'), 'warning');
  1081. }
  1082. $result .= wf_delimiter(0);
  1083. $result .= Diff::toTable(Diff::compare($configOne, $configTwo, false));
  1084. } else {
  1085. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': EX_NO_ARCHIVEID', 'error');
  1086. }
  1087. } else {
  1088. $result .= $this->messages->getStyledMessage(__('Same configs selected'), 'info');
  1089. }
  1090. }
  1091. return($result);
  1092. }
  1093. /**
  1094. * Deletes all records from archive except the last one for each of envy-devices
  1095. *
  1096. * @return void
  1097. */
  1098. public function cleanupArchive() {
  1099. $cleanupCounter = 0;
  1100. if (!empty($this->allConfigs)) {
  1101. foreach ($this->allConfigs as $recordId => $archiveData) {
  1102. $switchId = $archiveData['switchid'];
  1103. $lastConfigDate = $this->getLastConfigDate($switchId);
  1104. if ($archiveData['date'] != $lastConfigDate) {
  1105. $this->archive->where('id', '=', $recordId);
  1106. $this->archive->delete();
  1107. log_register('ENVY CLEANUP ARCHIVE RECORD [' . $recordId . '] SWITCHID [' . $switchId . '] DATE `' . $archiveData['date'] . '`');
  1108. $cleanupCounter++;
  1109. }
  1110. }
  1111. }
  1112. log_register('ENVVY ARCHIVE `' . $cleanupCounter . '` RECORDS CLEANED');
  1113. }
  1114. // __o__
  1115. // /\ | /\ ,__, \
  1116. // /__\|/__\o/o / / ,
  1117. // __(\ , , | `7 / \_ /)
  1118. // _.-' \\ _)\_/) | __||___, <----)_)---<< //
  1119. // ,-' _.---'\\ (/ (6\> |___// /_ /_\ ,_,_, / )\ //
  1120. // ,'_.--' \\ /` _ /\>/._\/\/__/\ | =/= / / / \_ //
  1121. // \\ / ,_//\ \>' , / ,/ / ) `0 / / /,__, \ //
  1122. // \\ \_('o | )> _)\_/) |\/ __\\_/ /o/o /- //
  1123. // , ,\\ `7 / / (/ (0\> , _/,/ /_'/ \j / o<\>>o
  1124. // _)\_/) \\,__\\_\' /` _ /\>_)\_/)|_|_/ __//___,____/_\
  1125. // (/ (9\> \\_) | / / ,_//\ (/ (6\> )_/_// /_ /__\_/_/
  1126. // /` _ /\> /\\\/_/ '\_(' | /` _ /\>/._\/\/__/
  1127. // / ,_//\ \>' \_)/ \_| _/// ,_//\ \> _)_/
  1128. // \_(' | )> x / _/ / _/ \\_('\|| )> x)_::\ ______,
  1129. // / \>_//( ( / /--.,/ +/ \>__// o /----.,/( )\\))
  1130. // \' \| ) \| / / / ' \' \| )___/ \ \/ \\\\\
  1131. // / +-/</\/ / / \_| / +-/o/----+ |
  1132. // / ' \ _/,/ _ / _/ / _// ' \_\, ___ /
  1133. // / \_| _// ( __,/( ( / / / \_| _/\_|-" /, /
  1134. // / _/ / _/ ^-' | | \| / / / _/ / _/ )\| | _/
  1135. // ( ( / / /_/ \_ \_\/ / ( ( / / /_/ \_ \_(__
  1136. // \| / / / /_/,/ \| / / / / /
  1137. // \/ / / // ( \/ / / / _/
  1138. // _/,/ _/_/,^-' _/,/ _/_/,/
  1139. // / ( /_/ ( / ( /_/ (
  1140. // ^-' ^-' ^-' ^-'
  1141. // Now I watched when the Lamb opened one of the seven seals, and I heard one of the four
  1142. // living creatures say with a voice like thunder, “Come!”
  1143. }