source: trunk/content/plugins/gravatar/gravatar.php @ 1375

Revision 1190, 4.9 KB checked in by nick_ramsay, 3 years ago (diff)

[TRUNK] Hotaru 1.1 - run install/upgrade.php and upgrade the plugins by turning them off and back on again. Details in forum soon.

Line 
1<?php
2/**
3 * name: Gravatar
4 * description: Enables Gravatar avatars for users
5 * version: 0.8
6 * folder: gravatar
7 * class: Gravatar
8 * type: avatar
9 * requires: users 1.1
10 * hooks: avatar_set_avatar, avatar_get_avatar, avatar_show_avatar, avatar_test_avatar
11 * author: Nick Ramsay
12 * authorurl: http://hotarucms.org/member.php?1-Nick
13 *
14 * PHP version 5
15 *
16 * LICENSE: Hotaru CMS is free software: you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation, either version 3 of
19 * the License, or (at your option) any later version.
20 *
21 * Hotaru CMS is distributed in the hope that it will be useful, but WITHOUT
22 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 * FITNESS FOR A PARTICULAR PURPOSE.
24 *
25 * You should have received a copy of the GNU General Public License along
26 * with Hotaru CMS. If not, see http://www.gnu.org/licenses/.
27 *
28 * @category  Content Management System
29 * @package   HotaruCMS
30 * @author    Nick Ramsay <admin@hotarucms.org>
31 * @copyright Copyright (c) 2009, Hotaru CMS
32 * @license   http://www.gnu.org/copyleft/gpl.html GNU General Public License
33 * @link      http://www.hotarucms.org/
34 *
35 *
36 * USAGE: This class hooks into the Avatar class, so is used like this:
37 *
38 * $avatar = new Avatar($h, $user_id, $size, $rating);
39 * $avatar->getAvatar($h); // returns the avatar for custom display... OR...
40 * $avatar->linkAvatar($h); // displays the avatar linked to the user's profile
41 * $avatar->wrapAvatar($h); // displays the avatar linked and wrapped in a div (css class: avatar_wrapper)
42 *
43 * Shortcuts:
44 * $h->setAvatar($user_id, $size, $rating);
45 * $h->getAvatar(); $h->linkAvatar(); $h->wrapAvatar();
46 */
47
48class Gravatar
49{
50    /**
51     * Set global $h vars for this avatar
52     *
53     * @param $vars array of size, user_id and user_email
54     */
55    public function avatar_set_avatar($h, $vars)
56    {
57        $h->vars['avatar_size'] = $vars['size'];
58        $h->vars['avatar_rating'] = $vars['rating'];
59        $h->vars['avatar_user_id'] = $vars['user_id'];
60        $h->vars['avatar_user_name'] = $vars['user_name'];
61        $h->vars['avatar_user_email'] = $vars['user_email'];
62    }
63   
64   
65    /**
66     * return the avatar with no surrounding HTML div
67     *
68     * @return return the avatar
69     */
70    public function avatar_test_avatar($h)
71    {
72        $grav_url = $this->buildGravatarUrl($h->vars['avatar_user_email'], $h->vars['avatar_size'], $h->vars['avatar_rating'], '404');
73
74        $headers = @get_headers($grav_url);
75        if (preg_match("|200|", $headers[0])) {
76            return $this->buildGravatarImage($grav_url, $h->vars['avatar_size']);
77        }
78    }
79   
80   
81    /**
82     * return the avatar with no surrounding HTML div
83     *
84     * @return return the avatar
85     */
86    public function avatar_get_avatar($h)
87    {
88        $grav_url = $this->buildGravatarUrl($h->vars['avatar_user_email'], $h->vars['avatar_size'], $h->vars['avatar_rating']);
89        $img_url = $this->buildGravatarImage($grav_url, $h->vars['avatar_size']);
90        return $img_url;
91    }
92   
93   
94    /**
95     * Build Gravatar image
96     *
97     * @param string $email - email of avatar user
98     * @param int $size - size (1 ~ 512 pixels)
99     * @param string $rating - g, pg, r or x
100     * @return string - html for image
101     */
102    public function buildGravatarUrl($email = '', $size = 32, $rating = 'g', $default = '')
103    {
104        if ($default != '404') {
105            // Look in the theme's images folder for a default avatar before using the one in the Gravatar images folder
106            if (file_exists(THEMES . THEME . "images/default_80.png")) {
107                $default_image = BASEURL . "content/themes/"  . THEME . "images/default_80.png";
108                $default = urlencode($default_image);
109            } else {
110                $default_image = BASEURL . "content/plugins/gravatar/images/default_80.png";
111                $default = urlencode($default_image);
112            }
113        }
114       
115        $grav_url = "http://www.gravatar.com/avatar/".md5( strtolower($email) ).
116            "?d=". $default .
117            "&amp;size=" . $size .
118            "&amp;r=" . $rating;
119       
120        return $grav_url;
121    }
122   
123   
124    /**
125     * Build Gravatar image
126     *
127     * @param string $email - email of avatar user
128     * @param int $size - size (1 ~ 512 pixels)
129     * @param string $rating - g, pg, r or x
130     * @return string - html for image
131     */
132    public function buildGravatarImage($grav_url = '', $size = 32)
133    {
134        if (!$grav_url) { return false; }
135       
136        $resized = "style='height: " . $size . "px; width: " . $size . "px'";
137               
138        $img_url = "<img class='avatar' src='" . $grav_url . "' " . $resized  ." alt='' />";
139        return $img_url;
140    }
141
142}
143
144?>
Note: See TracBrowser for help on using the repository browser.