Updated keyword expansion to be disabled for all gif,png,jpg files and sxw (OpenOffic...
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_diff.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2003 Kasper Skårhøj (kasper@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 * This class has functions which generates a difference output of a content string
29 *
30 * $Id$
31 *
32 * @author Kasper Skårhøj <kasper@typo3.com>
33 * @package TYPO3
34 * @subpackage t3lib
35 */
36
37
38 class t3lib_diff {
39 var $stripTags = 0;
40 var $clearBufferIdx=0;
41
42 function getDiff($str1,$str2) {
43 if (TYPO3_OS!="WIN") {
44 // Create file 1 and write string
45 $file1 = tempnam("","");
46 t3lib_div::writeFile($file1,$str1);
47 // Create file 2 and write string
48 $file2 = tempnam("","");
49 t3lib_div::writeFile($file2,$str2);
50 // Perform diff.
51 $cmd = $GLOBALS["TYPO3_CONF_VARS"]["BE"]["diff_path"]." ".$file1." ".$file2;
52 exec($cmd,$res);
53
54 unlink($file1);
55 unlink($file2);
56
57 return $res;
58 }
59 }
60 function explodeStringIntoWords($str) {
61 $strArr = t3lib_div::trimExplode(chr(10),$str);
62 $outArray=array();
63 reset($strArr);
64 while(list(,$lineOfWords)=each($strArr)) {
65 $allWords = t3lib_div::trimExplode(" ",$lineOfWords,1);
66 $outArray = array_merge($outArray,$allWords);
67 $outArray[]="";
68 $outArray[]="";
69 }
70 return $outArray;
71 }
72 function tagSpace($str,$rev=0) {
73 if ($rev) {
74 return str_replace(" &lt;","&lt;",str_replace("&gt; ","&gt;",$str));
75 } else {
76 return str_replace("<"," <",str_replace(">","> ",$str));
77 }
78 }
79 function makeDiffDisplay($str1,$str2) {
80 if ($this->stripTags) {
81 $str1 = strip_tags($str1);
82 $str2 = strip_tags($str2);
83 } else {
84 $str1 = $this->tagSpace($str1);
85 $str2 = $this->tagSpace($str2);
86 }
87 $str1Lines = $this->explodeStringIntoWords($str1);
88 $str2Lines = $this->explodeStringIntoWords($str2);
89 // debug($str1Lines);
90 // debug($str2Lines);
91 $diffRes = $this->getDiff(implode(chr(10),$str1Lines).chr(10),implode(chr(10),$str2Lines).chr(10));
92 //debug($diffRes);
93 if (is_array($diffRes)) {
94 reset($diffRes);
95 $c=0;
96 $diffResArray=array();
97 while(list(,$lValue)=each($diffRes)) {
98 if (intval($lValue)) {
99 $c=intval($lValue);
100 $diffResArray[$c]["changeInfo"]=$lValue;
101 }
102 if (substr($lValue,0,1)=="<") {
103 $diffResArray[$c]["old"][]=substr($lValue,2);
104 }
105 if (substr($lValue,0,1)==">") {
106 $diffResArray[$c]["new"][]=substr($lValue,2);
107 }
108 }
109 // debug($str1Lines);
110 // debug($str2Lines);
111 // debug($diffResArray);
112
113 $outString="";
114 $clearBuffer="";
115 for ($a=-1;$a<count($str1Lines);$a++) {
116 if (is_array($diffResArray[$a+1])) {
117 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.
118 $clearBuffer.=htmlspecialchars($str1Lines[$a])." ";
119 }
120
121 $outString.=$this->addClearBuffer($clearBuffer);
122 $clearBuffer="";
123 if (is_array($diffResArray[$a+1]["old"])) {
124 $outString.='<font color=red>'.htmlspecialchars(implode(" ",$diffResArray[$a+1]["old"])).'</font> ';
125 }
126 if (is_array($diffResArray[$a+1]["new"])) {
127 $outString.='<font color=green>'.htmlspecialchars(implode(" ",$diffResArray[$a+1]["new"])).'</font> ';
128 }
129 $chInfParts = explode(",",$diffResArray[$a+1]["changeInfo"]);
130 if (!strcmp($chInfParts[0],$a+1)) {
131 $newLine = intval($chInfParts[1])-1;
132 if ($newLine>$a) $a=$newLine; // Security that $a is not set lower than current for some reason...
133 }
134 } else {
135 $clearBuffer.=htmlspecialchars($str1Lines[$a])." ";
136 }
137 }
138 $outString.=$this->addClearBuffer($clearBuffer,1);
139
140 $outString = str_replace(" ",chr(10),$outString);
141 if (!$this->stripTags) {
142 $outString = $this->tagSpace($outString,1);
143 }
144 return $outString;
145 }
146 }
147 function addClearBuffer($clearBuffer,$last=0) {
148 if (strlen($clearBuffer)>200) {
149 $clearBuffer=($this->clearBufferIdx?t3lib_div::fixed_lgd($clearBuffer,70):"")."[".strlen($clearBuffer)."]".(!$last?t3lib_div::fixed_lgd_pre($clearBuffer,70):"");
150 }
151 $this->clearBufferIdx++;
152 return $clearBuffer;
153 }
154 }
155
156
157
158 if (defined("TYPO3_MODE") && $TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["t3lib/class.t3lib_diff.php"]) {
159 include_once($TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["t3lib/class.t3lib_diff.php"]);
160 }
161
162 ?>