Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        frontend
5
 * @package         search
6
 * @author          WebsiteBaker Project
7
 * @copyright       2004-2009, Ryan Djurovich
8
 * @copyright       2009-2010, Website Baker Org. e.V.
9
 * @link            http://www.websitebaker2.org/
10
 * @license         http://www.gnu.org/licenses/gpl.html
11
 * @platform        WebsiteBaker 2.8.x
12
 * @requirements    PHP 4.3.4 and higher
13
 * @version         $Id: search_modext.php 2 2017-07-02 15:14:29Z Manuela $
14
 * @filesource        $HeadURL: svn://isteam.dynxs.de/wb/2.10.x/trunk/search/search_modext.php $
15
 * @lastmodified    $Date: 2017-07-02 17:14:29 +0200 (Sun, 02 Jul 2017) $
16
 *
17
 */
18

    
19
// make the url-string for highlighting
20
function make_url_searchstring($search_match, $search_url_array) {
21
    $link = "";
22
    if ($search_match != 'exact') {
23
        $str = implode(" ", $search_url_array);
24
        $link = "?searchresult=1&amp;sstring=".urlencode($str);
25
    } else {
26
        $str = str_replace(' ', '_', $search_url_array[0]);
27
        $link = "?searchresult=2&amp;sstring=".urlencode($str);
28
    }
29
    return $link;
30
}
31

    
32
// make date and time for "last modified by... on ..."-string
33
function get_page_modified($page_modified_when) {
34
    global $TEXT;
35
    if($page_modified_when > 0) {
36
        $date = gmdate(DATE_FORMAT, $page_modified_when+TIMEZONE);
37
        $time = gmdate(TIME_FORMAT, $page_modified_when+TIMEZONE);
38
    } else {
39
        $date = $TEXT['UNKNOWN'].' '.$TEXT['DATE'];
40
        $time = $TEXT['UNKNOWN'].' '.$TEXT['TIME'];
41
    }
42
    return array($date, $time);
43
}
44

    
45
// make username and displayname for "last modified by... on ..."-string
46
function get_page_modified_by($page_modified_by, $users) {
47
    global $TEXT;
48
    if($page_modified_by>0) {
49
        $username = $users[$page_modified_by]['username'];
50
        $displayname = $users[$page_modified_by]['display_name'];
51
    } else {
52
        $username = "";
53
        $displayname = $TEXT['UNKNOWN'];
54
    }
55
    return array($username, $displayname);
56
}
57

    
58
// checks if _all_ searchwords matches
59
function is_all_matched($text, $search_words) {
60
    $all_matched = true;
61
    foreach ($search_words AS $word) {
62
        if(!preg_match('/'.$word.'/i', $text)) {
63
            $all_matched = false;
64
            break;
65
        }
66
    }
67
    return $all_matched;
68
}
69

    
70
// checks if _any_ of the searchwords matches
71
function is_any_matched($text, $search_words) {
72
    $any_matched = false;
73
    $word = '('.implode('|', $search_words).')';
74
    if(preg_match('/'.$word.'/i', $text)) {
75
        $any_matched = true;
76
    }
77
    return $any_matched;
78
}
79

    
80
// collects the matches from text in excerpt_array
81
function get_excerpts($text, $search_words, $max_excerpt_num) {
82
    $excerpt_array = FALSE;
83
    $word = '('.implode('|', $search_words).')';
84
    // start-sign: .!?; + INVERTED EXCLAMATION MARK - INVERTED QUESTION MARK - DOUBLE EXCLAMATION MARK - INTERROBANG - EXCLAMATION QUESTION MARK - QUESTION EXCLAMATION MARK - DOUBLE QUESTION MARK - HALFWIDTH IDEOGRAPHIC FULL STOP - IDEOGRAPHIC FULL STOP - IDEOGRAPHIC COMMA
85
    $p_start=".!?;"."\xC2\xA1"."\xC2\xBF"."\xE2\x80\xBC"."\xE2\x80\xBD"."\xE2\x81\x89"."\xE2\x81\x88"."\xE2\x81\x87"."\xEF\xBD\xA1"."\xE3\x80\x82"."\xE3\x80\x81";
86
    // stop-sign: .!?; + DOUBLE EXCLAMATION MARK - INTERROBANG - EXCLAMATION QUESTION MARK - QUESTION EXCLAMATION MARK - DOUBLE QUESTION MARK - HALFWIDTH IDEOGRAPHIC FULL STOP - IDEOGRAPHIC FULL STOP - IDEOGRAPHIC COMMA
87
    $p_stop=".!?;"."\xE2\x80\xBC"."\xE2\x80\xBD"."\xE2\x81\x89"."\xE2\x81\x88"."\xE2\x81\x87"."\xEF\xBD\xA1"."\xE3\x80\x82"."\xE3\x80\x81";
88
    // jump from match to match, get excerpt, stop if $max_excerpt_num is reached
89
    $match_array = $matches = array();
90
    $startpos = $wordpos = $endpos = 0; // although preg_match with u-switch handles unicode correctly, the ...pos-variables will count bytes (not chars) 
91
    while(preg_match("/$word/i", $text, $match_array, PREG_OFFSET_CAPTURE, $startpos)) {
92
        $wordpos = $match_array[0][1];
93
        $startpos = ($wordpos-200 < $endpos)?$endpos:$wordpos-200;
94
        $endpos = $wordpos+200;
95
        // look for better start position
96
        if(preg_match_all("/[$p_start]/u", substr($text, $startpos, $wordpos-$startpos), $matches, PREG_OFFSET_CAPTURE))
97
            $startpos += $matches[0][count($matches[0])-1][1]; // set startpos at last punctuation before word
98
        // look for better end position
99
        if(preg_match_all("/[$p_stop]/u", substr($text, $wordpos, $endpos-$wordpos), $matches, PREG_OFFSET_CAPTURE))
100
            $endpos = $wordpos+$matches[0][0][1]; // set endpos at first punctuation after word
101
        $match = substr($text, $startpos+1, $endpos-$startpos);
102
        if(!preg_match('/\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\./', $match)) // skip excerpts with email-addresses
103
            $excerpt_array[] = trim($match);
104
        if(count($excerpt_array)>=$max_excerpt_num) {
105
            $excerpt_array = array_unique($excerpt_array);
106
            if(count($excerpt_array) >= $max_excerpt_num)
107
                break;
108
        }
109
        // restart at last endpos
110
        $startpos = $endpos;
111
    }
112
    return $excerpt_array;
113
}
114

    
115
// makes excerpt_array a string ready to print out
116
function prepare_excerpts($excerpt_array, $search_words, $max_excerpt_num) {
117
    // excerpts: text before and after a single excerpt, html-tag for markup
118
    $EXCERPT_BEFORE =       '...&nbsp;';
119
    $EXCERPT_AFTER =        '&nbsp;...<br />';
120
    $EXCERPT_MARKUP_START = '<b>';
121
    $EXCERPT_MARKUP_END =   '</b>';
122
    // remove duplicate matches from $excerpt_array, if any.
123
    $excerpt_array = array_unique($excerpt_array);
124
    // use the first $max_excerpt_num excerpts only
125
    if(count($excerpt_array) > $max_excerpt_num) {
126
        $excerpt_array = array_slice($excerpt_array, 0, $max_excerpt_num);
127
    }
128
    // prepare search-string
129
    $string = "(".implode("|", $search_words).")";
130
    // we want markup on search-results page,
131
    // but we need some 'magic' to prevent <br />, <b>... from being highlighted
132
    $excerpt = '';
133
    foreach($excerpt_array as $str) {
134
        $excerpt .= '#,,#'.preg_replace("/($string)/i","#,,,,#$1#,,,,,#",$str).'#,,,#';
135
    }
136
    $excerpt = str_replace(array('&','<','>','"','\'',"\xC2\xA0"), array('&amp;','&lt;','&gt;','&quot;','&#039;',' '), $excerpt);
137
    $excerpt = str_replace(array('#,,,,#','#,,,,,#'), array($EXCERPT_MARKUP_START,$EXCERPT_MARKUP_END), $excerpt);
138
    $excerpt = str_replace(array('#,,#','#,,,#'), array($EXCERPT_BEFORE,$EXCERPT_AFTER), $excerpt);
139
    // prepare to write out
140
    if(DEFAULT_CHARSET != 'utf-8') {
141
        $excerpt = umlauts_to_entities($excerpt, 'UTF-8');
142
    }
143
    return $excerpt;
144
}
145

    
146
// work out what the link-anchor should be
147
function make_url_target($page_link_target, $text, $search_words) {
148
    // 1. e.g. $page_link_target=="&monthno=5&year=2007" - module-dependent target. Do nothing.
149
    // 2. $page_link_target=="#!wb_section_..." - the user wants the section-target, so do nothing.
150
    // 3. $page_link_target=="#wb_section_..." - try to find a better target, use the section-target as fallback.
151
    // 4. $page_link_target=="" - do nothing
152
    if(version_compare(PHP_VERSION, '4.3.3', ">=") && substr($page_link_target,0,12)=='#wb_section_') {
153
        $word = '('.implode('|', $search_words).')';
154
        preg_match('/'.$word.'/i', $text, $match, PREG_OFFSET_CAPTURE);
155
        if($match && is_array($match[0])) {
156
            $x=$match[0][1]; // position of first match
157
            // is there an anchor nearby?
158
            if(preg_match_all('/<\s*(?:a[^>]+?name|[^>]+?id)\s*=\s*"([^"]+)"/i', substr($text,0,$x), $match)) {
159
                $page_link_target = '#'.$match[1][count($match[1])-1];
160
            }
161
        }
162
    }
163
    elseif(substr($page_link_target,0,13)=='#!wb_section_') {
164
        $page_link_target = '#'.substr($page_link_target, 2);
165
    }
166
    
167
    // since wb 2.7.1 the section-anchor is configurable - SEC_ANCHOR holds the anchor name
168
    if(substr($page_link_target,0,12)=='#wb_section_') {
169
        if(defined('SEC_ANCHOR') && SEC_ANCHOR!='') {
170
            $sec_id = substr($page_link_target, 12);
171
            $page_link_target = '#'.SEC_ANCHOR.$sec_id;
172
        } else { // section-anchors are disabled
173
            $page_link_target = '';
174
        }
175
    }
176
    
177
    return $page_link_target;
178
}
179

    
180
// wrapper for compatibility with old print_excerpt()
181
function print_excerpt($page_link, $page_link_target, $page_title, $page_description, $page_modified_when, $page_modified_by, $text, $max_excerpt_num, $func_vars, $pic_link="") {
182
    $mod_vars = array(
183
        'page_link' => $page_link,
184
        'page_link_target' => $page_link_target,
185
        'page_title' => $page_title,
186
        'page_description' => $page_description,
187
        'page_modified_when' => $page_modified_when,
188
        'page_modified_by' => $page_modified_by,
189
        'text' => $text,
190
        'max_excerpt_num' => $max_excerpt_num,
191
        'pic_link' => $pic_link
192
    );
193
    print_excerpt2($mod_vars, $func_vars);
194
}
195

    
196
/* These functions can be used in module-supplied search_funcs
197
 * -----------------------------------------------------------
198
 * print_excerpt2() - the main-function to use in all search_funcs
199
 * print_excerpt() - wrapper for compatibility-reason. Use print_excerpt2() instead.
200
 * list_files_dirs() - lists all files and dirs below a given directory
201
 * clear_filelist() - keeps only wanted or removes unwanted entries in file-list.
202
 */
