Fixed bug #17184: Disable the CSRF protection in ExtDirect calls coming from the...
[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 * $Id$
31 * Revised for TYPO3 3.6 November/2003 by Kasper Skårhøj
32 * XHTML Compliant
33 *
34 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
35 */
36 /**
37 * [CLASS/FUNCTION INDEX of SCRIPT]
38 *
39 *
40 *
41 * 66: class t3lib_diff
42 * 86: function makeDiffDisplay($str1,$str2,$wrapTag='span')
43 * 163: function getDiff($str1,$str2)
44 * 189: function addClearBuffer($clearBuffer,$last=0)
45 * 205: function explodeStringIntoWords($str)
46 * 226: function tagSpace($str,$rev=0)
47 *
48 * TOTAL FUNCTIONS: 5
49 * (This index is automatically created/updated by the extension "extdeveval")
50 *
51 */
52
53
54 /**
55 * This class has functions which generates a difference output of a content string
56 *
57 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
58 * @package TYPO3
59 * @subpackage t3lib
60 */
61 class t3lib_diff {
62
63 // External, static:
64 var $stripTags = 0; // If set, the HTML tags are stripped from the input strings first.
65 var $diffOptions = ''; // Diff options. eg "--unified=3"
66
67 // Internal, dynamic:
68 var $clearBufferIdx = 0; // This indicates the number of times the function addClearBuffer has been called - and used to detect the very first call...
69 var $differenceLgd = 0;
70
71
72 /**
73 * This will produce a color-marked-up diff output in HTML from the input strings.
74 *
75 * @param string String 1
76 * @param string String 2
77 * @param string Setting the wrapping tag name
78 * @return string Formatted output.
79 */
80 function makeDiffDisplay($str1, $str2, $wrapTag = 'span') {
81 if ($this->stripTags) {
82 $str1 = strip_tags($str1);
83 $str2 = strip_tags($str2);
84 } else {
85 $str1 = $this->tagSpace($str1);
86 $str2 = $this->tagSpace($str2);
87 }
88 $str1Lines = $this->explodeStringIntoWords($str1);
89 $str2Lines = $this->explodeStringIntoWords($str2);
90
91 $diffRes = $this->getDiff(implode(LF, $str1Lines) . LF, implode(LF, $str2Lines) . LF);
92
93 if (is_array($diffRes)) {
94 $c = 0;
95 $diffResArray = array();
96 $differenceStr = '';
97 foreach ($diffRes as $lValue) {
98 if (intval($lValue)) {
99 $c = intval($lValue);
100 $diffResArray[$c]['changeInfo'] = $lValue;
101 }
102 if (substr($lValue, 0, 1) == '<') {
103 $differenceStr .= $diffResArray[$c]['old'][] = substr($lValue, 2);
104 }
105 if (substr($lValue, 0, 1) == '>') {
106 $differenceStr .= $diffResArray[$c]['new'][] = substr($lValue, 2);
107 }
108 }
109
110 $this->differenceLgd = strlen($differenceStr);
111
112 $outString = '';
113 $clearBuffer = '';
114 for ($a = -1; $a < count($str1Lines); $a++) {
115 if (is_array($diffResArray[$a + 1])) {
116 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.
117 $clearBuffer .= htmlspecialchars($str1Lines[$a]) . ' ';
118 }
119
120 $outString .= $this->addClearBuffer($clearBuffer);
121 $clearBuffer = '';
122 if (is_array($diffResArray[$a + 1]['old'])) {
123 $outString .= '<' . $wrapTag . ' class="diff-r">' . htmlspecialchars(implode(' ', $diffResArray[$a + 1]['old'])) . '</' . $wrapTag . '> ';
124 }
125 if (is_array($diffResArray[$a + 1]['new'])) {
126 $outString .= '<' . $wrapTag . ' class="diff-g">' . htmlspecialchars(implode(' ', $diffResArray[$a + 1]['new'])) . '</' . $wrapTag . '> ';
127 }
128 $chInfParts = explode(',', $diffResArray[$a + 1]['changeInfo']);
129 if (!strcmp($chInfParts[0], $a + 1)) {
130 $newLine = intval($chInfParts[1]) - 1;
131 if ($newLine > $a) {
132 $a = $newLine;
133 } // Security that $a is not set lower than current for some reason...
134 }
135 } else {
136 $clearBuffer .= htmlspecialchars($str1Lines[$a]) . ' ';
137 }
138 }
139 $outString .= $this->addClearBuffer($clearBuffer, 1);
140
141 $outString = str_replace(' ', LF, $outString);
142 if (!$this->stripTags) {
143 $outString = $this->tagSpace($outString, 1);
144 }
145 return $outString;
146 }
147 }
148
149 /**
150 * Produce a diff (using the "diff" application) between two strings
151 * The function will write the two input strings to temporary files, then execute the diff program, delete the temp files and return the result.
152 *
153 * @param string String 1
154 * @param string String 2
155 * @return array The result from the exec() function call.
156 * @access private
157 */
158 function getDiff($str1, $str2) {
159 // Create file 1 and write string
160 $file1 = t3lib_div::tempnam('diff1_');
161 t3lib_div::writeFile($file1, $str1);
162 // Create file 2 and write string
163 $file2 = t3lib_div::tempnam('diff2_');
164 t3lib_div::writeFile($file2, $str2);
165 // Perform diff.
166 $cmd = $GLOBALS['TYPO3_CONF_VARS']['BE']['diff_path'] . ' ' . $this->diffOptions . ' ' . $file1 . ' ' . $file2;
167 $res = array();
168 t3lib_utility_Command::exec($cmd, $res);
169
170 unlink($file1);
171 unlink($file2);
172
173 return $res;
174 }
175
176 /**
177 * 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
178 *
179 * @param string The input string.
180 * @param boolean If set, it indicates that the string should just end with ... (thus no "complete" ending)
181 * @return string Processed string.
182 * @access private
183 */
184 function addClearBuffer($clearBuffer, $last = 0) {
185 if (strlen($clearBuffer) > 200) {
186 $clearBuffer = ($this->clearBufferIdx ? t3lib_div::fixed_lgd_cs($clearBuffer, 70) : '') . '[' . strlen($clearBuffer) . ']' . (!$last ? t3lib_div::fixed_lgd_cs($clearBuffer, -70) : '');
187 }
188 $this->clearBufferIdx++;
189 return $clearBuffer;
190 }
191
192 /**
193 * Explodes the input string into words.
194 * 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.
195 *
196 * @param string The string input
197 * @return array Array with words.
198 * @access private
199 */
200 function explodeStringIntoWords($str) {
201 $strArr = t3lib_div::trimExplode(LF, $str);
202 $outArray = array();
203 foreach ($strArr as $lineOfWords) {
204 $allWords = t3lib_div::trimExplode(' ', $lineOfWords, 1);
205 $outArray = array_merge($outArray, $allWords);
206 $outArray[] = '';
207 $outArray[] = '';
208 }
209 return $outArray;
210 }
211
212 /**
213 * Adds a space character before and after HTML tags (more precisely any found < or >)
214 *
215 * @param string String to process
216 * @param boolean If set, the < > searched for will be &lt; and &gt;
217 * @return string Processed string
218 * @access private
219 */
220 function tagSpace($str, $rev = 0) {
221 if ($rev) {
222 return str_replace(' &lt;', '&lt;', str_replace('&gt; ', '&gt;', $str));
223 } else {
224 return str_replace('<', ' <', str_replace('>', '> ', $str));
225 }
226 }
227 }
228
229 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_diff.php'])) {
230 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_diff.php']);
231 }
232 ?>