Advertisement
illpastethat

Wordpress IRC plugin kirby3

Jan 26th, 2013
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.45 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: WP IRC
  4. Plugin Script: wp-irc.php
  5. Plugin URI: http://sudarmuthu.com/wordpress/wp-irc
  6. Description: Retrieves the number of people who are online in an IRC Channel, which can be displayed in the sidebar using a widget.
  7. Version: 1.0
  8. License: GPL
  9. Author: Sudar
  10. Author URI: http://sudarmuthu.com/
  11.  
  12. === RELEASE NOTES ===
  13. 2009-07-29 - v0.1 - first version
  14. 2012-01-31 - v0.2 - Fixed issue with textarea in the widget
  15. 2013-01-21 - v1.0 - (Dev Time: 20 hours)
  16.                   - Complete rewrite and added support for AJAX with caching
  17. */
  18. /*  Copyright 2009  Sudar Muthu  (email : sudar@sudarmuthu.com)
  19.  
  20.     This program is free software; you can redistribute it and/or modify
  21.     it under the terms of the GNU General Public License, version 2, as
  22.     published by the Free Software Foundation.
  23.  
  24.     This program is distributed in the hope that it will be useful,
  25.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  26.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  27.     GNU General Public License for more details.
  28.  
  29.     You should have received a copy of the GNU General Public License
  30.     along with this program; if not, write to the Free Software
  31.     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  32. */
  33.  
  34. //TODO: Create a settings page where people can test connection
  35. //TODO: Add support for alerts
  36. //TODO: Add support for shortcode
  37.  
  38. // so that the script doesn't timeout
  39. set_time_limit(0);
  40.  
  41. /**
  42.  * The main Plugin class
  43.  *
  44.  * @package WP IRC
  45.  * @author Sudar
  46.  */
  47. class WP_IRC {
  48.  
  49.     private $version = "0.3";
  50.     private $js_handle = "wp-irc";
  51.     private $js_variable = "WPIRC";
  52.     private $refresh_nonce = "wp-irc-refresh-count";
  53.  
  54.     /**
  55.      * Initalize the plugin by registering the hooks
  56.      */
  57.     function __construct() {
  58.  
  59.         // Load localization domain
  60.         load_plugin_textdomain( 'wp-irc', false, dirname(plugin_basename(__FILE__)) .  '/languages' );
  61.  
  62.         // Register hooks
  63.         add_action('wp_enqueue_scripts', array(&$this, 'add_script'));
  64.        
  65.         add_action('wp_ajax_refresh_count', array(&$this, 'refresh_count'));
  66.         add_action('wp_ajax_nopriv_refresh_count', array(&$this, 'refresh_count'));
  67.     }
  68.  
  69.     /**
  70.      * Refresh count for a widget
  71.      *
  72.      * @return void
  73.      */
  74.     function refresh_count() {
  75.            
  76.         if ( ! wp_verify_nonce( $_POST['nonce'], $this->refresh_nonce ) ) {
  77.             die ( 'Are you trying something funny?');
  78.         }
  79.  
  80.         header( "Content-Type: application/json" );
  81.  
  82.         $widget_id = absint($_POST['widget_id']);
  83.         $option = get_option('widget_irc_widget');
  84.         $instance = $option[$widget_id];
  85.  
  86.         if (false === ($count = get_transient($this->js_handle . $widget_id))) {
  87.             $count = IRC::get_irc_channel_users($instance['server'], $instance['port'], $instace['channel'], $instance['nickname']);
  88.             set_transient($this->js_handle . $widget_id, $count, $intance['interval'] * 60);
  89.         }
  90.  
  91.         $content = str_replace("[count]", $count, $instance['content']);
  92.         $content = str_replace("[channel]", $instance["channel"], $content);
  93.         $content = str_replace("[server]", $instance["server"], $content);
  94.  
  95.         // generate the response
  96.         $response = json_encode( array( 'success' => true, 'content' => $content ) );
  97.  
  98.         echo $response;
  99.    
  100.         exit;
  101.     }
  102.  
  103.     /**
  104.      * Add the requried JavaScript files
  105.      */
  106.     function add_script() {
  107.         wp_enqueue_script($this->js_handle, plugins_url('/js/wp-irc.js', __FILE__), array('jquery'), $this->version, TRUE);
  108.  
  109.         // JavaScript messages
  110.         $msg = array(
  111.             'refreshcountfailed' => __('Unable to fetch user count. Kindly try after sometime', 'wp-irc')
  112.         );
  113.         $translation_array = array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'refreshNonce' => wp_create_nonce($this->refresh_nonce), 'msg' => $msg );
  114.         wp_localize_script( $this->js_handle, $this->js_variable, $translation_array );
  115.     }
  116. }
  117.  
  118. // Start this plugin once all other plugins are fully loaded
  119. add_action( 'init', 'WP_IRC' ); function WP_IRC() { global $WP_IRC; $WP_IRC = new WP_IRC(); }
  120.  
  121. /**
  122.  * Adds IRC_Widget widget
  123.  *
  124.  * @package default
  125.  * @author Sudar
  126.  */
  127. class IRC_Widget extends WP_Widget {
  128.  
  129.     /**
  130.      * Register widget with WordPress.
  131.      */
  132.     public function __construct() {
  133.         parent::__construct(
  134.             'irc_widget', // Base ID
  135.             'IRC_Widget', // Name
  136.             array( 'description' => __( 'An IRC Widget', 'wp-irc' ), ) // Args
  137.         );
  138.     }
  139.  
  140.     /**
  141.      * Front-end display of widget.
  142.      *
  143.      * @see WP_Widget::widget()
  144.      *
  145.      * @param array $args     Widget arguments.
  146.      * @param array $instance Saved values from database.
  147.      */
  148.     public function widget( $args, $instance ) {
  149.         extract( $args );
  150.         $title = apply_filters( 'widget_title', $instance['title'] );
  151.  
  152.         echo $before_widget;
  153.         if ( ! empty( $title ) ) {
  154.             echo $before_title . $title . $after_title;
  155.         }
  156.         $this->getWidgetContent($instance);
  157.         echo $after_widget;
  158.     }
  159.  
  160.     /**
  161.      * Get the content for the widget
  162.      *
  163.      */
  164.     private function getWidgetContent($instance) {
  165. ?>
  166.         <div id = "<?php echo $this->id; ?>" class = "irc_widget_id">
  167.             <img src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" class="ajax-loading list-ajax-loading" alt="content-loading" />
  168.         </div>
  169. <?php      
  170.     }
  171.  
  172.     /**
  173.      * Sanitize widget form values as they are saved.
  174.      *
  175.      * @see WP_Widget::update()
  176.      *
  177.      * @param array $new_instance Values just sent to be saved.
  178.      * @param array $old_instance Previously saved values from database.
  179.      *
  180.      * @return array Updated safe values to be saved.
  181.      */
  182.     public function update( $new_instance, $old_instance ) {
  183.         $instance = array();
  184.  
  185.         $instance['title'] = strip_tags( $new_instance['title'] );
  186.         $instance['server'] = strip_tags( $new_instance['server'] );
  187.         $instance['port'] = intval( $new_instance['port'] );
  188.         $instance['channel'] = strip_tags( $new_instance['channel'] );
  189.         $instance['nickname'] = strip_tags( $new_instance['nickname'] );
  190.         $instance['content'] = ( $new_instance['content'] );
  191.         $instance['interval'] = intval( $new_instance['interval'] );
  192.  
  193.         return $instance;
  194.     }
  195.  
  196.     /**
  197.      * Back-end widget form.
  198.      *
  199.      * @see WP_Widget::form()
  200.      *
  201.      * @param array $instance Previously saved values from database.
  202.      */
  203.     public function form( $instance ) {
  204.         if ( isset( $instance[ 'title' ] ) ) {
  205.             $title = $instance[ 'title' ];
  206.         } else {
  207.             $title = __( 'New title', 'wp-irc' );
  208.         }
  209.  
  210.         if ( isset( $instance[ 'content' ] ) ) {
  211.             $content = $instance[ 'content' ];
  212.         } else {
  213.             $content = __( 'There are currently [count] people in [channel]', 'wp-irc' );
  214.         }
  215.  
  216.         if ( isset( $instance[ 'server' ] ) ) {
  217.             $server = $instance[ 'server' ];
  218.         } else {
  219.             $server = __( 'irc.freenode.net', 'wp-irc' );
  220.         }
  221.  
  222.         if ( isset( $instance[ 'port' ] ) ) {
  223.             $port = $instance[ 'port' ];
  224.         } else {
  225.             $port = __( '6667', 'wp-irc' );
  226.         }
  227.  
  228.         if ( isset( $instance[ 'channel' ] ) ) {
  229.             $channel = $instance[ 'channel' ];
  230.         } else {
  231.             $channel = __( 'wordpress', 'wp-irc' );
  232.         }
  233.  
  234.         if ( isset( $instance[ 'nickname' ] ) ) {
  235.             $nickname = $instance[ 'nickname' ];
  236.         } else {
  237.             $nickname = __( 'wp-irc-bot', 'wp-irc' );
  238.         }
  239.  
  240.         if ( isset( $instance[ 'interval' ] ) ) {
  241.             $interval = $instance[ 'interval' ];
  242.         } else {
  243.             $interval = __( '5', 'wp-irc' );
  244.         }
  245.  
  246. ?>
  247.         <p>
  248.         <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
  249.         <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
  250.         </p>
  251.  
  252.         <p>
  253.         <label for="<?php echo $this->get_field_id( 'server' ); ?>"><?php _e( 'Server:' ); ?></label>
  254.         <input class="widefat" id="<?php echo $this->get_field_id( 'server' ); ?>" name="<?php echo $this->get_field_name( 'server' ); ?>" type="text" value="<?php echo esc_attr( $server ); ?>" />
  255.         </p>
  256.  
  257.         <p>
  258.         <label for="<?php echo $this->get_field_id( 'port' ); ?>"><?php _e( 'Port:' ); ?></label>
  259.         <input class="widefat" id="<?php echo $this->get_field_id( 'port' ); ?>" name="<?php echo $this->get_field_name( 'port' ); ?>" type="text" value="<?php echo esc_attr( $port ); ?>" />
  260.         </p>
  261.  
  262.         <p>
  263.         <label for="<?php echo $this->get_field_id( 'channel' ); ?>"><?php _e( 'Channel:' ); ?></label>
  264.         #<input class="widefat" id="<?php echo $this->get_field_id( 'channel' ); ?>" name="<?php echo $this->get_field_name( 'channel' ); ?>" type="text" value="<?php echo esc_attr( $channel ); ?>" />
  265.         </p>
  266.  
  267.         <p>
  268.         <label for="<?php echo $this->get_field_id( 'nickname' ); ?>"><?php _e( 'Nickname:' ); ?></label>
  269.         <input class="widefat" id="<?php echo $this->get_field_id( 'nickname' ); ?>" name="<?php echo $this->get_field_name( 'nickname' ); ?>" type="text" value="<?php echo esc_attr( $nickname ); ?>" />
  270.         </p>
  271.  
  272.         <p>
  273.         <label for="<?php echo $this->get_field_id( 'content' ); ?>"><?php _e( 'Content:' ); ?></label>
  274.         <textarea class="widefat" id="<?php echo $this->get_field_id( 'content' ); ?>" name="<?php echo $this->get_field_name( 'content' ); ?>" ><?php echo esc_attr( $content ); ?></textarea>
  275.         <?php _e('You can use the following template tags [count], [channel], [server]'); ?>
  276.         </p>
  277.  
  278.         <p>
  279.         <label for="<?php echo $this->get_field_id( 'interval' ); ?>"><?php _e( 'Interval: (in minutes)' ); ?></label>
  280.         <input class="widefat" id="<?php echo $this->get_field_id( 'interval' ); ?>" name="<?php echo $this->get_field_name( 'interval' ); ?>" type="text" value="<?php echo esc_attr( $interval ); ?>" />
  281.         </p>
  282. <?php
  283.     }
  284. } // class IRC_Widget
  285.  
  286. // register IRC_Widget widget
  287. add_action( 'widgets_init', create_function( '', 'register_widget( "IRC_Widget" );' ) );
  288.  
  289. /**
  290.  * IRC Class
  291.  */
  292. class IRC {
  293.  
  294. /**
  295.  * Get the list of users who are active in a IRC Channel
  296.  *
  297.  * @param <type> $server
  298.  * @param <type> $port
  299.  * @param <type> $channel
  300.  * @param <type> $nickname
  301.  * @return <type>
  302.  */
  303.     static function get_irc_channel_users($irc_server, $port, $channel, $nickname = "wp-irc-bot") {
  304.         //TODO: Clean up this function
  305.         $server = array(); //we will use an array to store all the server data.
  306.         $count = 0;
  307.         //Open the socket connection to the IRC server
  308.         $fp = fsockopen($irc_server, $port, $errno, $errstr);
  309.         if($fp) {
  310.             //Ok, we have connected to the server, now we have to send the login commands.
  311.  
  312.             @fwrite($fp, "PASS NOPASS\n\r", strlen("PASS NOPASS\n\r")); //Sends the password not needed for most servers
  313.             @fwrite($fp, "NICK $nickname\n\r", strlen("NICK $nickname\n\r")); //sends the nickname
  314.             @fwrite($fp, "USER $nickname USING WP IRC Plugin\n\r", strlen("USER $nickname USING WP IRC Plugin\n\r"));
  315.  
  316.             $names = "";
  317.             while(!feof($fp)) //while we are connected to the server
  318.             {
  319.                 $server['READ_BUFFER'] = fgets($fp, 1024); //get a line of data from the server
  320.     //            echo "[RECIVE] ".$server['READ_BUFFER']."<br>\n\r"; //display the recived data from the server
  321.  
  322.                 //Now lets check to see if we have joined the server
  323.                 if(strpos($server['READ_BUFFER'], "/MOTD")){
  324.                     //MOTD (The last thing displayed after a successful connection)
  325.                     //If we have joined the server
  326.                     @fwrite($fp, "LIST $channel\n\r", strlen("LIST $channel\n\r")); //get information about the chanel
  327.                 }
  328.  
  329.                 if (strpos($server['READ_BUFFER'], "322")) { // Result for LIST Command
  330.                     preg_match("/$channel ([0-9]+)/", $server['READ_BUFFER'], $matches);
  331.     //              echo "count : " . $matches[1] . "<br>\n\r";
  332.                     $count = $matches[1];
  333.                 }
  334.                
  335.                 if(strpos($server['READ_BUFFER'], "/LIST")) { //End of LIST
  336.                     //Get the list of users from channel
  337.                     @fwrite($fp, "QUIT\n\r", strlen("QUIT\n\r")); //Quit the channel
  338.     //                @fwrite($fp, "NAMES $channel\n\r", strlen("NAMES $channel\n\r")); //get information about the chanel
  339.                 }
  340.                 //TODO: Need to get the list of people who are active. There seems to be some problem with the below code. Need to debug it
  341.     //            if (strpos($server['READ_BUFFER'], "353")) { // Result for NAMES Command
  342.     //              $names .= substr($server['READ_BUFFER'], strpos($server['READ_BUFFER'], ":", 2) + 1);
  343.     //            }
  344.  
  345.     //            if(strpos($server['READ_BUFFER'], "/NAMES")) { //End of Names
  346.                     //Quit the chanel
  347.     //                @fwrite($fp, "QUIT\n\r", strlen("QUIT\n\r"));
  348.     //            }
  349.  
  350.                 if(substr($server['READ_BUFFER'], 0, 6) == "PING :") {//If the server has sent the ping command
  351.                     //Reply with pong
  352.                     @fwrite($fp, "PONG :".substr($server['READ_BUFFER'], 6)."\n\r", strlen("PONG :" . substr($server['READ_BUFFER'], 6) . "\n\r"));
  353.                     //As you can see i dont have it reply with just "PONG"
  354.                     //It sends PONG and the data recived after the "PING" text on that recived line
  355.                     //Reason being is some irc servers have a "No Spoof" feature that sends a key after the PING
  356.                     //Command that must be replied with PONG and the same key sent.
  357.                 }
  358.                 flush(); //This flushes the output buffer forcing the text in the while loop to be displayed "On demand"
  359.             }
  360.         // close the socket
  361.         fclose($fp);
  362.         } else {
  363.             // If there is some error
  364.             //TODO:Include better error handling
  365.         }
  366.         return $count;
  367.     }
  368. }
  369. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement