<? /** * This plugin can be used to display entries from one year prior to todays date */
class NP_RandomQuote extends NucleusPlugin {
    function getEventList() { return array(); }
    function getName() { return 'RandomQuote'; }
    function getAuthor() { return 'Aspirant'; }
    function getURL() { return 'http://www.slavespath.net/gifts/nucleus/randomquote/'; }
    function getVersion() { return '1.1'; }
    function getDescription() { return 'This plugin will display a randomly selected quote that rotates on a periodic basis'; }
 
     function install() {
        $this->createOption('contentdir','Path to quote content', 'text',
            $_SERVER['DOCUMENT_ROOT'].'/quotes/');
        $this->createOption('cachedir','Path to current quote cache (must be different than content path)',
            'text', $_SERVER['DOCUMENT_ROOT'].'/quotes/cache/');
        $this->createOption('ext','File extension for quote files','text','.txt');
     }
 
     function init() {
        $this->contentdir = $this->getOption('contentdir');
        $this->cachedir = $this->getOption('cachedir');
        $this->ext = $this->getOption('ext');
     }
 
    function doSkinVar($skinType, $currentquote, $quotelife) {
        // Check if current quote file exists and is current
        $fs = @stat($this->cachedir.$currentquote);
        if(!$fs || $_GET["changequote"] || (time()-$fs["mtime"] > $quotelife))
            $this->changequote($this->cachedir.$currentquote);
        @readfile($this->cachedir.$currentquote);
    }
 
    function changequote($currentquote)
    {
        $quotes_seen = 0;
        $quote = "(No quote selected)";
        $filename = array();
 
        // Get new quote
        $extlen = strlen($this->ext);
        $fp = @opendir($this->contentdir);
        if($fp)
            while($f=readdir($fp)) {
                $flen=strlen($f);
                if($flen > $extlen)
                    if(strpos($f,$this->ext,0) == $flen - $extlen)
                        $filename[count($filename)]=$f;    
            }
 
        if(!count($filename)) {
            echo 'Sorry, no quote files (*'.$this->ext.') in '.$this->contentdir;
            return;
        }
        // Randomly select a quote file to read from
        $fname = $this->contentdir."/".$filename[array_rand($filename)];
        $fp = @fopen($fname,"r");
        if(!$fp) {
            echo "Sorry, cannot read quote file.";
            return;
        }
 
        while(!feof($fp)) {
            $q = $this->readquote($fp);
 
            if($q) {
                ++$quotes_seen;
                if(rand(1,$quotes_seen)==1)
                    $quote = $q;
            }
        }
        fclose($fp);
 
        if(!$quote) {
            echo "Sorry, no quotes in quote file $fname";
            return;
        }
 
        $fp = fopen($currentquote,"w");
        if(!$fp) {
            echo "Sorry, could not write quote to $currentquote";
            return;
        }
        fputs($fp,$quote);
        fclose($fp);
        return;
    }
 
    function readquote($fp)
    {
        $quote = "";
        while(!feof($fp)) {
            $s = trim(fgets($fp, 800));
            if($s == '$END')
                break;
            if(!$quote) {
                if(!$s)
                    continue;    // Trim leading blank lines
            } else
                $quote .= "\n";
            $quote .= $s;
        }
        return $quote;
    }
 
}
?>
randomquote_code.txt · Last modified: 2010/07/20 10:12 (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