1<?php
2/**
3 * Class used internally by Diff to actually compute the diffs.
4 *
5 * This class uses the xdiff PECL package (http://pecl.php.net/package/xdiff)
6 * to compute the differences between the two input arrays.
7 *
8 * Copyright 2004-2017 Horde LLC (http://www.horde.org/)
9 *
10 * See the enclosed file COPYING for license information (LGPL). If you did
11 * not receive this file, see http://www.horde.org/licenses/lgpl21.
12 *
13 * @author  Jon Parise <jon@horde.org>
14 * @package Text_Diff
15 */
16
17// Disallow direct access to this file for security reasons
18if(!defined("IN_MYBB"))
19{
20	die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
21}
22
23class Horde_Text_Diff_Engine_Xdiff
24{
25    /**
26     */
27    public function diff($from_lines, $to_lines)
28    {
29        if (!extension_loaded('xdiff')) {
30            throw new Horde_Text_Diff_Exception('The xdiff extension is required for this diff engine');
31        }
32
33        array_walk($from_lines, array('Horde_Text_Diff', 'trimNewlines'));
34        array_walk($to_lines, array('Horde_Text_Diff', 'trimNewlines'));
35
36        /* Convert the two input arrays into strings for xdiff processing. */
37        $from_string = implode("\n", $from_lines);
38        $to_string = implode("\n", $to_lines);
39
40        /* Diff the two strings and convert the result to an array. */
41        $diff = xdiff_string_diff($from_string, $to_string, count($to_lines));
42        $diff = explode("\n", $diff);
43
44        /* Walk through the diff one line at a time.  We build the $edits
45         * array of diff operations by reading the first character of the
46         * xdiff output (which is in the "unified diff" format).
47         *
48         * Note that we don't have enough information to detect "changed"
49         * lines using this approach, so we can't add Horde_Text_Diff_Op_Changed
50         * instances to the $edits array.  The result is still perfectly
51         * valid, albeit a little less descriptive and efficient. */
52        $edits = array();
53        foreach ($diff as $line) {
54            if (!strlen($line)) {
55                continue;
56            }
57            switch ($line[0]) {
58            case ' ':
59                $edits[] = new Horde_Text_Diff_Op_Copy(array(substr($line, 1)));
60                break;
61
62            case '+':
63                $edits[] = new Horde_Text_Diff_Op_Add(array(substr($line, 1)));
64                break;
65
66            case '-':
67                $edits[] = new Horde_Text_Diff_Op_Delete(array(substr($line, 1)));
68                break;
69            }
70        }
71
72        return $edits;
73    }
74}
75