[TASK] Remove t3lib_superadmin
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_diff.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Contains class which has functions that generates a difference output of a content string
29 *
30 * Revised for TYPO3 3.6 November/2003 by Kasper Skårhøj
31 * XHTML Compliant
32 *
33 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
34 */
35
36
37 /**
38 * This class has functions which generates a difference output of a content string
39 *
40 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
41 * @package TYPO3
42 * @subpackage t3lib
43 */
44 class t3lib_diff {
45
46 // External, static:
47 var $stripTags = 0; // If set, the HTML tags are stripped from the input strings first.
48 var $diffOptions = ''; // Diff options. eg "--unified=3"
49
50 // Internal, dynamic:
51 var $clearBufferIdx = 0; // This indicates the number of times the function addClearBuffer has been called - and used to detect the very first call...
52 var $differenceLgd = 0;
53
54
55 /**
56 * This will produce a color-marked-up diff output in HTML from the input strings.
57 *
58 * @param string String 1
59 * @param string String 2
60 * @param string Setting the wrapping tag name
61 * @return string Formatted output.
62 */
63 function makeDiffDisplay($str1, $str2, $wrapTag = 'span') {
64 if ($this->stripTags) {
65 $str1 = strip_tags($str1);
66 $str2 = strip_tags($str2);
67 } else {
68 $str1 = $this->tagSpace($str1);
69 $str2 = $this->tagSpace($str2);
70 }
71 $str1Lines = $this->explodeStringIntoWords($str1);
72 $str2Lines = $this->explodeStringIntoWords($str2);
73
74 $diffRes = $this->getDiff(implode(LF, $str1Lines) . LF, implode(LF, $str2Lines) . LF);
75
76 if (is_array($diffRes)) {
77 $c = 0;
78 $diffResArray = array();
79 $differenceStr = '';
80 foreach ($diffRes as $lValue) {
81 if (intval($lValue)) {
82 $c = intval($lValue);
83 $diffResArray[$c]['changeInfo'] = $lValue;
84 }
85 if (substr($lValue, 0, 1) == '<') {
86 $differenceStr .= $diffResArray[$c]['old'][] = substr($lValue, 2);
87 }
88 if (substr($lValue, 0, 1) == '>') {
89 $differenceStr .= $diffResArray[$c]['new'][] = substr($lValue, 2);
90 }
91 }
92
93 $this->differenceLgd = strlen($differenceStr);
94
95 $outString = '';
96 $clearBuffer = '';
97 for ($a = -1; $a < count($str1Lines); $a++) {
98 if (is_array($diffResArray[$a + 1])) {
99 if (strstr($diffResArray[$a + 1]['changeInfo'], 'a')) { // a=Add, c=change, d=delete: If a, then the content is Added after the entry and we must insert the line content as well.
100 $clearBuffer .= htmlspecialchars($str1Lines[$a]) . ' ';
101 }
102
103 $outString .= $this->addClearBuffer($clearBuffer);
104 $clearBuffer = '';
105 if (is_array($diffResArray[$a + 1]['old'])) {
106 $outString .= '<' . $wrapTag . ' class="diff-r">' . htmlspecialchars(implode(' ', $diffResArray[$a + 1]['old'])) . '</' . $wrapTag . '> ';
107 }
108 if (is_array($diffResArray[$a + 1]['new'])) {
109 $outString .= '<' . $wrapTag . ' class="diff-g">' . htmlspecialchars(implode(' ', $diffResArray[$a + 1]['new'])) . '</' . $wrapTag . '> ';
110 }
111 $chInfParts = explode(',', $diffResArray[$a + 1]['changeInfo']);
112 if (!strcmp($chInfParts[0], $a + 1)) {
113 $newLine = intval($chInfParts[1]) - 1;
114 if ($newLine > $a) {
115 $a = $newLine;
116 } // Security that $a is not set lower than current for some reason...
117 }
118 } else {
119 $clearBuffer .= htmlspecialchars($str1Lines[$a]) . ' ';
120 }
121 }
122 $outString .= $this->addClearBuffer($clearBuffer, 1);
123
124 $outString = str_replace(' ', LF, $outString);
125 if (!$this->stripTags) {
126 $outString = $this->tagSpace($outString, 1);
127 }
128 return $outString;
129 }
130 }
131
132 /**
133 * Produce a diff (using the "diff" application) between two strings
134 * The function will write the two input strings to temporary files, then execute the diff program, delete the temp files and return the result.
135 *
136 * @param string String 1
137 * @param string String 2
138 * @return array The result from the exec() function call.
139 * @access private
140 */
141 function getDiff($str1, $str2) {
142 // Create file 1 and write string
143 $file1 = t3lib_div::tempnam('diff1_');
144 t3lib_div::writeFile($file1, $str1);
145 // Create file 2 and write string
146 $file2 = t3lib_div::tempnam('diff2_');
147 t3lib_div::writeFile($file2, $str2);
148 // Perform diff.
149 $cmd = $GLOBALS['TYPO3_CONF_VARS']['BE']['diff_path'] . ' ' . $this->diffOptions . ' ' . $file1 . ' ' . $file2;
150 $res = array();
151 t3lib_utility_Command::exec($cmd, $res);
152
153 unlink($file1);
154 unlink($file2);
155
156 return $res;
157 }
158
159 /**
160 * Will bring down the length of strings to < 150 chars if they were longer than 200 chars. This done by preserving the 70 first and last chars and concatenate those strings with "..." and a number indicating the string length
161 *
162 * @param string The input string.
163 * @param boolean If set, it indicates that the string should just end with ... (thus no "complete" ending)
164 * @return string Processed string.
165 * @access private
166 */
167 function addClearBuffer($clearBuffer, $last = 0) {
168 if (strlen($clearBuffer) > 200) {
169 $clearBuffer = ($this->clearBufferIdx ? t3lib_div::fixed_lgd_cs($clearBuffer, 70) : '') . '[' . strlen($clearBuffer) . ']' . (!$last ? t3lib_div::fixed_lgd_cs($clearBuffer, -70) : '');
170 }
171 $this->clearBufferIdx++;
172 return $clearBuffer;
173 }
174
175 /**
176 * Explodes the input string into words.
177 * This is done by splitting first by lines, then by space char. Each word will be in stored as a value in an array. Lines will be indicated by two subsequent empty values.
178 *
179 * @param string The string input
180 * @return array Array with words.
181 * @access private
182 */
183 function explodeStringIntoWords($str) {
184 $strArr = t3lib_div::trimExplode(LF, $str);
185 $outArray = array();
186 foreach ($strArr as $lineOfWords) {
187 $allWords = t3lib_div::trimExplode(' ', $lineOfWords, 1);
188 $outArray = array_merge($outArray, $allWords);
189 $outArray[] = '';
190 $outArray[] = '';
191 }
192 return $outArray;
193 }
194
195 /**
196 * Adds a space character before and after HTML tags (more precisely any found < or >)
197 *
198 * @param string String to process
199 * @param boolean If set, the < > searched for will be &lt; and &gt;
200 * @return string Processed string
201 * @access private
202 */
203 function tagSpace($str, $rev = 0) {
204 if ($rev) {
205 return str_replace(' &lt;', '&lt;', str_replace('&gt; ', '&gt;', $str));
206 } else {
207 return str_replace('<', ' <', str_replace('>', '> ', $str));
208 }
209 }
210 }
211
212 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_diff.php'])) {
213 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_diff.php']);
214 }
215 ?>