203
 
204
// prints the excerpts for one section
205
function print_excerpt2($mod_vars, $func_vars) {
206
    extract($func_vars, EXTR_PREFIX_ALL, 'func');
207
    extract($mod_vars, EXTR_PREFIX_ALL, 'mod');
208
    global $TEXT;
209
    // check $mod_...vars
210
    if(!isset($mod_page_link))          $mod_page_link = $func_page_link;
211
    if(!isset($mod_page_link_target))   $mod_page_link_target = "";
212
    if(!isset($mod_page_title))         $mod_page_title = $func_page_title;
213
    if(!isset($mod_page_description))   $mod_page_description = $func_page_description;
214
    if(!isset($mod_page_modified_when)) $mod_page_modified_when = $func_page_modified_when;
215
    if(!isset($mod_page_modified_by))   $mod_page_modified_by = $func_page_modified_by;
216
    if(!isset($mod_text))               $mod_text = "";
217
    if(!isset($mod_max_excerpt_num))    $mod_max_excerpt_num = $func_default_max_excerpt;
218
    if(!isset($mod_pic_link))           $mod_pic_link = "";
219
    if(!isset($mod_no_highlight))       $mod_no_highlight = false;
220
    if(!isset($func_enable_flush))      $func_enable_flush = false; // set this in db: wb_search.cfg_enable_flush [READ THE DOC BEFORE]
221
    if(isset($mod_ext_charset)) $mod_ext_charset = strtolower($mod_ext_charset);
222
    else $mod_ext_charset = '';
223

    
224
    if($mod_text == "") // nothing to do
225
        { return false; }
226

    
227
    if($mod_no_highlight) // no highlighting
228
        { $mod_page_link_target = "&amp;nohighlight=1".$mod_page_link_target; }
229
    // clean the text:
230
    $mod_text = preg_replace('#<(br|dt|/dd|/?(?:h[1-6]|tr|table|p|li|ul|pre|code|div|hr))[^>]*>#i', '.', $mod_text);
231
    $mod_text = preg_replace('#<(!--.*--|style.*</style|script.*</script)>#iU', ' ', $mod_text);
232
    $mod_text = preg_replace('#\[\[.*?\]\]#', '', $mod_text); //Filter droplets from the page data
233
    // strip_tags() is called below
234
    if($mod_ext_charset!='') { // data from external database may have a different charset
235
        require_once(WB_PATH.'/framework/functions-utf8.php');
236
        switch($mod_ext_charset) {
237
        case 'latin1':
238
        case 'cp1252':
239
            $mod_text = charset_to_utf8($mod_text, 'CP1252');
240
            break;
241
        case 'cp1251':
242
            $mod_text = charset_to_utf8($mod_text, 'CP1251');
243
            break;
244
        case 'latin2':
245
            $mod_text = charset_to_utf8($mod_text, 'ISO-8859-2');
246
            break;
247
        case 'hebrew':
248
            $mod_text = charset_to_utf8($mod_text, 'ISO-8859-8');
249
            break;
250
        case 'greek':
251
            $mod_text = charset_to_utf8($mod_text, 'ISO-8859-7');
252
            break;
253
        case 'latin5':
254
            $mod_text = charset_to_utf8($mod_text, 'ISO-8859-9');
255
            break;
256
        case 'latin7':
257
            $mod_text = charset_to_utf8($mod_text, 'ISO-8859-13');
258
            break;
259
        case 'utf8':
260
        default:
261
            $mod_text = charset_to_utf8($mod_text, 'UTF-8');
262
        }
263
    } else {
264
        $mod_text = entities_to_umlauts($mod_text, 'UTF-8');
265
    }
266
    $anchor_text = $mod_text; // make an copy containing html-tags
267
    $mod_text = strip_tags($mod_text);
268
    $mod_text = str_replace(array('&gt;','&lt;','&amp;','&quot;','&#039;','&apos;','&nbsp;'), array('>','<','&','"','\'','\'',' '), $mod_text);
269
    $mod_text = '.'.trim($mod_text).'.';
270
    // Do a fast scan over $mod_text first. This may speedup things a lot.
271
    if($func_search_match == 'all') {
272
        if(!is_all_matched($mod_text, $func_search_words))
273
            return false;
274
    }
275
    elseif(!is_any_matched($mod_text, $func_search_words)) {
276
        return false;
277
    }
278
    // search for an better anchor - this have to be done before strip_tags() (may fail if search-string contains <, &, amp, gt, lt, ...)
279
    $anchor =  make_url_target($mod_page_link_target, $anchor_text, $func_search_words);
280
    // make the link from $mod_page_link, add anchor
281
    $link = "";
282
    $link = page_link($mod_page_link);
283
    if(strpos($mod_page_link, 'http:')===FALSE)
284
        $link .= make_url_searchstring($func_search_match, $func_search_url_array);
285
    $link .= $anchor;
286

    
287
    // now get the excerpt
288
    $excerpt = "";
289
    $excerpt_array = array();
290
    if($mod_max_excerpt_num > 0) {
291
        if(!$excerpt_array = get_excerpts($mod_text, $func_search_words, $mod_max_excerpt_num)) {
292
            return false;
293
        }
294
        $excerpt = prepare_excerpts($excerpt_array, $func_search_words, $mod_max_excerpt_num);
295
    }
296
    // handle thumbs - to deactivate this look in the module's search.php: $show_thumb (or maybe in the module's settings-page)
297
    if($mod_pic_link != "") {
298
        if(isset($mod_special) && $mod_special=='lightbox2_plus')
299
            $excerpt = '<table class="excerpt_thumb" width="100%" cellspacing="0" cellpadding="0" border="0"><tbody><tr><td width="110" valign="top">'.$mod_special_piclink.'<img src="'.WB_URL.'/'.MEDIA_DIRECTORY.$mod_pic_link.'" alt="" /></a></td><td>'.$excerpt.'</td></tr></tbody></table>';
300
        else
301
            $excerpt = '<table class="excerpt_thumb" width="100%" cellspacing="0" cellpadding="0" border="0"><tbody><tr><td width="110" valign="top"><a href="'.$link.'"><img src="'.WB_URL.'/'.MEDIA_DIRECTORY.$mod_pic_link.'" alt="" /></a></td><td>'.$excerpt.'</td></tr></tbody></table>';
302
    }
303

    
304
    // print-out the excerpt
305
    $vars = array();
306
    $values = array();
307
    list($date, $time) = get_page_modified($mod_page_modified_when);
308
    list($username, $displayname) = get_page_modified_by($mod_page_modified_by, $func_users);
309
    $vars = array('[LINK]', '[TITLE]','[PAGE_TITLE]' ,'[MENU_TITLE]' , '[DESCRIPTION]', '[USERNAME]','[DISPLAY_NAME]','[DATE]','[TIME]','[TEXT_LAST_UPDATED_BY]','[TEXT_ON]','[EXCERPT]');
310
    $values = array(
311
        $link,
312
        $mod_page_title,
313
        $func_page_title,
314
        $func_page_menu_title,
315
        $mod_page_description,
316
        $username,
317
        $displayname,
318
        $date,
319
        $time,
320
        $TEXT['LAST_UPDATED_BY'],
321
        $TEXT['ON'],
322
        $excerpt
323
    );
324
    echo str_replace($vars, $values, $func_results_loop_string);
325
    if($func_enable_flush) { // ATTN: this will bypass output-filters and may break template-layout or -filters
326
        ob_flush();flush();
327
    }
328
    return true;
329
}
330

    
331
// list all files and dirs in $dir (recursive), omits '.', '..', and hidden files/dirs
332
// returns an array of two arrays ($files[] and $dirs[]).
333
// usage: list($files,$dirs) = list_files_dirs($directory);
334
//        $depth: get subdirs (true/false)
335
function list_files_dirs($dir, $depth=true, $files=array(), $dirs=array()) {
336
    $dh=opendir($dir);
337
    while(($file = readdir($dh)) !== false) {
338
        if($file{0} == '.' || $file == '..') {
339
            continue;
340
        }
341
        if(is_dir($dir.'/'.$file)) {
342
            if($depth) {
343
                $dirs[] = $dir.'/'.$file;
344
                list($files, $dirs) = list_files_dirs($dir.'/'.$file, $depth, $files, $dirs);
345
            }
346
        } else {
347
            $files[] = $dir.'/'.$file;
348
        }
349
    }
350
    closedir($dh);
351
    natcasesort($files);
352
    natcasesort($dirs);
353
    return(array($files, $dirs));
354
}
355

    
356
// keeps only wanted entries in array $files. $str have to be an eregi()-compatible regex
357
function clear_filelist($files, $str, $keep=true) {
358
    // options: $keep = true  : remove all non-matching entries
359
    //          $keep = false : remove all matching entries
360
    $c_filelist = array();
361
    if($str == '')
362
        return $files;
363
    foreach($files as $file) {
364
        if($keep) {
365
            if(preg_match("~$str~i", $file)) {
366
                $c_filelist[] = $file;
367
            }
368
        } else {
369
            if(!preg_match("~$str~i", $file)) {
370
                $c_filelist[] = $file;
371
            }
372
        }
373
    }
374
    return($c_filelist);
375
}
376

    
377
function search_make_sql_part($words, $match, $columns) {
378
    // $words are utf-8 encoded, will be converted to DEFAULT_CHARSET below
379
    if(empty($words) || empty($columns)) return('(1=1)');
380
    global $database;
381

    
382
    // check if we can use SQL'S "LIKE"
383
    // work-around for WB'S missing-SET-NAMES-problem
384
    static $checked = FALSE;
385
    if($checked===FALSE) {
386
        $checked = TRUE;
387
        $lowers = array('utf8'=>"\xc3\xa1", 'iso'=>"\xe1");
388
        $uppers = array('utf8'=>"\xc3\x81", 'iso'=>"\xc1");
389
        switch(DEFAULT_CHARSET) {
390
            case 'utf-8':
391
                $lo = $lowers['utf8'];
392
                $up = $uppers['utf8'];
393
                break;
394
            case 'iso-8859-1':
395
            case 'iso-8859-2':
396
            case 'iso-8859-3':
397
            case 'iso-8859-4':
398
            case 'iso-8859-5':
399
            case 'iso-8859-7':
400
            case 'iso-8859-9':
401
            case 'iso-8859-10':
402
                $lo = $lowers['iso'];
403
                $up = $uppers['iso'];
404
                break;
405
            default:
406
                $checked = 'check failed'; // we can't handle arabic,hebrew,thai
407
        }
408
        if($checked===TRUE && $query = $database->query("SELECT UPPER('$lo')='$up'")) {
409
            $res = $query->fetchRow();
410
            if($res[0]==0) {
411
                $checked = 'check failed';
412
            }
413
        } else
414
            $checked = 'check failed';
415
    }
416

    
417
    require_once(WB_PATH.'/framework/functions-utf8.php');
418
    global $search_table_sql_local;
419
    $altnum = count($search_table_sql_local);
420

    
421
    if($match=='all') $op = 'AND';
422
    else $op = ' OR'; // keep the leading space!
423

    
424
    // create sql-template
425
    $sql = '';
426
    $i = 0;
427
    foreach($words as $w) {
428
        if(empty($w)) continue;
429
        $w_alts = $e_alts = array();
430
        if($altnum) {
431
            for($x=0;$x<$altnum;$x++)
432
                $w_alts[$x] = strtr($w, $search_table_sql_local[$x]);
433
        } else {
434
            $w_alts[0] = $w;
435
        }
436
        $w_alts = array_unique($w_alts);
437
        foreach($w_alts as $a) {
438
            $tmp = htmlentities($a, ENT_COMPAT, 'UTF-8');
439
            // if the missing-SET-NAMES-issue appears and $tmp contains non-ascii characters: exit and use the normal (slow) search-function instead
440
            if($checked!==TRUE && preg_match('/[\x80-\xFF]/', $tmp)) return('(1=1)'); // missing-SET-NAMES-issue
441
            $e_alts[] = $tmp;
442
        }
443
        $sql .= "";
444
        foreach($w_alts as $a)
445
            $sql .= "{{COL}} LIKE '%".addslashes(utf8_to_charset($a))."%' OR ";
446
        if(isset($e_alts[$i]) && $e_alts[$i]!=$w)
447
            $sql .= " {{COL}} LIKE '%".addslashes($e_alts[$i])."%'";
448
        else {
449
            $sql = substr($sql, 0, strlen($sql)-4);
450
            $sql .= '';
451
        }
452
        $sql .= " $op ";
453
        $i++;
454
    }
455
    $sql = substr($sql, 0, strlen($sql)-5);
456
    $sql_template = $sql;
457

    
458
    // create SQL-string from template
459
    $sql = '(';
460
    foreach($columns as $c) {
461
        $sql .= '(';
462
        $sql .= str_replace('{{COL}}', $c, $sql_template);
463
        $sql .= ")  OR ";
464
    }
465
    $sql = substr($sql, 0, strlen($sql)-4);
466
    $sql .= ')';
467

    
468
    return($sql);
469
}
(5-5/5)