00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00067 class t3lib_diff {
00068
00069
00070 var $stripTags = 0;
00071 var $diffOptions = '';
00072
00073
00074 var $clearBufferIdx=0;
00075
00076
00077
00078
00086 function makeDiffDisplay($str1,$str2) {
00087 if ($this->stripTags) {
00088 $str1 = strip_tags($str1);
00089 $str2 = strip_tags($str2);
00090 } else {
00091 $str1 = $this->tagSpace($str1);
00092 $str2 = $this->tagSpace($str2);
00093 }
00094 $str1Lines = $this->explodeStringIntoWords($str1);
00095 $str2Lines = $this->explodeStringIntoWords($str2);
00096
00097 $diffRes = $this->getDiff(implode(chr(10),$str1Lines).chr(10),implode(chr(10),$str2Lines).chr(10));
00098
00099 if (is_array($diffRes)) {
00100 reset($diffRes);
00101 $c=0;
00102 $diffResArray=array();
00103 while(list(,$lValue)=each($diffRes)) {
00104 if (intval($lValue)) {
00105 $c=intval($lValue);
00106 $diffResArray[$c]['changeInfo']=$lValue;
00107 }
00108 if (substr($lValue,0,1)=='<') {
00109 $diffResArray[$c]['old'][]=substr($lValue,2);
00110 }
00111 if (substr($lValue,0,1)=='>') {
00112 $diffResArray[$c]['new'][]=substr($lValue,2);
00113 }
00114 }
00115
00116 $outString='';
00117 $clearBuffer='';
00118 for ($a=-1;$a<count($str1Lines);$a++) {
00119 if (is_array($diffResArray[$a+1])) {
00120 if (strstr($diffResArray[$a+1]['changeInfo'],'a')) {
00121 $clearBuffer.=htmlspecialchars($str1Lines[$a]).' ';
00122 }
00123
00124 $outString.=$this->addClearBuffer($clearBuffer);
00125 $clearBuffer='';
00126 if (is_array($diffResArray[$a+1]['old'])) {
00127 $outString.='<span class="diff-r">'.htmlspecialchars(implode(' ',$diffResArray[$a+1]['old'])).'</span> ';
00128 }
00129 if (is_array($diffResArray[$a+1]['new'])) {
00130 $outString.='<span class="diff-g">'.htmlspecialchars(implode(' ',$diffResArray[$a+1]['new'])).'</span> ';
00131 }
00132 $chInfParts = explode(',',$diffResArray[$a+1]['changeInfo']);
00133 if (!strcmp($chInfParts[0],$a+1)) {
00134 $newLine = intval($chInfParts[1])-1;
00135 if ($newLine>$a) $a=$newLine;
00136 }
00137 } else {
00138 $clearBuffer.=htmlspecialchars($str1Lines[$a]).' ';
00139 }
00140 }
00141 $outString.=$this->addClearBuffer($clearBuffer,1);
00142
00143 $outString = str_replace(' ',chr(10),$outString);
00144 if (!$this->stripTags) {
00145 $outString = $this->tagSpace($outString,1);
00146 }
00147 return $outString;
00148 }
00149 }
00150
00160 function getDiff($str1,$str2) {
00161 if (TYPO3_OS!='WIN') {
00162
00163 $file1 = t3lib_div::tempnam('diff1_');
00164 t3lib_div::writeFile($file1,$str1);
00165
00166 $file2 = t3lib_div::tempnam('diff2_');
00167 t3lib_div::writeFile($file2,$str2);
00168
00169 $cmd = $GLOBALS['TYPO3_CONF_VARS']['BE']['diff_path'].' '.$this->diffOptions.' '.$file1.' '.$file2;
00170 exec($cmd,$res);
00171
00172 unlink($file1);
00173 unlink($file2);
00174
00175 return $res;
00176 }
00177 }
00178
00187 function addClearBuffer($clearBuffer,$last=0) {
00188 if (strlen($clearBuffer)>200) {
00189 $clearBuffer=($this->clearBufferIdx?t3lib_div::fixed_lgd_cs($clearBuffer,70):'').'['.strlen($clearBuffer).']'.(!$last?t3lib_div::fixed_lgd_cs($clearBuffer,-70):'');
00190 }
00191 $this->clearBufferIdx++;
00192 return $clearBuffer;
00193 }
00194
00203 function explodeStringIntoWords($str) {
00204 $strArr = t3lib_div::trimExplode(chr(10),$str);
00205 $outArray=array();
00206 reset($strArr);
00207 while(list(,$lineOfWords)=each($strArr)) {
00208 $allWords = t3lib_div::trimExplode(' ',$lineOfWords,1);
00209 $outArray = array_merge($outArray,$allWords);
00210 $outArray[]='';
00211 $outArray[]='';
00212 }
00213 return $outArray;
00214 }
00215
00224 function tagSpace($str,$rev=0) {
00225 if ($rev) {
00226 return str_replace(' <','<',str_replace('> ','>',$str));
00227 } else {
00228 return str_replace('<',' <',str_replace('>','> ',$str));
00229 }
00230 }
00231 }
00232
00233 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_diff.php']) {
00234 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_diff.php']);
00235 }
00236 ?>