1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Esther Brunner <wikidesign@gmail.com>
5 */
6
7// must be run within Dokuwiki
8if (!defined('DOKU_INC')) die();
9
10class helper_plugin_avatar extends DokuWiki_Plugin {
11
12  function getMethods() {
13    $result = array();
14    $result[] = array(
15      'name'   => 'getXHTML',
16      'desc'   => 'returns the XHTML to display an avatar',
17      'params' => array(
18        'user or mail'     => 'string',
19        'title (optional)' => 'string',
20        'align (optional)' => 'string',
21        'size (optional)'  => 'integer'),
22      'return' => array('xhtml' => 'string'),
23    );
24    return $result;
25  }
26
27  /**
28   * Returns the XHTML of the Avatar
29   */
30  function getXHTML($user, $title = '', $align = '', $size = NULL) {
31
32    // determine the URL of the avatar image
33    $src = $this->_getAvatarURL($user, $title, $size);
34
35    // output with vcard photo microformat
36    return '<img src="'.$src.'" class="media'.$align.' photo fn"'.
37      ' title="'.$title.'" alt="'.$title.'" width="'.$size.'"'.
38      ' height="'.$size.'" />';
39  }
40
41  /**
42   * Main function to determine the avatar to use
43   */
44  function _getAvatarURL($user, &$title, &$size) {
45    global $auth;
46
47    if (!$size || !is_int($size)) $size = $this->getConf('size');
48
49    if(is_array($user)) {
50        $mail = $user['mail'];
51        $name = $user['name'];
52        $user = $user['user'];
53    } else {
54        $mail = $user;
55    }
56
57    // check first if a local image for the given user exists
58    $userinfo = $auth->getUserData($user);
59    if (is_array($userinfo)) {
60      if (($userinfo['name']) && (!$title)) $title = hsc($userinfo['name']);
61      $ns = $this->getConf('namespace');
62      $formats = array('.png', '.jpg', '.gif');
63      foreach ($formats as $format) {
64        $user_img = mediaFN($ns.':'.$user.$format);
65        $name_img = mediaFN($ns.':'.$name.$format);
66        if(@file_exists($user_img)) {;
67            $src = ml($ns.':'.$user.$format, array('w' => $size, 'h' => $size));
68            break;
69        } elseif(@file_exists($name_img)) {
70            $src = ml($ns.':'.$name.$format, array('w' => $size, 'h' => $size));
71        }
72      }
73      if (empty($src)) $mail = $userinfo['mail'];
74    }
75
76    if (empty($src)) {
77      $seed = md5(utf8_strtolower($mail));
78
79      if (function_exists('imagecreatetruecolor')) {
80        // we take the monster ID as default
81        $file = 'monsterid.php?seed='.$seed.'&size='.$size.'&.png';
82
83      } else {
84        // GDlib is not availble - resort to default images
85        switch ($size) {
86        case 20: case 40: case 80:
87          $file = 'images/default_'.$size.'.png';
88          break;
89        default:
90          $file = 'images/default_120.png';
91        }
92      }
93      $default = ml(DOKU_URL.'/lib/plugins/avatar/'.$file, 'cache=recache', true, '&', true);
94
95      // do not pass invalid or empty emails to gravatar site...
96      if (mail_isvalid($mail)){
97          if (is_ssl()) {
98              $src = 'https://secure.gravatar.com/';
99          } else {
100              $src = 'http://www.gravatar.com/';
101          }
102          $src .= 'avatar/'.$seed.'?s='.$size.'&d='.$this->getConf('default').'&r='.$this->getConf('rating').'&.jpg';
103          $src = ml($src);
104      // show only default image if invalid or empty email given
105      } else {
106        $src = $default;
107      }
108    }
109
110    if (!$title) $title = obfuscate($mail);
111
112    return $src;
113  }
114}
115// vim:ts=4:sw=4:et:enc=utf-8:
116