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
7 * username.jpg in user namespace. If none found or for unregistered guests, the
8 * avatar from Gravatar.com is taken when available. The MonsterID by Andreas
9 * Gohr serves as fallback.
10 *
11 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
12 * @author   Esther Brunner <wikidesign@gmail.com>
13 * @author   Daniel Dias Rodrigues <danieldiasr@gmail.com> (modernization)
14 */
15
16if(!defined('DOKU_INC')) die();
17
18class syntax_plugin_avatar extends DokuWiki_Syntax_Plugin {
19
20    const SIZE_SMALL = 20;
21    const SIZE_MEDIUM = 40;
22    const SIZE_LARGE = 80;
23    const SIZE_XLARGE = 120;
24
25    public function getType(): string { return 'substition'; }
26    public function getSort(): int { return 315; }
27
28    public function connectTo($mode): void {
29        $this->Lexer->addSpecialPattern("{{(?:gr|)avatar>.+?}}", $mode, 'plugin_avatar');
30    }
31
32    public function handle($match, $state, $pos, Doku_Handler $handler): array {
33        list($syntax, $match) = explode('>', substr($match, 2, -2), 2);
34        // $syntax = 'avatar' or 'gravatar'
35
36        if (!preg_match('/^([^?|]+)(?:\?([^|]*))?(?:\|(.*))?$/', $match, $matches)) {
37            return ['', '', null, null];
38        }
39
40        $user = $matches[1];
41        $param = isset($matches[2]) ? trim(strtolower($matches[2])) : '';
42        $title = isset($matches[3]) ? trim($matches[3]) : '';
43
44        // Determine alignment
45        $align = null;
46        if (preg_match('/^ /', $user)) $align = 'right';
47        if (preg_match('/ $/', $user)) $align = $align ? 'center' : 'left';
48        $user = trim($user);
49
50        // Determine size
51        switch ($param) {
52            case 's':  $size = self::SIZE_SMALL; break;
53            case 'm':  $size = self::SIZE_MEDIUM; break;
54            case 'l':  $size = self::SIZE_LARGE; break;
55            case 'xl': $size = self::SIZE_XLARGE; break;
56            default:   $size = null;
57        }
58
59        return [$user, $title, $align, $size];
60    }
61
62    public function render($mode, Doku_Renderer $renderer, $data): bool {
63        if ($mode !== 'xhtml') return false;
64
65        if ($my = plugin_load('helper', 'avatar')) {
66            $renderer->doc .= '<span class="vcard">' .
67                $my->renderXhtml($data[0], $data[1], $data[2], $data[3]) .
68                '</span>';
69        }
70        return true;
71    }
72}
73
74