Nucleus Plugin: NP_Counter2: Code

Back to plugin overview

<?php
// +-------------------------------------------------------
// |            Nucleus Counter2 Plug-in              
// +-------------------------------------------------------
// |
// +-INFO--------------------------------------------------
// |  Author:   Jeroen Budts (TeRanEX)
// |  URL:      http://budts.be/weblog/
// |  JabberID: teranex@jabber.org
// |
// +-TODO--------------------------------------------------
// | 
// +-HISTORY-----------------------------------------------
// |  2004-04-22: - (v0.1) Initial Release
// |  2004-04-23: - (v0.2) added bot-detection
// |  2004-04-25: - (v0.3) The value of the counter is now
// |                       stored as a blogoption so that 
// |                       every blog has its own counter
// |  2004-05-11: - (v0.4) Added a counter for feeds
// |                       Added 'nodisplay' to not display
// |                         a counter (only count)
// |                       Added some more bots (7)
// |                       Added 'nocount' option
// |
// +-CVS---------------------------------------------------
// | $Id: NP_Counter2.php,v 1.6 2004/05/11 22:18:47 Jeroen Budts Exp $
// |
// +-------------------------------------------------------
 
class NP_Counter2 extends NucleusPlugin {
 
// --------- Plug-in Info ---------------------------------
  // name of plugin
  function getName() {
    return 'Counter2';
  }
 
  // author of plugin
  function getAuthor() {
    return 'TeRanEX';
  }
  // an URL to the plugin website
  function getURL() {
    return 'http://budts.be/weblog/';
  }
 
  // version of the plugin
  function getVersion() {
    return '0.4';
  }
 
  // a description to be shown on the installed plugins listing
  function getDescription() {
    return 'A plugin to add a textual or graphical counter to your blog';
  }
 
  //supported features
  function supportsFeature($what) {
    switch($what) {
      case 'SqlTablePrefix':
        return 1;
      default:
        return 0;
    }
  }
 
  function getMinNucleusVersion() {
    return 220;
  }
// --------- Install and Uninstall functions --------------
  function install() {
    $this->createBlogOption('CounterValue', 'Value of your counter', 'text', '0');
    $this->createOption('FeedCounterValue', 'Value of you feedcounter', 'text', '0');
  }
 
 
// --------- do...-Functions ------------------------------
  function doSkinVar($skinType, $counterType = 'visits', $counterMode = 'textual', $counterUpdate = 'count') {
    global $blog;
    $currentBlog = $blog->blogid;
    //countertype must be 'visits' or 'feeds'
    //but may be 'visits', 'feeds', 'textual', 'graphical' for compatibility
    //and to provide a shorter way to put the counter in the skin
    if (($counterType == 'textual')||($counterType == 'graphical')) {
        $counterMode = $counterType;
        $counterType = 'visits';
    }
    if ($counterType == '') {
        $counterType = 'visits';
    }
    //now we are sure that countertype is visits of feeds
    //and countermode is textual or graphics or nodisplay
 
    switch ($counterType) {
	    // visitcounter
        case 'visits':
    		// first check if there is a session (there is if the session variable
            // has a value)
            if (!$_SESSION['CounterValue']) {
                //There is no session
                $counterValue = $this->getBlogOption($currentBlog, 'CounterValue');
                if ($counterUpdate != 'nocount') {
                    //if the UA is not a bot and we may count, update the counter by one
                    $ua = $_SERVER['HTTP_USER_AGENT'];
                    if (!$this->_isBot($ua)) {
                        $counterValue++;
                        $this->setBlogOption($currentBlog, 'CounterValue', $counterValue);
                    }
                }
                $_SESSION['CounterValue'] = $counterValue;
            } else {
                //we have a session, retreive the counter value
                $counterValue = $_SESSION['CounterValue'];
            }
            break;
 
        // feedcounter
        case 'feeds':
            //Feeds don't work with sessions (as far as I know)
            //so we don't have to use session stuff
            $counterValue = $this->getOption('FeedCounterValue');
            if ($counterUpdate != 'nocount') {
                //if the UA is not a bot and we may count, update the counter by one
                $ua = $_SERVER['HTTP_USER_AGENT'];
                if (!$this->_isBot($ua)) {
                    $counterValue++;
                    $this->setOption('FeedCounterValue', $counterValue);
                }
            }
            break;
 
        //something's wrong
        default:
            $counterValue = 'Invalid countervalue';
            break;
    }
 
    //now we can display the counter;
    switch ($counterMode) {
        //user wants a graphical counter
        case 'graphical':
            //display the graphical counter
            for ($i = 0; $i < strlen($counterValue); $i++) {
                $counterDigit = substr($counterValue, $i, 1);
                echo '<img src="'.$this->getAdminURL().$counterDigit.'.png" alt="HitCounter: '.$counterDigit.'" />';
            }
            break;
 
        //user wants a graphical counter
        case 'textual':
            //display the textual counter
            echo $counterValue;
            break;
    }
  }
 
// --------- Various Functions ----------------------------
  function _isBot($ua) {
    $list[0]  = 'googlebot';
    $list[1]  = 'altavista';
    $list[2]  = 'infoseek';
    $list[3]  = 'lycos';
    $list[4]  = 'muscatferret';
    $list[5]  = 'hotbot';
    $list[6]  = 'ultraseek';
    $list[7]  = 'webcrawler';
    $list[8]  = 'yahoo';
    $list[9]  = 'msnbot';
    $list[10] = 'scooter';
    $list[11] = 'technoratibot';
    $list[12] = 'blogsharesbot';
    $list[13] = 'ia_archiver';
    $list[14] = 'qweerybot';
    $list[15] = 'nitleblogspider';
    $list[16] = 'gigabot';
    $list[17] = 'mozdex';
 
    for ($i=0;$i<count($list);$i++) {
      if (stristr($ua,$list[$i])) {
        return 1;
      }
    }
 
    return 0;
  }
}
?>
nucleus/plugins/counter2/code.txt · Last modified: 2007/11/03 02:52 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki