0 ]; if (is_executable($config['svn'])) { //$svnlogs = external_exec($config['svn'] . ' log -q -l 8 ' . $device_config_file); // Last 8 entries $svnlogs = external_exec($config['svn'] . ' log -q ' . $cmd_file); foreach (explode("\n", $svnlogs) as $line) { // r1884 | rancid | 2014-09-19 19:50:12 +0400 (Fri, 19 Sep 2014) // ------------------------------------------------------------------------ if (preg_match('/r(?\d+) \| .+? \| (?[\d\-]+ [\d:]+ [\+\-]?\d+)/', $line, $matches)) { $rev['list'][] = ['rev' => $matches['rev'], 'date' => format_timestamp(trim($matches['date']))]; $rev['count']++; } } if ($rev['count']) { $rev['type'] = 'svn'; } } if (!$rev['count'] && is_executable($config['git'])) { // You should know, I hate git! // Why git commands should be as: // git --git-dir='/rancid_path/git_dir/.git' --work-tree='/rancid_path/git_dir' log --pretty=format:"%h %ci" 'absolute_config_path' // git --git-dir='/rancid_path/git_dir/.git' --work-tree='/rancid_path/git_dir' show 96b30a1:'relative_git_file_path' // git --git-dir='/rancid_path/git_dir/.git' --work-tree='/rancid_path/git_dir' diff 96b30a1 441b0e6 'absolude_config_path' // and nothing else formats, else git return error $file_base = basename($device_config_file); $file_dir = dirname($device_config_file); if (is_dir($file_dir . '/.git')) { // Ok, do nothing $git_file = escapeshellarg($file_base); } elseif (is_dir($file_dir . '/../.git')) { // Rancid >= 3.2 $file_array = explode('/', $file_dir); $end = array_pop($file_array); $file_dir = implode('/', $file_array); $git_file = escapeshellarg($end . '/' . $file_base); } $cmd_dir = escapeshellarg($file_dir); $git_dir = escapeshellarg($file_dir . '/.git'); $gitlogs = external_exec($config['git'] . ' --git-dir=' . $git_dir . ' --work-tree=' . $cmd_dir . ' log --pretty=format:"%h %ci" ' . $cmd_file); foreach (explode("\n", $gitlogs) as $line) { // b6989b9 2014-11-10 00:16:53 +0100 // 66840ee 2014-11-02 23:34:18 +0100 if (preg_match('/(?\w+) (?[\d\-]+ [\d:]+ [\+\-]?\d+)/', $line, $matches)) { $rev['list'][] = ['rev' => $matches['rev'], 'date' => format_timestamp($matches['date'])]; $rev['count']++; } } if ($rev['count']) { $rev['type'] = 'git'; } } $navbar['options']['latest']['url'] = generate_url(['page' => 'device', 'device' => $device['device_id'], 'tab' => 'showconfig']); $navbar['options']['latest']['class'] = 'active'; if ($rev['count']) { $rev_active_index = 0; $rev_max = (int)$config['rancid_revisions']; if ($rev_max <= 0) { $rev_max = 1; } elseif ($rev_max > 32) { $rev_max = 32; } foreach ($rev['list'] as $i => $entry) { $rev_name = ($rev['type'] === 'svn' ? 'r' . $entry['rev'] : $entry['rev']); if ($i > ($rev_max - 1)) { break; // Show only last 10 revisions } elseif ($i > 0) { $navbar['options'][$rev_name]['text'] = '[' . $rev_name . ', ' . $entry['date'] . ']'; $navbar['options'][$rev_name]['url'] = generate_url(['page' => 'device', 'device' => $device['device_id'], 'tab' => 'showconfig', 'rev' => $entry['rev']]); if ($vars['rev'] == $entry['rev']) { unset($navbar['options']['latest']['class']); $navbar['options'][$rev_name]['class'] = 'active'; $rev_active_index = $i; } elseif ($rev['count'] > 4) { // Simplify too long revisions list $navbar['options'][$rev_name]['alt'] = $navbar['options'][$rev_name]['text']; $navbar['options'][$rev_name]['text'] = '[' . $rev_name . ']'; } } else { // Latest revision $navbar['options']['latest']['text'] = 'Latest [' . $rev_name . ', ' . $entry['date'] . ']'; } } } else { $navbar['options']['latest']['text'] = 'Latest'; } // Print out the navbar defined above print_navbar($navbar); unset($navbar); if ($rev['count']) { $rev['curr'] = $rev['list'][$rev_active_index]['rev']; if (isset($rev['list'][$rev_active_index + 1])) { $rev['prev'] = $rev['list'][$rev_active_index + 1]['rev']; } switch ($rev['type']) { case 'svn': if ($rev['count'] === 1) { // SVN not show initial revision in cat $cmd_cat = $config['svn'] . ' cat -rHEAD ' . $cmd_file; } else { $cmd_cat = $config['svn'] . ' cat -r' . $rev['curr'] . ' ' . $cmd_file; } $cmd_diff = $config['svn'] . ' diff -r' . $rev['prev'] . ':' . $rev['curr'] . ' ' . $cmd_file; $prev_name = 'r' . $rev['prev']; break; case 'git': $cmd_cat = $config['git'] . ' --git-dir=' . $git_dir . ' --work-tree=' . $cmd_dir . ' show ' . $rev['curr'] . ':' . $git_file; $cmd_diff = $config['git'] . ' --git-dir=' . $git_dir . ' --work-tree=' . $cmd_dir . ' diff ' . $rev['prev'] . ' ' . $rev['curr'] . ' ' . $cmd_file; $prev_name = $rev['prev']; } $device_config = external_exec($cmd_cat); if (!isset($rev['prev'])) { $diff = ''; if (empty($device_config)) { $device_config = '# Initial device added.'; } } else { $diff = external_exec($cmd_diff); if (!$diff) { $diff = 'No Difference'; } } } elseif (is_file($device_config_file)) { $device_config = file_get_contents($device_config_file); } else { $device_config = ''; } if ($config['rancid_ignorecomments']) { if (isset($config['os'][$device['os']]['comments'])) { $comments_pattern = $config['os'][$device['os']]['comments']; } else { // Default pattern $comments_pattern = $config['os_group']['default']['comments']; //$comments_pattern = '/^\s*#/'; } $lines = explode(PHP_EOL, $device_config); foreach ($lines as $i => $line) { if (@preg_match($comments_pattern, $line)) { unset($lines[$i]); } } $device_config = implode(PHP_EOL, $lines); } if ($rev['count']) { $text = ''; ?>
' . PHP_EOL . escape_html($diff) . '' . PHP_EOL); ?>

' . PHP_EOL . escape_html($device_config) . '' . PHP_EOL); ?>
' . PHP_EOL . escape_html($device_config) . '' . PHP_EOL; } $text .= '' . PHP_EOL; echo($text); // Clean unset($text, $device_config, $diff, $rev, $rev_active_index); // EOF