Autarchy of the Private Cave

Tiny bits of bioinformatics, [web-]programming etc

    • Archives

    • Recent comments

    search-hilite.php file

    2nd October 2006

    1. <?php
    2. /*
    3. Plugin Name: Search_Hilite
    4. Plugin URI: http://www.blog.mediaprojekte.de/cms-systeme/wordpress/wordpress-plugin-search-hilite/
    5. Description: When someone is referred from a search engine like Google, Yahoo, or WordPress' own, the searchterms are highlighted. You can set the markup-style in Options pages or in your own css.  /original Version 1.5 by <a href="http://dev.wp-plugins.org/file/google-highlight/>Ryan Boren </a>. Support for Chinese Language Added by Xuefeng Li
    6. Version: 1.9
    7. Author: Georg Leciejewski
    8. Author URI: http://www.blog.mediaprojekte.de
    9. */
    10. define("SEARCHHILITE", "190");
    11.  
    12. /**
    13. * @desc get search terms as array
    14. * @author Georg Leciejewski
    15. */
    16. function get_search_query_terms($engine = 'google') {
    17.         $referer = urldecode($_SERVER['HTTP_REFERER']);
    18.         $query_array = array();
    19.         switch ($engine) {
    20.         case 'google':
    21.                 // Google query parsing code adapted from Dean Allen's
    22.                 // Google Hilite 0.3. http://textism.com
    23.                 $query_terms = preg_replace('/^.*q=([^&]+)&?.*$/i','$1', $referer);
    24.                 $query_terms = preg_replace('/\'|"/', '', $query_terms);
    25.                 $query_array = preg_split ("/[\s,\+\.]+/", $query_terms);
    26.                 break;
    27.         //added by  Xuefeng Li        
    28.         case 'baidu':
    29.         // My server don't support iconv function, so i had to use another function gb2utf8 to replace it.
    30.             //$referer = iconv('gb2312', 'utf-8', $referer);
    31.                 $referer =  gb2utf8($referer);
    32.                 $query_terms = preg_replace('/^.*(wd=|word=)([^&]+)&?.*$/i','$2', $referer);
    33.                 $query_terms = preg_replace('/\'|"/', '', $query_terms);
    34.                 $query_array = preg_split ("/[\s,\+\.]+/", $query_terms);
    35.                 break;
    36.  
    37.         case 'lycos':
    38.                 $query_terms = preg_replace('/^.*query=([^&]+)&?.*$/i','$1', $referer);
    39.                 $query_terms = preg_replace('/\'|"/', '', $query_terms);
    40.                 $query_array = preg_split ("/[\s,\+\.]+/", $query_terms);
    41.                 break;
    42.  
    43.         case 'yahoo':
    44.                 $query_terms = preg_replace('/^.*p=([^&]+)&?.*$/i','$1', $referer);
    45.                 $query_terms = preg_replace('/\'|"/', '', $query_terms);
    46.                 $query_array = preg_split ("/[\s,\+\.]+/", $query_terms);
    47.                 break;
    48.                
    49.         case 'wordpress':
    50.                 $search = get_query_var('s');
    51.                 $search_terms = get_query_var('search_terms');
    52.  
    53.                 if (!empty($search_terms)) {
    54.                         $query_array = $search_terms;
    55.                 } else if (!empty($search)) {
    56.                         $query_array = array($search);
    57.                 } else if (empty($search)) {
    58.                     //do nothing man
    59.                 } else {
    60.                         $query_terms = preg_replace('/^.*s=([^&]+)&?.*$/i','$1', $referer);
    61.                         $query_terms = preg_replace('/\'|"/', '', $query_terms);
    62.                         $query_array = preg_split ("/[\s,\+\.]+/", $query_terms);
    63.                 }
    64.         }
    65.        
    66.         return $query_array;
    67. }
    68. /**
    69. * @desc get textfile to make chinese character conversion
    70. * @author Xuefeng Li
    71. */
    72. function gb2utf8($text) {
    73.    $charset[0]=0;
    74.    $filename = dirname(__FILE__).'/'."gb2utf8.txt";
    75.    $fp = fopen($filename,"r");
    76.    while(! feof($fp)) {
    77.       list($gb,$utf8) = fgetcsv($fp,10);
    78.       $charset[$gb] = $utf8;
    79.    }
    80.    fclose($fp);
    81.  
    82.    preg_match_all("/(?:[\x80-\xff].)|[\x01-\x7f]+/",$text,$tmp);
    83.    $tmp = $tmp[0];
    84.    $ar = array_intersect($tmp, array_keys($charset));
    85.    foreach($ar as $k=>$v)
    86.     $tmp[$k] = $charset[$v];
    87.    return join('',$tmp);
    88. }
    89. /**
    90. * @desc check from which searchengine
    91. * @author Georg Leciejewski
    92. */
    93. function is_referer_search_engine($engine = 'google') {
    94.         if( empty($_SERVER['HTTP_REFERER']) && 'wordpress' != $engine ) {
    95.                 return false;
    96.         }
    97.  
    98.         $referer = urldecode($_SERVER['HTTP_REFERER']);
    99.  
    100.         if ( ! $engine ) {
    101.                 return false;
    102.         }
    103.  
    104.         switch ($engine) {
    105.         case 'google':
    106.                 if (preg_match('|^http://(www)?\.?google.*|i', $referer)) {
    107.                         return true;
    108.                 }
    109.                 break;
    110.  
    111.         case 'lycos':
    112.                 if (preg_match('|^http://search\.lycos.*|i', $referer)) {
    113.                         return true;
    114.                 }
    115.                 break;
    116.  
    117.         case 'yahoo':
    118.                 if (preg_match('|^http://search\.yahoo.*|i', $referer)) {
    119.                         return true;
    120.                 }
    121.                 break;
    122.         //added by  Xuefeng Li
    123.         case 'baidu':
    124.                 if (preg_match('|^http://(www)?\.?baidu.com|i', $referer)) {
    125.                         return true;
    126.                 }
    127.                 break;
    128.         case 'wordpress':
    129.                 if ( is_search() )
    130.                         return true;
    131.  
    132.                 $siteurl = get_option('home');
    133.                 if (preg_match("#^$siteurl#i", $referer))
    134.                         return true;
    135.  
    136.                 break;
    137.         }
    138.  
    139.         return false;
    140. }
    141.  
    142. /**
    143. * @desc Hiliting Conversion
    144. * @author Georg Leciejewski
    145. */
    146. function hilite($text)
    147. {
    148.     $search_engines = array('wordpress', 'google', 'lycos', 'yahoo', 'baidu');
    149.     $loopcounter=0;
    150.    foreach ($search_engines as $engine)
    151.     {
    152.          if ( is_referer_search_engine($engine))
    153.             {
    154.                $query_terms = get_search_query_terms($engine);
    155.                foreach ($query_terms as $term)
    156.                     {
    157.                         if ( !empty($term) && $term != ' ' && strlen($term) >= 3 )
    158.                         {
    159.                             $loopcounter++;
    160.                             if ( $loopcounter == 1) $hilite_class='hilite';
    161.                             elseif($loopcounter == 2) $hilite_class='hilite1';
    162.                             elseif($loopcounter == 3) $hilite_class='hilite2';
    163.                             elseif($loopcounter == 4) $hilite_class='hilite3';
    164.  
    165.                             $qterm = preg_quote($term, '/');
    166.                             if ( preg_match("/<&#91;^>]*?>/s",$text) != 0 )
    167.                                 $text = preg_replace('/('.$qterm.')/i','<span class="'.$hilite_class.'">$1</span>',$text);
    168.                             else
    169.                             {
    170.                                 $regex = '/(>[^<>]*?)('.$qterm.')([^>]*?<)/i';
    171. error_log( htmlspecialchars($text) );
    172. error_log( "regex='$regex',qterm='$qterm'" );
    173.                                 $text = preg_replace($regex,'$1<span class="'.$hilite_class.'">$2</span>$3',$text);
    174.                                 /* (?![^<&#93;*?>) */
    175. //                              $text = preg_replace('/(?<=>)([^<&#93;+)?('.$term.')/i','$1<span class="'.$hilite_class.'">$2</span>',$text);  //taken out the \b option to also mark substrings
    176.                             }
    177.                         }
    178.                }
    179.                break;
    180.          }
    181.    }
    182.    return $text;
    183. }
    184.  
    185. /**
    186. * @desc insert hilite css into head
    187. * @author Georg Leciejewski
    188. */
    189. function hilite_head() {
    190.  
    191.     $css = search_hilite_getCss();
    192.  
    193.      if(get_option('search_hilite_use_own_css')=='1')
    194.      {
    195.      }   
    196.      elseif (!empty ($css) )
    197.      {
    198.       echo $css ;
    199.  
    200.      }else{
    201.        echo "
    202.       <style type='text/css'>
    203.      .hilite {
    204.             color: #000;
    205.             background-color: #99ccff;
    206.             }
    207.    .hilite1 {
    208.             color: #000;
    209.             background-color: #ffcc66;
    210.             }
    211.    .hilite2 {
    212.             color: #000;
    213.             background-color: #99ff66;
    214.             }
    215.    .hilite3 {
    216.             color: #000;
    217.             background-color: #ff9999;
    218.             }
    219.     </style>";
    220.      }
    221. }
    222.  
    223. /**
    224. * @desc Admin menu Page
    225. * @author Georg Leciejewski
    226. */
    227. function search_hilite_options() {
    228.  
    229.     if($_POST['search_hilite_options_save']){
    230.      update_option('search_hilite_css',$_POST['search_hilite_css']);
    231.      update_option('search_hilite_use_own_css',$_POST['search_hilite_use_own_css']);
    232.         echo '<div class="updated"><p>'. __('Search Hilite Options successfully saved.','searchHilite') .'</p></div>';
    233.     }
    234.     ?>
    235.     <div class="wrap">
    236.     <h2><?php _e('Search Hilite CSS Options','searchHilite') ?> </h2>
    237.     <form method="post" id="search_hilite_options" action="">
    238.         <fieldset class="options">
    239.         <legend><?php _e('Insert your own Hilite CSS Style','searchHilite') ?></legend>
    240.         <table width="100%" cellspacing="2" cellpadding="5" class="editform">
    241.         <tr valign="top">
    242.                 <th width="33%" scope="row"><?php _e('take CSS Style from my Stylesheet:','searchHilite') ?></th>
    243.                 <td>
    244.                <input type="checkbox" name="search_hilite_use_own_css" value='1' <?php if(get_option('search_hilite_use_own_css')=='1') { echo "checked='checked'";  } ?> >
    245. <br /><?php _e('Should be set if you inserted the hilite style in your existing css. If not set and Style Field below is empty. The default style (example) will be used.
    246. ','searchHilite') ?>
    247.               </td>
    248.             </tr>
    249.             <tr valign="top">
    250.                 <th width="33%" scope="row"><?php _e('CSS Style:','searchHilite') ?></th>
    251.                 <td>
    252.                 <textarea rows="10" cols="30" name="search_hilite_css" tabindex="4" id="search_hilite_css"><?php echo stripslashes(get_option('search_hilite_css')) ;  ?>
    253.                 </textarea>
    254.                <br /><br /><strong><?php _e('Example:','searchHilite') ?></strong> <br />
    255.  
    256.                <?php _e('CSS Style:','searchHilite') ?>
    257.                <?php
    258.                echo "<br />
    259.                 &lt;style type='text/css'&gt;<br />
    260.                 .hilite {<br />
    261.                 color: #000;<br />
    262.                 background-color: #;<br />
    263.                 }<br />
    264.                 .hilite1 {<br />
    265.                 color: #000;<br />
    266.                 background-color: #99FF99;<br />
    267.                 }<br />
    268.                 .hilite2 {<br />
    269.                 color: #000;<br />
    270.                 background-color: #FFFF66;<br />
    271.                 }<br />
    272.                 .hilite3 {<br />
    273.                 color: #000;<br />
    274.                 background-color: #FF66FF;<br />
    275.                 }<br />
    276.                 &lt;/style&gt;
    277.                 ";
    278.                 ?>
    279.                 </td>
    280.             </tr>
    281.         </table>
    282.         <p class="submit"><input type="submit" name="search_hilite_options_save" value="Save" /></p>
    283.         </fieldset>
    284.     </form>
    285.     </div>
    286. <?php
    287. }
    288.  
    289. /**
    290. * @desc get search_hilite_css
    291. * @author Georg Leciejewski
    292. */
    293. function search_hilite_getCss(){
    294.     if($css = get_option('search_hilite_css')){
    295.        $css = stripslashes($css) ;
    296.         return $css;
    297.     }
    298. }
    299.  
    300. /**
    301. * @desc look for old google_hilite_css option and removes them just for security if someone uses the old plugin
    302. * @author Georg Leciejewski
    303. */
    304. function search_hilite_install(){
    305.     if(get_option('google_hilite_css')){
    306.       delete_option ('google_hilite_css');
    307.     }
    308.  
    309.     add_option('search_hilite_css', " <style type='text/css'>
    310.     .hilite {
    311.             color: #000;
    312.             background-color: #99ccff;
    313.             }
    314.    .hilite1 {
    315.             color: #000;
    316.             background-color: #ffcc66;
    317.             }
    318.    .hilite2 {
    319.             color: #000;
    320.             background-color: #99ff66;
    321.             }
    322.    .hilite3 {
    323.             color: #000;
    324.             background-color: #ff9999;
    325.             }
    326.     </style>");
    327.     add_option('search_hilite_use_own_css', "0");
    328. }
    329.  
    330. /**
    331. * @desc Admin Menu hook
    332. * @author Georg Leciejewski
    333. */
    334. function search_hilite_adminmenu(){
    335.     add_options_page('Search Hilite Options', 'Search Hilite', 9, 'search-hilite.php', 'search_hilite_options');
    336. }
    337.  
    338. /**
    339. * @desc Remote Version checking
    340. * @author georg leciejewski /  Per Soderlind
    341. * @param string $widgetname full Widgetname ie: King_Text_Widget
    342. * @param int $localversion widget Version Number
    343. */
    344. function search_hilite_remote_version_check($widgetname,$localversion) {
    345.     require_once(ABSPATH . WPINC . '/class-snoopy.php');
    346.     if (class_exists(snoopy)) {
    347.         $client = new Snoopy();
    348.         $client->_fp_timeout = 10;
    349.         if (@$client->fetch('http://website-king.com/versiontrack/'.$widgetname.'.txt') === false) {
    350.             return -1;
    351.         }
    352.         $remote = $client->results;
    353.         if (!$remote || strlen($remote) > 8 ) {
    354.             return -1;
    355.         }
    356.         if (intval($remote) > intval($localversion)) {
    357.             return 1;
    358.         } else {
    359.             return 0;
    360.         }
    361.     }
    362. }
    363.  
    364. /**
    365. * @desc Admin Header for Version Check on Pluginpage
    366. * @author georg leciejewski /  Per Soderlind
    367. * @param string $widgetname full Widgetname ie: King_Text_Widget
    368. * @param int $localversion widget Version Number
    369. */
    370. function search_hilite_admin_head($widgetname,$localversion) {
    371.     load_plugin_textdomain('searchHilite');
    372.     if ((strpos($_SERVER['REQUEST_URI'], 'plugins.php') !== false) && (search_hilite_remote_version_check($widgetname,$localversion) == 1)) {
    373.         echo "<script type='text/javascript' src='" . get_settings('siteurl') . "/wp-content/plugins/king-includes/js/prototype-1.4.0.js'></script>\n";
    374.  
    375.         $alert = "\n";
    376.         $alert .= "\n<script type='text/javascript'>";
    377.         $alert .= "\n//<!&#91;CDATA&#91;";
    378.         $alert .= "\nfunction alertNewVersion" . $widgetname . "() {";
    379.         $alert .= "\n   pluginname = '" . $widgetname . "';";
    380.         $alert .= "\n   allNodes = document.getElementsByClassName('name');";
    381.         $alert .= "\n   for(i = 0; i < allNodes.length; i++) {";
    382.         $alert .= "\n           var regExp=/<\S&#91;^>&#93;*>/g;";
    383.         $alert .= "\n       temp = allNodes&#91;i&#93;.innerHTML;";
    384.         $alert .= "\n       if (temp.replace(regExp,'') == pluginname) {";
    385.         $alert .= "\n           Element.setStyle(allNodes&#91;i&#93;.getElementsByTagName('a')&#91;0&#93;, {color: '#f00'});";
    386.         $alert .= "\n           new Insertion.After(allNodes&#91;i&#93;.getElementsByTagName('strong')&#91;0&#93;,'<br/><small>" .  __("new version available","widgetKing") . "</small>');";
    387.         $alert .= "\n       }";
    388.         $alert .= "\n   }";
    389.         $alert .= "\n}";
    390.         $alert .= "\naddLoadEvent(alertNewVersion" . $widgetname . ");";
    391.         $alert .= "\n//&#93;&#93;>";
    392.         $alert .= "\n</script>";
    393.         $alert .= "\n";
    394.         echo $alert;
    395.     }
    396. }
    397. /**
    398. * @desc Version Check Heading
    399. * @author Georg Leciejewski
    400. */
    401. function search_hilite_version() {
    402.  
    403.     search_hilite_admin_head('Search_Hilite',SEARCHHILITE);
    404. }
    405.  
    406. if (isset($_GET['activate']) && $_GET['activate'] == 'true') {
    407.     add_action('init', 'search_hilite_install');
    408.     }
    409.  
    410. add_action('admin_head','search_hilite_version');
    411. add_action('admin_menu','search_hilite_adminmenu',1);
    412.  
    413. // Highlight text and comments:
    414. add_filter('the_content', 'hilite');
    415. add_filter('the_excerpt', 'hilite');
    416. add_filter('comment_text', 'hilite');
    417. add_action('wp_head', 'hilite_head');
    418. add_filter('the_title', 'hilite');
    419.  
    420. ?>
    Share

    Leave a Reply

    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>