Horde_Text_Diff_Renderer::render PHP Method

render() public method

Renders a diff.
public render ( Horde_Text_Diff $diff ) : string
$diff Horde_Text_Diff A Horde_Text_Diff object.
return string The formatted output.
    public function render($diff)
    {
        $xi = $yi = 1;
        $block = false;
        $context = array();
        $nlead = $this->_leading_context_lines;
        $ntrail = $this->_trailing_context_lines;
        $output = $this->_startDiff();
        $diffs = $diff->getDiff();
        foreach ($diffs as $i => $edit) {
            /* If these are unchanged (copied) lines, and we want to keep
             * leading or trailing context lines, extract them from the copy
             * block. */
            if ($edit instanceof Horde_Text_Diff_Op_Copy) {
                /* Do we have any diff blocks yet? */
                if (is_array($block)) {
                    /* How many lines to keep as context from the copy
                     * block. */
                    $keep = $i == count($diffs) - 1 ? $ntrail : $nlead + $ntrail;
                    if (count($edit->orig) <= $keep) {
                        /* We have less lines in the block than we want for
                         * context => keep the whole block. */
                        $block[] = $edit;
                    } else {
                        if ($ntrail) {
                            /* Create a new block with as many lines as we need
                             * for the trailing context. */
                            $context = array_slice($edit->orig, 0, $ntrail);
                            $block[] = new Horde_Text_Diff_Op_Copy($context);
                        }
                        /* @todo */
                        $output .= $this->_block($x0, $ntrail + $xi - $x0, $y0, $ntrail + $yi - $y0, $block);
                        $block = false;
                    }
                }
                /* Keep the copy block as the context for the next block. */
                $context = $edit->orig;
            } else {
                /* Don't we have any diff blocks yet? */
                if (!is_array($block)) {
                    /* Extract context lines from the preceding copy block. */
                    $context = array_slice($context, count($context) - $nlead);
                    $x0 = $xi - count($context);
                    $y0 = $yi - count($context);
                    $block = array();
                    if ($context) {
                        $block[] = new Horde_Text_Diff_Op_Copy($context);
                    }
                }
                $block[] = $edit;
            }
            if ($edit->orig) {
                $xi += count($edit->orig);
            }
            if ($edit->final) {
                $yi += count($edit->final);
            }
        }
        if (is_array($block)) {
            $output .= $this->_block($x0, $xi - $x0, $y0, $yi - $y0, $block);
        }
        return $output . $this->_endDiff();
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Adds a log entry to the #__admintools_scanalerts table, marking a modified, added or suspicious file.
  *
  * @param   \stdClass       $newFileRecord  The record of the current version of the file
  * @param   \stdClass|null  $oldFileRecord  The record of the old version of the file (or null if it's an added file)
  *
  * @return  void
  */
 private function _logFileChange(&$newFileRecord, &$oldFileRecord = null)
 {
     // Initialise the new alert record
     $alertRecord = array('path' => $newFileRecord->path, 'scan_id' => \Akeeba\Engine\Factory::getStatistics()->getId(), 'diff' => '', 'threat_score' => 0, 'acknowledged' => 0);
     // Produce the diff if there is an old file
     if (!is_null($oldFileRecord)) {
         if ($this->generateDiff) {
             // Modified file, generate diff
             $newText = gzinflate($newFileRecord->data);
             $newText = str_replace("\r\n", "\n", $newText);
             $newText = str_replace("\r", "\n", $newText);
             $newLines = explode("\n", $newText);
             unset($newText);
             $oldText = gzinflate($oldFileRecord->data);
             $oldText = str_replace("\r\n", "\n", $oldText);
             $oldText = str_replace("\r", "\n", $oldText);
             $oldLines = explode("\n", $oldText);
             unset($oldText);
             $diffObject = new \Horde_Text_Diff('native', array($newLines, $oldLines));
             $renderer = new \Horde_Text_Diff_Renderer();
             $alertRecord['diff'] = $renderer->render($diffObject);
             unset($renderer);
             unset($diffObject);
             unset($newLines);
             unset($oldLines);
             $alertRecord['threat_score'] = $this->_getThreatScore($alertRecord['diff']);
         } else {
             // Modified file, do not generate diff
             $alertRecord['diff'] = "###MODIFIED FILE###\n";
             $newText = @file_get_contents($newFileRecord->sourcePath);
             $alertRecord['threat_score'] = $this->_getThreatScore($newText);
             unset($newText);
         }
     } else {
         // New file
         $newText = @file_get_contents($newFileRecord->sourcePath);
         $alertRecord['threat_score'] = $this->_getThreatScore($newText);
         unset($newText);
     }
     // Do not create a record for non-threat files
     if ($this->ignoreNonThreats && !$alertRecord['threat_score']) {
         return;
     }
     $alertRecord = (object) $alertRecord;
     $db = \JFactory::getDbo();
     $db->insertObject('#__admintools_scanalerts', $alertRecord);
     unset($alertRecord);
 }
All Usage Examples Of Horde_Text_Diff_Renderer::render