Advertisement
Guest User

Untitled

a guest
Jun 29th, 2012
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3. Plugin Name: Game Server Tracker
  4. Plugin URI: http://hannawerner.com/wordpress/game-server-tracker-version-1-7/
  5. Description: Gets current stats of a Game Server from GameTracker.com and displays them in the sidebar. The Game Server Tracker supports all games that are supported by GameTracker.com.
  6. Version: 1.7
  7. License: GPLv2
  8. Author: Hanna Camille Werner
  9. Author URI: http://www.hannawerner.com
  10. */
  11.  
  12. $alttitle = $instance['alttitle'];
  13.  
  14. add_action('get_header', function(){
  15.   echo
  16. <<<Javascript
  17.   <script type="text/javascript" language="JavaScript">
  18.   <!--
  19.       function HideMoreContent(d) {
  20.           document.getElementById(d).style.display = "none";
  21.       }
  22.       function ShowMoreContent(d) {
  23.           document.getElementById(d).style.display = "block";
  24.       }
  25.       function ReverseDisplay(d) {
  26.           if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; }
  27.           else { document.getElementById(d).style.display = "none"; }
  28.       }
  29.   //-->
  30.   </script>
  31. Javascript;
  32. });
  33.  
  34. class HCW_Gameserver_Tracker extends WP_Widget {
  35.  
  36.     function HCW_Gameserver_Tracker() {
  37.         $widget_ops = array(
  38.             'classname' => 'widget_hcw_gst',
  39.             'description' => __('Displays Game  Server Information')
  40.         );
  41.        
  42.         $control_ops = array();
  43.         $this->WP_Widget(
  44.             'hcwgst',
  45.             __('Game Server Tracker'),
  46.             $widget_ops,
  47.             $control_ops
  48.         );
  49.     }
  50.  
  51.     function widget( $args, $instance ) {
  52.         extract($args);
  53.         $serverip = $instance['serverip'];
  54.        
  55.         echo $before_widget . $before_title;
  56.         echo $instance['alttitle'];
  57.         echo $after_title;
  58.        
  59.         if(class_exists('\Memcached'))
  60.         {
  61.             $m = new Memcached();
  62.             $m->addServer('localhost', 11211);
  63.         }
  64.  
  65.         $cacheKey = 'wp_gst_serverinfo_'.$serverip;
  66.         if(class_exists('\Memcached') && ($cachedValue = $m->get($cacheKey))){
  67.             $dataMapping = $cachedValue;
  68.         }else{
  69.             $content = file_get_contents("http://www.gametracker.com/server_info/$serverip/");
  70.  
  71.             preg_match("/<span class=\"item_color_success\"(.*?)>(.+?)<\/span>/s", $content, $matchesalive);
  72.             preg_match("/<span id=\"HTML_num_players\"(.*?)>(.+?)<\/span>/s", $content, $matchesnumplayers);
  73.             preg_match("/<span id=\"HTML_max_players\"(.*?)>(.+?)<\/span>/s", $content, $matchestotalplayers);
  74.             preg_match("/<span id=\"HTML_num_bots\"(.*?)>(.+?)<\/span>/s", $content, $matchesbots);
  75.             preg_match("/<div class=\"si_map_header\" id=\"HTML_curr_map\"(.*?)>(.+?)<\/div>/s", $content, $matchesmap);
  76.             preg_match("/<span class=\"item_color_title\"(.*?)>Game:<\/span>(.+?)&nbsp;/s", $content, $matchesgame);
  77.             preg_match("/<span class=\"item_color_title\"(.*?)>Name:<\/span>(.+?)<br\/\>/s", $content, $matchesclan);
  78.             preg_match("/<div class=\"si_map_image\" id=\"HTML_map_ss_img\"(.*?)>(.+?)<\/div>/s", $content, $matchesimgmap);
  79.            
  80.             $dataMapping = array(
  81.                 'status'         => trim($matchesalive[2]),
  82.                 'game'           => trim($matchesgame[2]),
  83.                 'bots'           => trim($matchesbots[2]),
  84.                 'player_current' => trim($matchesnumplayers[2]),
  85.                 'player_total'   => trim($matchestotalplayers[2]),
  86.                 'map'            => trim($matchesmap[2]),
  87.                 'map_image'      => $matchesimgmap[2],
  88.                 'clan'           => trim($matchesclan[2]),
  89.             );
  90.  
  91.             if(class_exists('\Memcached'))
  92.                 $m->set(
  93.                     $cacheKey,
  94.                     $dataMapping,
  95.                     60 * 5
  96.                 ); /* Cache for 1 Minute */
  97.         }
  98.      
  99.         echo '<ul>';
  100.        
  101.         echo '<li><strong>Game: </strong>';
  102.         echo $dataMapping['game'];
  103.         echo '</li><li><strong>IP: </strong><a href="http://www.gametracker.com/server_info/';
  104.         echo $serverip;
  105.         echo '/" target="_blank">';
  106.         echo $serverip;
  107.         echo '</a></li><li><strong>Server Status: </strong>';
  108.         echo $dataMapping['status'];
  109.         echo '</li><li><strong>Players: </strong>';
  110.         echo $dataMapping['player_current'];
  111.         echo '/';
  112.         echo $dataMapping['player_total'];
  113.        
  114.         if($dataMapping['bots'] != 0)
  115.         {
  116.             echo '</li><li><strong>Bots: </strong>';
  117.             echo $dataMapping['bots'];
  118.         }
  119.         if(!empty($dataMapping['map']))
  120.             echo '</li><li><strong>Map: </strong>';
  121.    
  122.         ?>
  123.         <?php if(!empty($dataMapping['map']) and strpos($dataMapping['map_image'], 'nomap.jpg') === false): ?>
  124.             <a class="moreserverinfo" href="javascript:ReverseDisplay('<?php echo $serverip; ?>')"><?php echo $dataMapping['map']; ?> &raquo;</a>
  125.  
  126.             <div class="content-sidebar" id="<?php echo $serverip; ?>" style="display:none; padding-top:5px;">
  127.                 <?php echo $dataMapping['map_image']; ?>
  128.             </div>
  129.         <?php elseif(!empty($dataMapping['map'])): ?>
  130.           <?php echo $dataMapping['map']; ?>
  131.         <?php endif; ?>
  132.         <?php
  133.             echo '</li>';
  134.             echo '</ul>';
  135.         ?>
  136.         <?php echo $after_widget;
  137.     }
  138.  
  139.     function update( $new_instance, $old_instance ) {
  140.         $instance = $old_instance;
  141.         $instance['serverip'] = strip_tags($new_instance['serverip']);
  142.         $instance['alttitle'] = strip_tags($new_instance['alttitle']);
  143.         return $instance;
  144.     }
  145.  
  146.     function form( $instance ) {
  147.         $instance = wp_parse_args(
  148.             (array) $instance,
  149.             array(
  150.                 'title' => '',
  151.                 'serverip' => '',
  152.                 'alttitle' => false
  153.             )
  154.         );
  155.  
  156.         if ($instance['serverip'])
  157.             $title = preg_replace('/\?.*/', "", basename($instance['serverip']));
  158.  
  159.         ?>
  160.         <?php ?>
  161.         <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="hidden" value="<?php echo $title; ?>" />
  162.         <p>
  163.             <label for="<?php echo $this->get_field_id('alttitle'); ?>">
  164.                 &nbsp;<?php _e('Server Name:'); ?>
  165.                 <input class="widefat" id="<?php echo $this->get_field_id('alttitle'); ?>" name="<?php echo $this->get_field_name('alttitle'); ?>" type="text" value="<?php echo $instance['alttitle']; ?>" />
  166.            
  167.             </label>
  168.         </p>
  169.  
  170.         <p>
  171.             <label for="<?php echo $this->get_field_id('serverip'); ?>">
  172.                  &nbsp;<?php _e('Server IP and Port:'); ?>
  173.                 <input class="widefat" id="<?php echo $this->get_field_id('serverip'); ?>" name="<?php echo $this->get_field_name('serverip'); ?>" type="text" value="<?php echo $instance['serverip']; ?>" /><br />&nbsp;<small>e.g. 213.239.207.85:27960</small><br />
  174.             </label>
  175.         </p>
  176.     <?php
  177.     }
  178. }
  179.  
  180. function widget_hcw_gst_init() {
  181.     register_widget('HCW_Gameserver_Tracker');
  182. }
  183. add_action('init', 'widget_hcw_gst_init', 1);
  184.  
  185. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement