Code

test change
[gosa.git] / include / functions_helpviewer.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003  Cajus Pollmeier, 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);
108 /* Read filecontent */
109 function getcontents($file)
111   $str = "" ;   // Temporary variable for file contents 
112   $tmp = "" ;   // Temporary varibale for partitial file contents
114   /* open file and read*/
115   $fp = fopen($file,"r");
116   if($fp) {
117     while($tmp = fread($fp,512))
118     {
119       $str.=  $tmp;
120     }
121   }else{
122     return(false);
123   }
124   return($str);
127 /*Remove tags */
128 function remove_unwanted_tags($str,$replacements)
130   $str=preg_replace($replacements['from'],$replacements['to'],$str);
131   return($str);
134 /*Converts the all links to specified path, is needed to get simple navigation */
135 function linkwrapper($str,$link)
137   $str=str_replace("HREF=\"","href=\"".$link."?pg=",$str);  
138   return($str);
141 /* Search content */
142 function search($arr,$word)
144   global $minwordlength,$allowed_chars_in_searchword;
145   /* Prepare Vars */ 
146   $result                     =array(); // Search result, filename, + hits + hits per word + matches 
147   $words                      =array(); // Temporary searchword handling
148   $useablewords               =array(); // Temporary searchword handling
149   $tryword                    = "";     // Temporary searchword handling
150   $result['global']['maxhit'] = 0;
151   unset($_SESSION['lastresults']);
152   unset($_SESSION['parsed_search_keyword']);
153   $_SESSION['parsed_search_keyword']="";
155   /* prepare searchwords */
156   $word   = trim($word);
158   /* Filter all unusable chars */
159   $word   = preg_replace($allowed_chars_in_searchword,"",$word);
160   $words  = split(" ",str_replace("+"," ",$word));
162   /* Check all wordlengths */
163   foreach($words as $tryword){
164     $tryword = trim($tryword);
166     /* Filter words smaler than 3 chars */
167     if(strlen($tryword)>=$minwordlength) {
168       $_SESSION['parsed_search_keyword'].=$tryword." ";
169       $useablewords[]=$tryword;
170     }
171   }
173   /* Use words to search the content */
174   foreach($arr as $key=>$val)
175   {
176     /* overallhits counts hits per page */
177     $overallhits=0;
179     /* Search all words */
180     foreach($useablewords as $word)
181     {
182       /* Skip key global, it contains no file data - it is a summary info*/
183       if($key!="global")
184       {
185         /* Get all hits for the word in $matches*/
186         preg_match_all("/".$word."/i",$arr[$key]['content'], $matches,PREG_OFFSET_CAPTURE);
188         /* Filter in Tag results*/
189         if(count($matches[0])){
190           foreach($matches[0] as $num=>$hit){
191             if(is_in_tag($arr[$key]['content'],$hit[1]))  {
192               unset($matches[0][$num]);    
193             }    
194           }
195         }
197         /* Count matches */
198         $overallhits=$overallhits + count($matches[0]);    
200         /* Save collected data */
201         $result[$key]['hits'][$word]    = count($matches[0]); 
202         $result[$key]['hits']['overall']= $overallhits;  
204         /* Save max hits for page */
205         if($overallhits > $result['global']['maxhit']){
206           $result['global']['maxhit']=$overallhits;  
207         }
209         /* Add results for word to return value*/
210         $result[$key]['match'][$word]=array();
211         $result[$key]['match'][$word]=$matches[0];
212       }
213     }
214   }
216   /* Save result in Session, so we can mark words later, or go back to search, without searching again*/
217   $_SESSION['lastresults'] = $result;
218   return($result);
221 /* Detect 10 Best result entries, sort and call createResultEntry to create HTML output for  complete list */
222 function searchlist($arr,$res,$maxresults)
224   $global = $res['global'];
225   $topten = array();        // To detect 10 best solutions
226   $ret    = "";             // return value
227   unset($res['global']);
229   /* Detect 10 best Sites */
230   foreach($res as $key=>$val){
232     /* Skip results with no hits */
233     if($val['hits']['overall']>0){
234       $topten[$key] = $val['hits']['overall']; 
235     }
236   }
238   /* Sort by hit position in content, to easier mark words */
239   asort($topten);
240   $topten = array_reverse($topten);
241   $topten = (array_slice($topten,0,$maxresults));
243   /* We have a result, an array with all content, an array with hits and position and we have the 10 best hits */
244   /* Foreach */  
245   foreach($topten as $name => $hits)  {
246     $ret.= createResultEntry($arr[$name],$res[$name],$name,$global['maxhit']);    
247   }
249   /* appending footer message for resultlist */
250   $ret.= "<br> ".count($topten)." - "._("Results for your search with the keyword")." <b>".htmlentities($_SESSION['search_string'])."</b>"._(" interpreted as ")."<b>".$_SESSION['parsed_search_keyword']."</b>";
252   return($ret);
255 /* This function marks a string with the given search result for this string*/
256 function markup_page($arr,$res)
258 global $pre_mark,$suf_mark;
259   $ret    = "";             // return value
260   $repl   = array();
261   $posadd = 0;
263   foreach($res['match'] as $word => $matches)   {
264     foreach($matches as $matchnr=>$match)   {
265       $repl[$match[1]]=$match[0];
266     }
267   }
269   ksort($repl);
271   foreach($repl as $position=>$word)  {
272     $pos1 = strlen($arr);
273     $arr= markword($arr,($position+$posadd),$word,$pre_mark,$suf_mark);
274     $pos2 = strlen($arr);
275     $posadd =$posadd + ($pos2 - $pos1);
276   }
277   return($arr); 
280 /* This function marks a single word with the specified prefix and suffix */
281 function markword($string,$position,$word,$prefix,$suffix)
283   $wordlength   = strlen($word);
284   $wholelength  = strlen($string); 
286   $first = substr($string,0,$position);
287   $last  = substr($string,($position+$wordlength),$wholelength);  
289   return($first.$prefix.$word.$suffix.$last);
290
293 /* Creates HTML output for a single search result entry */
294 function createResultEntry($entry,$res,$name,$max)
296   $percentage = (int)(($res['hits']['overall'] / $max) * 100) ;
298   $str =  "<b><a href=\"?pg=".$name."&mark=1\">".$percentage."% "._("hit rate in following file ").$name."</a></b><br>" ;
299   $str.=  substr(strip_tags($entry['content']),0,200);
300   $str.=  "<hr>";
302   return($str);
305 /*Simple function to detect if we prepare to change a tag or visible text */
306 function is_in_tag($string,$pos)
308   $pos1 = strpos($string,"<",$pos);
309   $pos2 = strpos($string,">",$pos);
311   if ($pos1 > $pos2)  {
312     return(true);
313   }else{
314     return(false);
315   }
318 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
319 ?>