$value) { $v = '_' . $param; if (isset($this->$v)) { $this->$v = $value; } } } /** * Get any renderer parameters. * * @return array All parameters of this renderer object. */ function get_params() { $params = array(); foreach (get_object_vars($this) as $k => $v) { if ($k[0] == '_') { $params[substr($k, 1)] = $v; } } return $params; } /** * Renders a diff. * * @param diff &$diff A diff object. * * @return string The formatted output. */ function render(&$diff) { $xi = $yi = 1; $block = false; $context = array(); // Create a new diff object if it is a 3-way diff if (is_a($diff, 'diff3')) { $diff3 = &$diff; $diff_1 = $diff3->get_original(); $diff_2 = $diff3->merged_output(); unset($diff3); $diff = &new diff($diff_1, $diff_2); } $nlead = $this->_leading_context_lines; $ntrail = $this->_trailing_context_lines; $output = $this->_start_diff(); $diffs = $diff->get_diff(); foreach ($diffs as $i => $edit) { if (is_a($edit, 'diff_op_copy')) { if (is_array($block)) { $keep = ($i == sizeof($diffs) - 1) ? $ntrail : $nlead + $ntrail; if (sizeof($edit->orig) <= $keep) { $block[] = $edit; } else { if ($ntrail) { $context = array_slice($edit->orig, 0, $ntrail); $block[] = &new diff_op_copy($context); } $output .= $this->_block($x0, $ntrail + $xi - $x0, $y0, $ntrail + $yi - $y0, $block); $block = false; } } $context = $edit->orig; } else { if (!is_array($block)) { $context = array_slice($context, sizeof($context) - $nlead); $x0 = $xi - sizeof($context); $y0 = $yi - sizeof($context); $block = array(); if ($context) { $block[] = &new diff_op_copy($context); } } $block[] = $edit; } $xi += ($edit->orig) ? sizeof($edit->orig) : 0; $yi += ($edit->final) ? sizeof($edit->final) : 0; } if (is_array($block)) { $output .= $this->_block($x0, $xi - $x0, $y0, $yi - $y0, $block); } return $output . $this->_end_diff(); } function _block($xbeg, $xlen, $ybeg, $ylen, &$edits) { $output = $this->_start_block($this->_block_header($xbeg, $xlen, $ybeg, $ylen)); foreach ($edits as $edit) { switch (get_class($edit)) { case 'diff_op_copy': $output .= $this->_context($edit->orig); break; case 'diff_op_add': $output .= $this->_added($edit->final); break; case 'diff_op_delete': $output .= $this->_deleted($edit->orig); break; case 'diff_op_change': $output .= $this->_changed($edit->orig, $edit->final); break; } } return $output . $this->_end_block(); } function _start_diff() { return ''; } function _end_diff() { return ''; } function _block_header($xbeg, $xlen, $ybeg, $ylen) { if ($xlen > 1) { $xbeg .= ',' . ($xbeg + $xlen - 1); } if ($ylen > 1) { $ybeg .= ',' . ($ybeg + $ylen - 1); } return $xbeg . ($xlen ? ($ylen ? 'c' : 'd') : 'a') . $ybeg; } function _start_block($header) { return $header . "\n"; } function _end_block() { return ''; } function _lines($lines, $prefix = ' ') { return $prefix . implode("\n$prefix", $lines) . "\n"; } function _context($lines) { return $this->_lines($lines, ' '); } function _added($lines) { return $this->_lines($lines, '> '); } function _deleted($lines) { return $this->_lines($lines, '< '); } function _changed($orig, $final) { return $this->_deleted($orig) . "---\n" . $this->_added($final); } /** * Our function to get the diff */ function get_diff_content($diff) { return $this->render($diff); } } /** * Renders a unified diff * @package diff */ class diff_renderer_unified extends diff_renderer { var $_leading_context_lines = 4; var $_trailing_context_lines = 4; /** * Our function to get the diff */ function get_diff_content($diff) { return nl2br($this->render($diff)); } function _block_header($xbeg, $xlen, $ybeg, $ylen) { if ($xlen != 1) { $xbeg .= ',' . $xlen; } if ($ylen != 1) { $ybeg .= ',' . $ylen; } return '
@@ -' . $xbeg . ' +' . $ybeg . ' @@
'; } function _context($lines) { return '
' . htmlspecialchars($this->_lines($lines, ' ')) . '
'; } function _added($lines) { return '
' . htmlspecialchars($this->_lines($lines, '+')) . '
'; } function _deleted($lines) { return '
' . htmlspecialchars($this->_lines($lines, '-')) . '
'; } function _changed($orig, $final) { return $this->_deleted($orig) . $this->_added($final); } function _start_diff() { $start = '
'; return $start; } function _end_diff() { return '
'; } function _end_block() { return ''; } } /** * "Inline" diff renderer. * * This class renders diffs in the Wiki-style "inline" format. * * @author Ciprian Popovici * @package diff */ class diff_renderer_inline extends diff_renderer { var $_leading_context_lines = 10000; var $_trailing_context_lines = 10000; // Prefix and suffix for inserted text var $_ins_prefix = ''; var $_ins_suffix = ''; // Prefix and suffix for deleted text var $_del_prefix = ''; var $_del_suffix = ''; var $_block_head = ''; // What are we currently splitting on? Used to recurse to show word-level var $_split_level = 'lines'; /** * Our function to get the diff */ function get_diff_content($diff) { return '
' . nl2br($this->render($diff)) . '
'; } function _start_diff() { return ''; } function _end_diff() { return ''; } function _block_header($xbeg, $xlen, $ybeg, $ylen) { return $this->_block_head; } function _start_block($header) { return $header; } function _lines($lines, $prefix = ' ', $encode = true) { if ($encode) { array_walk($lines, array(&$this, '_encode')); } if ($this->_split_level == 'words') { return implode('', $lines); } else { return implode("\n", $lines) . "\n"; } } function _added($lines) { array_walk($lines, array(&$this, '_encode')); $lines[0] = $this->_ins_prefix . $lines[0]; $lines[sizeof($lines) - 1] .= $this->_ins_suffix; return $this->_lines($lines, ' ', false); } function _deleted($lines, $words = false) { array_walk($lines, array(&$this, '_encode')); $lines[0] = $this->_del_prefix . $lines[0]; $lines[sizeof($lines) - 1] .= $this->_del_suffix; return $this->_lines($lines, ' ', false); } function _changed($orig, $final) { // If we've already split on words, don't try to do so again - just display. if ($this->_split_level == 'words') { $prefix = ''; while ($orig[0] !== false && $final[0] !== false && substr($orig[0], 0, 1) == ' ' && substr($final[0], 0, 1) == ' ') { $prefix .= substr($orig[0], 0, 1); $orig[0] = substr($orig[0], 1); $final[0] = substr($final[0], 1); } return $prefix . $this->_deleted($orig) . $this->_added($final); } $text1 = implode("\n", $orig); $text2 = implode("\n", $final); // Non-printing newline marker. $nl = "\0"; // We want to split on word boundaries, but we need to preserve whitespace as well. // Therefore we split on words, but include all blocks of whitespace in the wordlist. $splitted_text_1 = $this->_split_on_words($text1, $nl); $splitted_text_2 = $this->_split_on_words($text2, $nl); $diff = &new diff($splitted_text_1, $splitted_text_2); unset($splitted_text_1, $splitted_text_2); // Get the diff in inline format. $renderer = &new diff_renderer_inline(array_merge($this->get_params(), array('split_level' => 'words'))); // Run the diff and get the output. return str_replace($nl, "\n", $renderer->render($diff)) . "\n"; } function _split_on_words($string, $newline_escape = "\n") { // Ignore \0; otherwise the while loop will never finish. $string = str_replace("\0", '', $string); $words = array(); $length = strlen($string); $pos = 0; $tab_there = true; while ($pos < $length) { // Check for tabs... do not include them if ($tab_there && substr($string, $pos, 1) === "\t") { $words[] = "\t"; $pos++; continue; } else { $tab_there = false; } // Eat a word with any preceding whitespace. $spaces = strspn(substr($string, $pos), " \n"); $nextpos = strcspn(substr($string, $pos + $spaces), " \n"); $words[] = str_replace("\n", $newline_escape, substr($string, $pos, $spaces + $nextpos)); $pos += $spaces + $nextpos; } return $words; } function _encode(&$string) { $string = htmlspecialchars($string); } } /** * "raw" diff renderer. * This class could be used to output a raw unified patch file * * @package diff */ class diff_renderer_raw extends diff_renderer { var $_leading_context_lines = 4; var $_trailing_context_lines = 4; /** * Our function to get the diff */ function get_diff_content($diff) { return ''; } function _block_header($xbeg, $xlen, $ybeg, $ylen) { if ($xlen != 1) { $xbeg .= ',' . $xlen; } if ($ylen != 1) { $ybeg .= ',' . $ylen; } return '@@ -' . $xbeg . ' +' . $ybeg . ' @@'; } function _context($lines) { return $this->_lines($lines, ' '); } function _added($lines) { return $this->_lines($lines, '+'); } function _deleted($lines) { return $this->_lines($lines, '-'); } function _changed($orig, $final) { return $this->_deleted($orig) . $this->_added($final); } } /** * "chora (Horde)" diff renderer - similar style. * This renderer class is a modified human_readable function from the Horde Framework. * * @package diff */ class diff_renderer_side_by_side extends diff_renderer { var $_leading_context_lines = 3; var $_trailing_context_lines = 3; var $lines = array(); // Hold the left and right columns of lines for change blocks. var $cols; var $state; var $data = false; /** * Our function to get the diff */ function get_diff_content($diff) { global $user; $output = ''; $output .= ' '; $this->render($diff); // Is the diff empty? if (!sizeof($this->lines)) { $output .= ''; } else { // Iterate through every header block of changes foreach ($this->lines as $header) { $output .= ''; // Each header block consists of a number of changes (add, remove, change). $current_context = ''; foreach ($header['contents'] as $change) { if (!empty($current_context) && $change['type'] != 'empty') { $line = $current_context; $current_context = ''; $output .= ''; } switch ($change['type']) { case 'add': $line = ''; foreach ($change['lines'] as $_line) { $line .= htmlspecialchars($_line) . '
'; } $output .= ''; break; case 'remove': $line = ''; foreach ($change['lines'] as $_line) { $line .= htmlspecialchars($_line) . '
'; } $output .= ''; break; case 'empty': $current_context .= htmlspecialchars($change['line']) . '
'; break; case 'change': // Pop the old/new stacks one by one, until both are empty. $oldsize = sizeof($change['old']); $newsize = sizeof($change['new']); $left = $right = ''; for ($row = 0, $row_max = max($oldsize, $newsize); $row < $row_max; ++$row) { $left .= isset($change['old'][$row]) ? htmlspecialchars($change['old'][$row]) : ''; $left .= '
'; $right .= isset($change['new'][$row]) ? htmlspecialchars($change['new'][$row]) : ''; $right .= '
'; } $output .= ''; if (!empty($left)) { $output .= ''; } else if ($row < $oldsize) { $output .= ''; } else { $output .= ''; } if (!empty($right)) { $output .= ''; } else if ($row < $newsize) { $output .= ''; } else { $output .= ''; } $output .= ''; break; } } if (!empty($current_context)) { $line = $current_context; $current_context = ''; $output .= ''; $output .= ''; } } } $output .= '
  ' . $user->lang['LINE_UNMODIFIED'] . '   ' . $user->lang['LINE_ADDED'] . '   ' . $user->lang['LINE_MODIFIED'] . '   ' . $user->lang['LINE_REMOVED'] . '
' . $user->lang['NO_VISIBLE_CHANGES'] . '
Line ' . $header['oldline'] . '' . $user->lang['LINE'] . ' ' . $header['newline'] . '
' . ((strlen($line)) ? $line : ' ') . '
' . ((strlen($line)) ? $line : ' ') . '
 
' . ((strlen($line)) ? $line : ' ') . '
' . ((strlen($line)) ? $line : ' ') . '
 
' . $left . '
  
' . $right . '
  
' . ((strlen($line)) ? $line : ' ') . '
' . ((strlen($line)) ? $line : ' ') . '
'; return $output; } function _start_diff() { $this->lines = array(); $this->data = false; $this->cols = array(array(), array()); $this->state = 'empty'; return ''; } function _end_diff() { // Just flush any remaining entries in the columns stack. switch ($this->state) { case 'add': $this->data['contents'][] = array('type' => 'add', 'lines' => $this->cols[0]); break; case 'remove': // We have some removal lines pending in our stack, so flush them. $this->data['contents'][] = array('type' => 'remove', 'lines' => $this->cols[0]); break; case 'change': // We have both remove and addition lines, so this is a change block. $this->data['contents'][] = array('type' => 'change', 'old' => $this->cols[0], 'new' => $this->cols[1]); break; } if ($this->data !== false) { $this->lines[] = $this->data; } return ''; } function _block_header($xbeg, $xlen, $ybeg, $ylen) { // Push any previous header information to the return stack. if ($this->data !== false) { $this->lines[] = $this->data; } $this->data = array('type' => 'header', 'oldline' => $xbeg, 'newline' => $ybeg, 'contents' => array()); $this->state = 'dump'; } function _added($lines) { array_walk($lines, array(&$this, '_perform_add')); } function _perform_add($line) { if ($this->state == 'empty') { return ''; } // This is just an addition line. if ($this->state == 'dump' || $this->state == 'add') { // Start adding to the addition stack. $this->cols[0][] = $line; $this->state = 'add'; } else { // This is inside a change block, so start accumulating lines. $this->state = 'change'; $this->cols[1][] = $line; } } function _deleted($lines) { array_walk($lines, array(&$this, '_perform_delete')); } function _perform_delete($line) { // This is a removal line. $this->state = 'remove'; $this->cols[0][] = $line; } function _context($lines) { array_walk($lines, array(&$this, '_perform_context')); } function _perform_context($line) { // An empty block with no action. switch ($this->state) { case 'add': $this->data['contents'][] = array('type' => 'add', 'lines' => $this->cols[0]); break; case 'remove': // We have some removal lines pending in our stack, so flush them. $this->data['contents'][] = array('type' => 'remove', 'lines' => $this->cols[0]); break; case 'change': // We have both remove and addition lines, so this is a change block. $this->data['contents'][] = array('type' => 'change', 'old' => $this->cols[0], 'new' => $this->cols[1]); break; } $this->cols = array(array(), array()); $this->data['contents'][] = array('type' => 'empty', 'line' => $line); $this->state = 'dump'; } function _changed($orig, $final) { return $this->_deleted($orig) . $this->_added($final); } } ?>/span> before_leaving { eval { modules::unload("parport_probe") } }; detect_devices::whatPrinter(); } sub net_detect { whatNetPrinter(1, 0) } sub net_smb_detect { whatNetPrinter(0, 1) } sub detect { local_detect(), whatNetPrinter(1, 1); } sub whatNetPrinter { my ($network, $smb) = @_; my (@res); # Which ports should be scanned? my @portstoscan; push @portstoscan, "139" if $smb; push @portstoscan, "4010", "4020", "4030", "5503", "9100-9104" if $network; return () if $#portstoscan < 0; my $portlist = join ",", @portstoscan; # Which hosts should be scanned? # (Applying nmap to a whole network is very time-consuming, because nmap # waits for a certain timeout period on non-existing hosts, so we get a # lists of existing hosts by pinging the broadcast addresses for existing # hosts and then scanning only them, which is much faster) my @hostips = getIPsInLocalNetworks(); return () if $#hostips < 0; my $hostlist = join " ", @hostips; # Scan network for printers, the timeout settings are there to avoid # delays caused by machines blocking their ports with a firewall local *F; open F, ($::testing ? "" : "chroot $::prefix/ ") . "/bin/sh -c \"export LC_ALL=C; nmap -r -P0 --host_timeout 400 --initial_rtt_timeout 200 -p $portlist $hostlist\" |" or return @res; my ($host, $ip, $port, $modelinfo) = ("", "", "", ""); while (my $line = <F>) { chomp $line; # head line of the report of a host with the ports in question open #if ($line =~ m/^\s*Interesting\s+ports\s+on\s+(\S*)\s*\(([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\)\s*:\s*$/i) { if ($line =~ m/^\s*Interesting\s+ports\s+on\s+(\S*)\s*\((\S+)\)\s*:\s*$/i) { ($host, $ip) = ($1, $2); $host = $ip if $host eq ""; $port = ""; undef $modelinfo; } elsif ($line =~ m/^\s*(\d+)\/\S+\s+open\s+/i) { next if $ip eq ""; $port = $1; # Now we have all info for one printer # Store this auto-detection result in the data structure # Determine the protocol by the port number # SMB/Windows if ($port eq "139") { my @shares = getSMBPrinterShares($ip); foreach my $share (@shares) { push @res, { port => "smb://$host/$share->{name}", val => { CLASS => 'PRINTER', MODEL => N("Unknown Model"), MANUFACTURER => "", DESCRIPTION => $share->{description}, SERIALNUMBER => "" } }; } } else { if (!defined($modelinfo)) { # SNMP request to auto-detect model $modelinfo = getSNMPModel($ip); } if (defined($modelinfo)) { push @res, { port => "socket://$host:$port", val => $modelinfo }; } } } } close F; @res; } sub getIPsInLocalNetworks { # subroutine determines the list of all hosts reachable in the local # networks by means of pinging the broadcast addresses. # Return an empty list if no network is running return () unless network_running(); # Read the output of "ifconfig" to determine the broadcast addresses of # the local networks my $dev_is_localnet = 0; my @local_bcasts; my $current_bcast = ""; local *IFCONFIG_OUT; open IFCONFIG_OUT, ($::testing ? "" : "chroot $::prefix/ ") . "/bin/sh -c \"export LC_ALL=C; ifconfig\" |" or return (); while (my $readline = <IFCONFIG_OUT>) { # New entry ... if ($readline =~ /^(\S+)\s/) { my $dev = $1; # ... for a local network (eth = ethernet, # vmnet = VMWare, # ethernet card connected to ISP excluded)? $dev_is_localnet = $dev =~ /^eth/ || $dev =~ /^vmnet/; # delete previous address $current_bcast = ""; } # Are we in the important line now? if ($readline =~ /\sBcast:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\s/) { # Rip out the broadcast IP address $current_bcast = $1; # Are we in an entry for a local network? if ($dev_is_localnet == 1) { # Store current IP address push @local_bcasts, $current_bcast; } } } close(IFCONFIG_OUT); my @addresses; # Now ping all broadcast addresses and additionally "nmblookup" the # networks (to find Windows servers which do not answer to ping) foreach my $bcast (@local_bcasts) { local *F; open F, ($::testing ? "" : "chroot $::prefix/ ") . "/bin/sh -c \"export LC_ALL=C; ping -w 1 -b -n $bcast | cut -f 4 -d ' ' | sed s/:// | egrep '^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+' | uniq | sort\" |" or next; local $_; while (<F>) { chomp; push @addresses, $_ } close F; if (-x "/usr/bin/nmblookup") { local *F; open F, ($::testing ? "" : "chroot $::prefix/ ") . "/bin/sh -c \"export LC_ALL=C; nmblookup -B $bcast \\* | cut -f 1 -d ' ' | egrep '^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+' | uniq | sort\" |" or next; local $_; while (<F>) { chomp; push @addresses, $_ if !(member($_,@addresses)); } } } @addresses; } sub getSMBPrinterShares { my ($host) = @_; # SMB request to auto-detect shares local *F; open F, ($::testing ? "" : "chroot $::prefix/ ") . "/bin/sh -c \"export LC_ALL=C; smbclient -N -L $host\" |" or return (); my $insharelist = 0; my @shares; while (my $l = <F>) { chomp $l; if ($l =~ /^\s*Sharename\s+Type\s+Comment\s*$/i) { $insharelist = 1; } elsif ($l =~ /^\s*Server\s+Comment\s*$/i) { $insharelist = 0; } elsif ($l =~ /^\s*(\S+)\s+Printer\s*(.*)$/i && $insharelist) { my $name = $1; my $description = $2; $description =~ s/^(\s*)//; push @shares, { name => $name, description => $description }; } } close F; return @shares; } sub getSNMPModel { my ($host) = @_; my $manufacturer = ""; my $model = ""; my $description = ""; my $serialnumber = ""; # SNMP request to auto-detect model local *F; open F, ($::testing ? $::prefix : "chroot $::prefix/ ") . "/bin/sh -c \"scli -1 -c 'show printer info' $host\" |" or return { CLASS => 'PRINTER', MODEL => N("Unknown Model"), MANUFACTURER => "", DESCRIPTION => "", SERIALNUMBER => "" }; while (my $l = <F>) { chomp $l; if ($l =~ /^\s*Manufacturer:\s*(\S.*)$/i && $l =~ /^\s*Vendor:\s*(\S.*)$/i) { $manufacturer = $1; $manufacturer =~ s/Hewlett[-\s_]Packard/HP/; $manufacturer =~ s/HEWLETT[-\s_]PACKARD/HP/; } elsif ($l =~ /^\s*Model:\s*(\S.*)$/i) { $model = $1; } elsif ($l =~ /^\s*Description:\s*(\S.*)$/i) { $description = $1; $description =~ s/Hewlett[-\s_]Packard/HP/; $description =~ s/HEWLETT[-\s_]PACKARD/HP/; } elsif ($l =~ /^\s*Serial\s*Number:\s*(\S.*)$/i) { $serialnumber = $1; } } close F; # Was there a manufacturer and a model in the output? # If not, get them from the description if ($manufacturer eq "" || $model eq "") { if ($description =~ /^\s*(\S*)\s+(\S.*)$/) { $manufacturer = $1 if $manufacturer eq ""; $model = $2 if $model eq ""; } # No description field? Make one out of manufacturer and model. } elsif ($description eq "") { $description = "$manufacturer $model"; } # We couldn't determine a model $model = N("Unknown Model") if $model eq ""; # Remove trailing spaces $manufacturer =~ s/(\S+)\s+$/$1/; $model =~ s/(\S+)\s+$/$1/; $description =~ s/(\S+)\s+$/$1/; $serialnumber =~ s/(\S+)\s+$/$1/; # Now we have all info for one printer # Store this auto-detection result in the data structure return { CLASS => 'PRINTER', MODEL => $model, MANUFACTURER => $manufacturer, DESCRIPTION => $description, SERIALNUMBER => $serialnumber }; } sub network_running { # If the network is not running return 0, otherwise 1. local *F; open F, ($::testing ? $::prefix : "chroot $::prefix/ ") . "/bin/sh -c \"export LC_ALL=C; /sbin/ifconfig\" |" or die "Could not run \"ifconfig\"!"; while (my $line = <F>) { if ($line !~ /^lo\s+/ && # The loopback device can have been # started by the spooler's startup script $line =~ /^(\S+)\s+/) { # In this line starts an entry for a # running network close F; return 1; } } close F; return 0; } sub parport_addr { # auto-detect the parallel port addresses my ($device) = @_; $device =~ m!^/dev/lp(\d+)$! or $device =~ m!^/dev/printers/(\d+)$!; my $portnumber = $1; my $parport_addresses =