1<?php
2/**
3 * Avatar Plugin: displays avatar images with syntax {{avatar>email@domain.com}}
4 * Optionally you can add a title attribute: {{avatar>email@domain.com|My Name}}
5 *
6 * For registered users the plugin looks first for a local avatar named username.jpg
7 * in user namespace. If none found or for unregistered guests, the avatar from
8 * Gravatar.com is taken when available. The MonsterID by Andreas Gohr serves as fallback.
9 *
10 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
11 * @author   Esther Brunner <wikidesign@gmail.com>
12 */
13
14// must be run within Dokuwiki
15if(!defined('DOKU_INC')) die();
16
17if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
18
19class syntax_plugin_avatar extends DokuWiki_Syntax_Plugin {
20
21    function getType() { return 'substition'; }
22    function getSort() { return 315; }
23
24    function connectTo($mode) {
25        $this->Lexer->addSpecialPattern("{{(?:gr|)avatar>.+?}}",$mode,'plugin_avatar');
26    }
27
28    function handle($match, $state, $pos, Doku_Handler $handler) {
29        list($syntax, $match) = explode('>', substr($match, 0, -2), 2); // strip markup
30        $one = explode('?', $match, 2);    // [user|mail] ? [size]|[title]
31        $two = explode('|', $one[0], 2);   // [user] & [mail]
32        $three = explode('|', $one[1], 2); // [size] & [title]
33        $user = $two[0];
34        $title = $three[1];
35        $param = $three[0];
36
37        // Check alignment
38        $ralign = (bool)preg_match('/^ /', $user);
39        $lalign = (bool)preg_match('/ $/', $user);
40        if ($lalign & $ralign) $align = 'center';
41        else if ($ralign)      $align = 'right';
42        else if ($lalign)      $align = 'left';
43        else                   $align = NULL;
44
45        if (preg_match('/^s/', $param))       $size = 20;
46        else if (preg_match('/^m/', $param))  $size = 40;
47        else if (preg_match('/^l/', $param))  $size = 80;
48        else if (preg_match('/^xl/', $param)) $size = 120;
49        else $size = NULL;
50
51        return array($user, $title, $align, $size);
52    }
53
54    function render($mode, Doku_Renderer $renderer, $data) {
55        if ($mode == 'xhtml') {
56            if ($my =& plugin_load('helper', 'avatar'))
57                $renderer->doc .= '<span class="vcard">'.
58                $my->getXHTML($data[0], $data[1], $data[2], $data[3]).
59                '</span>';
60            return true;
61        }
62        return false;
63    }
64}
65// vim:ts=4:sw=4:et:enc=utf-8:
66