1<?php
2/**
3 * Plugin Tuxquote: Inserts a random image and quote.
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Craig Douglas
7 */
8
9// must be run within DokuWiki
10if(!defined('DOKU_INC')) die();
11
12define('TUXQUOTE_DEFAULT_WIDTH', '256px'); // Default div width for shortcode print.
13define('TUXQUOTE_DEFAULT_ALIGN', 'right'); // Default div alignment.
14
15
16if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
17require_once DOKU_PLUGIN.'syntax.php';
18
19/**
20 * All DokuWiki plugins to extend the parser/rendering mechanism
21 * need to inherit from this class
22 */
23class syntax_plugin_tuxquote extends DokuWiki_Syntax_Plugin {
24
25    function getInfo() {
26        return array('author' => 'Craig Douglas',
27                     'email'  => 'contact22@eldougo.net',
28                     'date'   => '2014-09-18',
29                     'name'   => 'Tuxquote Plugin',
30                     'desc'   => 'Show a random image and quote',
31                     'url'    => 'https://github.com/eldougo/dokuwiki_plugin_tuxquote');
32    }
33
34    function getType() { return 'substition'; }
35    function getSort() { return 32; }
36
37    function connectTo($mode) {
38        $this->Lexer->addSpecialPattern('\[TUXQUOTE\]',$mode,'plugin_tuxquote');
39    }
40
41    function handle($match, $state, $pos, Doku_Handler $handler) {
42        return array($match, $state, $pos);
43    }
44
45    function render($mode, Doku_Renderer $renderer, $data) {
46        if($mode == 'xhtml'){
47            $renderer->doc .= $this->tuxquote_main();
48            return true;
49        }
50        return false;
51    }
52
53    /**
54     * Callback used to determine if the passed file is an image.
55     */
56    function tuxquote_is_image( $file_name ){
57        return strpos( $file_name, ".jpg" )
58        ||     strpos( $file_name, ".png" )
59        ||     strpos( $file_name, ".gif" );
60    }
61
62   /**
63    * Return a random quote.
64    */
65    public function tuxquote_choose_quote(){
66        $quotes = file( DOKU_PLUGIN . $this->getPluginName() . "/quotes.txt" );
67        return $quotes[ array_rand( $quotes, 1 ) ];
68    }
69
70    /**
71     * Chose and format a random image.
72     */
73    function tuxquote_choose_image() {
74        $image_url   = getBaseURL() . "lib/plugins/{$this->getPluginName()}/images/";
75        $image_dir   = DOKU_PLUGIN . $this->getPluginName() . "/images/";
76        $image_array = array_filter( scandir( $image_dir ), array( $this, 'tuxquote_is_image' ) );
77        return $image_url . $image_array[ array_rand( $image_array,1 ) ];
78    }
79
80    /**
81     * Build and format HTML output.
82     *
83     * @param   string  $div_width   Div width in pixels or percentage [NN%|NNpx].
84     * @param   string  $div_align       Div float alignment [none|left|right].
85     * @param   string  $title       Div title, optional.
86     */
87    function tuxquote_build_format( $div_width, $div_align, $title = '' ) {
88        if ( empty( $div_width ) ) {
89            $div_width = TUXQUOTE_DEFAULT_WIDTH;
90        }
91        if ( is_numeric( $div_width ) ) {
92            $div_width = trim( $div_width ) . "%";
93        }
94        if ( empty( $div_align ) ) {
95        $div_align = TUXQUOTE_DEFAULT_ALIGN;
96        }
97        if ( ! empty( $title ) ) {
98             $title_line = "  <p style='text-align: center; font-weight: 900'>" . $title . "</p>\n";
99        } else {
100            $title_line = '';
101        }
102        return  "\n<div style='float: " . $div_align . "; width: " . $div_width . "; '>\n"
103                .$title_line
104                ."  <img style='width:100%' src='" . $this->tuxquote_choose_image()  . "'><br />\n"
105                ."  <p style='text-align: center'>" . $this->tuxquote_choose_quote() . "</p>\n"
106                ."</div>\n";
107     }
108
109    /**
110     * Return HTML encoded random image and quote.
111     */
112    function tuxquote_main() {
113        return  $this->tuxquote_build_format( $this->getConf( 'tuxquote_width' ),
114                                              $this->getConf( 'tuxquote_align' ),
115                                              $this->getConf( 'tuxquote_title' ) );
116    }
117} // class syntax_plugin_tuxquote
118