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
00066 class t3lib_diff {
00067
00068
00069 var $stripTags = 0;
00070 var $diffOptions = '';
00071
00072
00073 var $clearBufferIdx=0;
00074
00075
00076
00077
00085 function makeDiffDisplay($str1,$str2) {
00086 if ($this->stripTags) {
00087 $str1 = strip_tags($str1);
00088 $str2 = strip_tags($str2);
00089 } else {
00090 $str1 = $this->tagSpace($str1);
00091 $str2 = $this->tagSpace($str2);
00092 }
00093 $str1Lines = $this->explodeStringIntoWords($str1);
00094 $str2Lines = $this->explodeStringIntoWords($str2);
00095
00096 $diffRes = $this->getDiff(implode(chr(10),$str1Lines).chr(10),implode(chr(10),$str2Lines).chr(10));
00097
00098 if (is_array($diffRes)) {
00099 reset($diffRes);
00100 $c=0;
00101 $diffResArray=array();
00102 while(list(,$lValue)=each($diffRes)) {
00103 if (intval($lValue)) {
00104 $c=intval($lValue);
00105 $diffResArray[$c]['changeInfo']=$lValue;
00106 }
00107 if (substr($lValue,0,1)=='<') {
00108 $diffResArray[$c]['old'][]=substr($lValue,2);
00109 }
00110 if (substr($lValue,0,1)=='>') {
00111 $diffResArray[$c]['new'][]=substr($lValue,2);
00112 }
00113 }
00114
00115 $outString='';
00116 $clearBuffer='';
00117 for ($a=-1;$a<count($str1Lines);$a++) {
00118 if (is_array($diffResArray[$a+1])) {
00119 if (strstr($diffResArray[$a+1]['changeInfo'],'a')) {
00120 $clearBuffer.=htmlspecialchars($str1Lines[$a]).' ';
00121 }
00122
00123 $outString.=$this->addClearBuffer($clearBuffer);
00124 $clearBuffer='';
00125 if (is_array($diffResArray[$a+1]['old'])) {
00126 $outString.='<span class="diff-r">'.htmlspecialchars(implode(' ',$diffResArray[$a+1]['old'])).'</span> ';
00127 }
00128 if (is_array($diffResArray[$a+1]['new'])) {
00129 $outString.='<span class="diff-g">'.htmlspecialchars(implode(' ',$diffResArray[$a+1]['new'])).'</span> ';
00130 }
00131 $chInfParts = explode(',',$diffResArray[$a+1]['changeInfo']);
00132 if (!strcmp($chInfParts[0],$a+1)) {
00133 $newLine = intval($chInfParts[1])-1;
00134 if ($newLine>$a) $a=$newLine;
00135 }
00136 } else {
00137 $clearBuffer.=htmlspecialchars($str1Lines[$a]).' ';
00138 }
00139 }
00140 $outString.=$this->addClearBuffer($clearBuffer,1);
00141
00142 $outString = str_replace(' ',chr(10),$outString);
00143 if (!$this->stripTags) {
00144 $outString = $this->tagSpace($outString,1);
00145 }
00146 return $outString;
00147 }
00148 }
00149
00159 function getDiff($str1,$str2) {
00160
00161 $file1 = t3lib_div::tempnam('diff1_');
00162 t3lib_div::writeFile($file1,$str1);
00163
00164 $file2 = t3lib_div::tempnam('diff2_');
00165 t3lib_div::writeFile($file2,$str2);
00166
00167 $cmd = $GLOBALS['TYPO3_CONF_VARS']['BE']['diff_path'].' '.$this->diffOptions.' '.$file1.' '.$file2;
00168 exec($cmd,$res);
00169
00170 unlink($file1);
00171 unlink($file2);
00172
00173 return $res;
00174 }
00175
00184 function addClearBuffer($clearBuffer,$last=0) {
00185 if (strlen($clearBuffer)>200) {
00186 $clearBuffer=($this->clearBufferIdx?t3lib_div::fixed_lgd_cs($clearBuffer,70):'').'['.strlen($clearBuffer).']'.(!$last?t3lib_div::fixed_lgd_cs($clearBuffer,-70):'');
00187 }
00188 $this->clearBufferIdx++;
00189 return $clearBuffer;
00190 }
00191
00200 function explodeStringIntoWords($str) {
00201 $strArr = t3lib_div::trimExplode(chr(10),$str);
00202 $outArray=array();
00203 reset($strArr);
00204 while(list(,$lineOfWords)=each($strArr)) {
00205 $allWords = t3lib_div::trimExplode(' ',$lineOfWords,1);
00206 $outArray = array_merge($outArray,$allWords);
00207 $outArray[]='';
00208 $outArray[]='';
00209 }
00210 return $outArray;
00211 }
00212
00221 function tagSpace($str,$rev=0) {
00222 if ($rev) {
00223 return str_replace(' <','<',str_replace('> ','>',$str));
00224 } else {
00225 return str_replace('<',' <',str_replace('>','> ',$str));
00226 }
00227 }
00228 }
00229
00230 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_diff.php']) {
00231 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_diff.php']);
00232 }
00233 ?>