Autarchy of the Private Cave

Tiny bits of bioinformatics, [web-]programming etc

  • Exits

  • Categories

  • Archives

  • Tags list

    search-hilite.php file

    2nd October 2006

    PHP:
    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 <a href="http://blog.taglife.net/2006/06/28/121/" rel="external">Xuefeng Li</a>
    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("/<[^>]*?>/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( "regex='$regex',qterm='$qterm'" );
    172.                                 $text = preg_replace($regex,'$1<span class="'.$hilite_class.'">$2</span>$3',$text);
    173.                                 /* (?![^<]*?>) */
    174. //                $text = preg_replace('/(?<=>)([^<]+)?('.$term.')/i','$1<span class="'.$hilite_class.'">$2</span>',$text);  //taken out the \b option to also mark substrings
    175.                             }
    176.                         }
    177.                }
    178.                break;
    179.          }
    180.    }
    181.    return $text;
    182. }
    183.  
    184. /**
    185. * @desc insert hilite css into head
    186. * @author Georg Leciejewski
    187. */
    188. function hilite_head() {
    189.  
    190.     $css = search_hilite_getCss();
    191.  
    192.      if(get_option('search_hilite_use_own_css')=='1')
    193.      {
    194.      }  
    195.      elseif (!empty ($css) )
    196.      {
    197.       echo $css ;
    198.  
    199.      }else{
    200.        echo "
    201.        <style type='text/css'>
    202.       .hilite {
    203.             color: #000;
    204.             background-color: #99ccff;
    205.             }
    206.     .hilite1 {
    207.             color: #000;
    208.             background-color: #ffcc66;
    209.             }
    210.     .hilite2 {
    211.             color: #000;
    212.             background-color: #99ff66;
    213.             }
    214.     .hilite3 {
    215.             color: #000;
    216.             background-color: #ff9999;
    217.             }
    218.     </style>";
    219.      }
    220. }
    221.  
    222. /**
    223. * @desc Admin menu Page
    224. * @author Georg Leciejewski
    225. */
    226. function search_hilite_options() {
    227.  
    228.     if($_POST['search_hilite_options_save']){
    229.      update_option('search_hilite_css',$_POST['search_hilite_css']);
    230.      update_option('search_hilite_use_own_css',$_POST['search_hilite_use_own_css']);
    231.         echo '<div class="updated"><p>'. __('Search Hilite Options successfully saved.','searchHilite') .'</p></div>';
    232.     }
    233.     ?>
    234.     <div class="wrap">
    235.     <h2><?php _e('Search Hilite CSS Options','searchHilite') ?> </h2>
    236.     <form method="post" id="search_hilite_options" action="">
    237.         <fieldset class="options">
    238.         <legend><?php _e('Insert your own Hilite CSS Style','searchHilite') ?></legend>
    239.         <table width="100%" cellspacing="2" cellpadding="5" class="editform">
    240.         <tr valign="top">
    241.                 <th width="33%" scope="row"><?php _e('take CSS Style from my Stylesheet:','searchHilite') ?></th>
    242.                 <td>
    243.                <input type="checkbox" name="search_hilite_use_own_css" value='1' <?php if(get_option('search_hilite_use_own_css')=='1') { echo "checked='checked'"} ?>>
    244. <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.
    245. ','searchHilite') ?>
    246.               </td>
    247.             </tr>
    248.             <tr valign="top">
    249.                 <th width="33%" scope="row"><?php _e('CSS Style:','searchHilite') ?></th>
    250.                 <td>
    251.                 <textarea rows="10" cols="30" name="search_hilite_css" tabindex="4" id="search_hilite_css"><?php echo stripslashes(get_option('search_hilite_css'))?>
    252.                 </textarea>
    253.                <br /><br /><strong><?php _e('Example:','searchHilite') ?></strong> <br />
    254.  
    255.                <?php _e('CSS Style:','searchHilite') ?>
    256.                <?php
    257.                echo "<br />
    258.                  &lt;style type='text/css'&gt;<br />
    259.                 .hilite {<br />
    260.                 color: #000;<br />
    261.                 background-color: #;<br />
    262.                 }<br />
    263.                 .hilite1 {<br />
    264.                 color: #000;<br />
    265.                 background-color: #99FF99;<br />
    266.                 }<br />
    267.                 .hilite2 {<br />
    268.                 color: #000;<br />
    269.                 background-color: #FFFF66;<br />
    270.                 }<br />
    271.                 .hilite3 {<br />
    272.                 color: #000;<br />
    273.                 background-color: #FF66FF;<br />
    274.                 }<br />
    275.                 &lt;/style&gt;
    276.                 ";
    277.                 ?>
    278.                 </td>
    279.             </tr>
    280.         </table>
    281.         <p class="submit"><input type="submit" name="search_hilite_options_save" value="Save" /></p>
    282.         </fieldset>
    283.     </form>
    284.     </div>
    285. <?php
    286. }
    287.  
    288. /**
    289. * @desc get search_hilite_css
    290. * @author Georg Leciejewski
    291. */
    292. function search_hilite_getCss(){
    293.     if($css = get_option('search_hilite_css')){
    294.        $css = stripslashes($css) ;
    295.         return $css;
    296.     }
    297. }
    298.  
    299. /**
    300. * @desc look for old google_hilite_css option and removes them just for security if someone uses the old plugin
    301. * @author Georg Leciejewski
    302. */
    303. function search_hilite_install(){
    304.     if(get_option('google_hilite_css')){
    305.       delete_option ('google_hilite_css');
    306.     }
    307.  
    308.     add_option('search_hilite_css', " <style type='text/css'>
    309.     .hilite {
    310.             color: #000;
    311.             background-color: #99ccff;
    312.             }
    313.     .hilite1 {
    314.             color: #000;
    315.             background-color: #ffcc66;
    316.             }
    317.     .hilite2 {
    318.             color: #000;
    319.             background-color: #99ff66;
    320.             }
    321.     .hilite3 {
    322.             color: #000;
    323.             background-color: #ff9999;
    324.             }
    325.     </style>");
    326.     add_option('search_hilite_use_own_css', "0");
    327. }
    328.  
    329. /**
    330. * @desc Admin Menu hook
    331. * @author Georg Leciejewski
    332. */
    333. function search_hilite_adminmenu(){
    334.     add_options_page('Search Hilite Options', 'Search Hilite', 9, 'search-hilite.php', 'search_hilite_options');
    335. }
    336.  
    337. /**
    338. * @desc Remote Version checking
    339. * @author georg leciejewski /  Per Soderlind
    340. * @param string $widgetname full Widgetname ie: King_Text_Widget
    341. * @param int $localversion widget Version Number
    342. */
    343. function search_hilite_remote_version_check($widgetname,$localversion) {
    344.     require_once(ABSPATH . WPINC . '/class-snoopy.php');
    345.     if (class_exists(snoopy)) {
    346.         $client = new Snoopy();
    347.         $client->_fp_timeout = 10;
    348.         if (@$client->fetch('http://website-king.com/versiontrack/'.$widgetname.'.txt') === false) {
    349.             return -1;
    350.         }
    351.         $remote = $client->results;
    352.      if (!$remote || strlen($remote)> 8 ) {
    353.             return -1;
    354.         }
    355.         if (intval($remote)> intval($localversion)) {
    356.             return 1;
    357.         } else {
    358.             return 0;
    359.         }
    360.     }
    361. }
    362.  
    363. /**
    364. * @desc Admin Header for Version Check on Pluginpage
    365. * @author georg leciejewski /  Per Soderlind
    366. * @param string $widgetname full Widgetname ie: King_Text_Widget
    367. * @param int $localversion widget Version Number
    368. */
    369. function search_hilite_admin_head($widgetname,$localversion) {
    370.     load_plugin_textdomain('searchHilite');
    371.     if ((strpos($_SERVER['REQUEST_URI'], 'plugins.php') !== false) && (search_hilite_remote_version_check($widgetname,$localversion) == 1)) {
    372.         echo "<script type='text/javascript' src='" . get_settings('siteurl') . "/wp-content/plugins/king-includes/js/prototype-1.4.0.js'></script>\n";
    373.  
    374.         $alert = "\n";
    375.         $alert .= "\n<script type='text/javascript'>";
    376.         $alert .= "\n//<![CDATA[";
    377.         $alert .= "\nfunction alertNewVersion" . $widgetname . "() {";
    378.         $alert .= "\n   pluginname = '" . $widgetname . "';";
    379.         $alert .= "\n   allNodes = document.getElementsByClassName('name');";
    380.         $alert .= "\n   for(i = 0; i <allNodes.length; i++) {";
    381.         $alert .= "\n         var regExp=/<\S[^>]*>/g;";
    382.         $alert .= "\n       temp = allNodes[i].innerHTML;";
    383.         $alert .= "\n       if (temp.replace(regExp,'') == pluginname) {";
    384.         $alert .= "\n          Element.setStyle(allNodes[i].getElementsByTagName('a')[0], {color: '#f00'});";
    385.         $alert .= "\n          new Insertion.After(allNodes[i].getElementsByTagName('strong')[0],'<br/><small>" .  __("new version available","widgetKing") . "</small>');";
    386.         $alert .= "\n      }";
    387.         $alert .= "\n   }";
    388.         $alert .= "\n}";
    389.         $alert .= "\naddLoadEvent(alertNewVersion" . $widgetname . ");";
    390.         $alert .= "\n//]]>";
    391.         $alert .= "\n</script>";
    392.         $alert .= "\n";
    393.         echo $alert;
    394.     }
    395. }
    396. /**
    397. * @desc Version Check Heading
    398. * @author Georg Leciejewski
    399. */
    400. function search_hilite_version() {
    401.  
    402.     search_hilite_admin_head('Search_Hilite',SEARCHHILITE);
    403. }
    404.  
    405. if (isset($_GET['activate']) && $_GET['activate'] == 'true') {
    406.     add_action('init', 'search_hilite_install');
    407.     }
    408.  
    409. add_action('admin_head','search_hilite_version');
    410. add_action('admin_menu','search_hilite_adminmenu',1);
    411.  
    412. // Highlight text and comments:
    413. add_filter('the_content', 'hilite');
    414. add_filter('the_excerpt', 'hilite');
    415. add_filter('comment_text', 'hilite');
    416. add_action('wp_head', 'hilite_head');
    417. add_filter('the_title', 'hilite');
    418.  
    419. ?>

    • Delicious
    • Google Bookmarks
    • Yahoo Bookmarks
    • Windows Live Favorites
    • Technorati Favorites
    • Digg
    • Slashdot
    • StumbleUpon
    • Read It Later
    • Twitter
    • Share/Bookmark

    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>