Code

helpviewer now parse extern links
[gosa.git] / include / functions_helpviewer.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2005, Fabian Hickert
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
22 /******************************************* 
23   Only function definition will follow here
24 /*******************************************
27 /* Reads all files in specified directory with contents an some inforations about the file */
28 /* Read all files with contents*/
29 /*                 |Folder="/var/ww...",
30                    |        |Fileprefix="node"
31                    |        |       |Filesuffix=".html"
32                    |        |       |       |WithoutContent=false(This means : read content)
33                    |        |       |       |          |Singlepage=false(Means read all, if w want to read single, specify its filename)"*/
34 function readfiles($basedir,$prefix,$suffix,$onlyIndex,$singlepage=false)
35 {
36   global $replacements;
38   $str    = array();  // Temporary variable
39   $cnt    = 0;        // Array index creation
40   $file   = "";       // Contains Filename
42   $dir = opendir($basedir);
44   $str['global']['start']       = $cnt;     // collect basic informations - Startpage
45   $str['global']['basedir']     = $basedir; // collect basic informations - Basedirectory
47   /* Startime for Benchmark */ 
48   $start =   (time()+microtime());
50   /* if singlepage == false -> Get all pages, */
51   if(!$singlepage) {
53     /* While theres is an unreaded file in our resource */
54     while (($file = readdir($dir)) !== false) {
56       /* Filter all files which arn't intressting */
57       if((strstr($file,$suffix))&&($file!=".")&&($file!="..")&&(strstr($file,$prefix))){
59         /* Collect informations */
60         $str[$file]=array();
61         $str[$file]['name']   = $file;
62         $str[$file]['size']   = filesize($basedir.$file);
64         /* Readfile conent too ? */
65         if(!$onlyIndex){
66           $str[$file]['content']= remove_unwanted_tags(linkwrapper(getcontents($basedir.$file),""),$replacements);
67         }
69         /* Include file status, for debugging, not used in script yet */
70         $str[$file]['stat']   = stat($basedir.$file);
71         $cnt++;
72       }
73     }
75     /* Only get on file*/
76   }else{
77     /* Pages read = 1 */       
78     $cnt = 1;
80     /* Prepare result*/
81     $file                 = $singlepage;
82     $str[$file]           = array();
83     $str[$file]['name']   = $file;
84     $str[$file]['size']   = filesize($basedir.$file);
86     /* If onlyIndex == true skip reading content */
87     if(!$onlyIndex){
88       $str[$file]['content']= remove_unwanted_tags(linkwrapper(getcontents($basedir.$file),""),$replacements);
89     }
91     /* Include file status, for debugging, not used in script yet */
92     $str[$file]['stat']   = stat($basedir.$file);
93   }
95   /* Sort to  right order */
96   asort($str);
98   /* Endtime for Benchmark*/
99   $end = (time()+microtime());
100   $str['global']['cmptime'] = $end-$start;
102   /* Number of pages readed */
103   $str['global']['numpages']= $cnt;
104   closedir($dir);
105   return($str);
109 /* Read filecontent */
110 function getcontents($file)
112   $str = "" ;   // Temporary variable for file contents 
113   $tmp = "" ;   // Temporary varibale for partitial file contents
115   /* open file and read*/
116   $fp = fopen($file,"r");
117   if($fp) {
118     while($tmp = fread($fp,512))
119     {
120       $str.=  $tmp;
121     }
122   }else{
123     return(false);
124   }
125   return($str);
129 /*Remove tags */
130 function remove_unwanted_tags($str,$replacements)
132   $str=preg_replace($replacements['from'],$replacements['to'],$str);
133   return($str);
137 /*Converts the all links to specified path, is needed to get simple navigation */
138 function linkwrapper($str,$link)
140   $str = preg_replace("/HREF=\"http/i","target=\"_blank\" href=\"http",$str);
141   $str = preg_replace("/HREF=\"/","href=\"".$link."?pg=",$str);
142 //  $str=str_replace("HREF=\"","href=\"".$link."?pg=",$str);  
143   return($str);
147 /* Search content */
148 function search($arr,$word)
150   global $minwordlength,$allowed_chars_in_searchword;
151   /* Prepare Vars */ 
152   $result                     =array(); // Search result, filename, + hits + hits per word + matches 
153   $words                      =array(); // Temporary searchword handling
154   $useablewords               =array(); // Temporary searchword handling
155   $tryword                    = "";     // Temporary searchword handling
156   $result['global']['maxhit'] = 0;
157   unset($_SESSION['lastresults']);
158   unset($_SESSION['parsed_search_keyword']);
159   $_SESSION['parsed_search_keyword']="";
161   /* prepare searchwords */
162   $word   = trim($word);
164   /* Filter all unusable chars */
165   $word   = preg_replace($allowed_chars_in_searchword,"",$word);
166   $words  = split(" ",str_replace("+"," ",$word));
168   /* Check all wordlengths */
169   foreach($words as $tryword){
170     $tryword = trim($tryword);
172     /* Filter words smaler than 3 chars */
173     if(strlen($tryword)>=$minwordlength) {
174       $_SESSION['parsed_search_keyword'].=$tryword." ";
175       $useablewords[]=$tryword;
176     }
177   }
179   /* Use words to search the content */
180   foreach($arr as $key=>$val)
181   {
182     /* overallhits counts hits per page */
183     $overallhits=0;
185     /* Search all words */
186     foreach($useablewords as $word)
187     {
188       /* Skip key global, it contains no file data - it is a summary info*/
189       if($key!="global")
190       {
191         /* Get all hits for the word in $matches*/
192         preg_match_all("/".$word."/i",$arr[$key]['content'], $matches,PREG_OFFSET_CAPTURE);
194         /* Filter in Tag results*/
195         if(count($matches[0])){
196           foreach($matches[0] as $num=>$hit){
197             if(is_in_tag($arr[$key]['content'],$hit[1]))  {
198               unset($matches[0][$num]);    
199             }    
200           }
201         }
203         /* Count matches */
204         $overallhits=$overallhits + count($matches[0]);    
206         /* Save collected data */
207         $result[$key]['hits'][$word]    = count($matches[0]); 
208         $result[$key]['hits']['overall']= $overallhits;  
210         /* Save max hits for page */
211         if($overallhits > $result['global']['maxhit']){
212           $result['global']['maxhit']=$overallhits;  
213         }
215         /* Add results for word to return value*/
216         $result[$key]['match'][$word]=array();
217         $result[$key]['match'][$word]=$matches[0];
218       }
219     }
220   }
222   /* Save result in Session, so we can mark words later, or go back to search, without searching again*/
223   $_SESSION['lastresults'] = $result;
224   return($result);
228 /* Detect 10 Best result entries, sort and call createResultEntry to create HTML output for  complete list */
229 function searchlist($arr,$res,$maxresults)
231   $global = $res['global'];
232   $topten = array();        // To detect 10 best solutions
233   $ret    = "";             // return value
234   unset($res['global']);
236   /* Detect 10 best Sites */
237   foreach($res as $key=>$val){
239     /* Skip results with no hits */
240     if($val['hits']['overall']>0){
241       $topten[$key] = $val['hits']['overall']; 
242     }
243   }
245   /* Sort by hit position in content, to easier mark words */
246   asort($topten);
247   $topten = array_reverse($topten);
248   $topten = (array_slice($topten,0,$maxresults));
250   /* We have a result, an array with all content, an array with hits and position and we have the 10 best hits */
251   /* Foreach */  
252   foreach($topten as $name => $hits)  {
253     $ret.= createResultEntry($arr[$name],$res[$name],$name,$global['maxhit']);    
254   }
256   /* appending footer message for resultlist */
257   $ret.= "<br> ".sprintf(_("%s results for your search with the keyword %s interpreted as %s"), "<b>".count($topten)."</b>", "<b>".htmlentities($_SESSION['search_string'])."</b>", "<b>".$_SESSION['parsed_search_keyword']."</b>");
259   return($ret);
263 /* This function marks a string with the given search result for this string*/
264 function markup_page($arr,$res)
266   global $pre_mark,$suf_mark;
267   
268   $ret    = "";             // return value
269   $repl   = array();
270   $posadd = 0;
272   foreach($res['match'] as $word => $matches)   {
273     foreach($matches as $matchnr=>$match)   {
274       $repl[$match[1]]=$match[0];
275     }
276   }
278   ksort($repl);
280   foreach($repl as $position=>$word)  {
281     $pos1 = strlen($arr);
282     $arr= markword($arr,($position+$posadd),$word,$pre_mark,$suf_mark);
283     $pos2 = strlen($arr);
284     $posadd =$posadd + ($pos2 - $pos1);
285   }
286   return($arr); 
290 /* This function marks a single word with the specified prefix and suffix */
291 function markword($string,$position,$word,$prefix,$suffix)
293   $wordlength   = strlen($word);
294   $wholelength  = strlen($string); 
296   $first = substr($string,0,$position);
297   $last  = substr($string,($position+$wordlength),$wholelength);  
299   return($first.$prefix.$word.$suffix.$last);
300
303 /* Creates HTML output for a single search result entry */
304 function createResultEntry($entry,$res,$name,$max)
306   $percentage = (int)(($res['hits']['overall'] / $max) * 100) ;
308   $str =  "<b><a href=\"?pg=".$name."&mark=1\">".sprintf(_("%s%% hit rate in file %s"),$percentage,$name)."</a></b><br>" ;
309   $str.=  substr(strip_tags($entry['content']),0,200);
310   $str.=  "<hr>";
312   return($str);
316 /*Simple function to detect if we prepare to change a tag or visible text */
317 function is_in_tag($string,$pos)
319   $pos1 = strpos($string,"<",$pos);
320   $pos2 = strpos($string,">",$pos);
322   if ($pos1 > $pos2)  {
323     return(true);
324   }else{
325     return(false);
326   }
329 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
330 ?>