PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 8ms RepoModel.GetById 32ms app.codeStats 0ms

/View/Helper/Gravatar.php

https://bitbucket.org/goldie/zend-framework1
PHP | 363 lines | 146 code | 29 blank | 188 comment | 4 complexity | 432b7c4eeac44757f2ed72d612be2a88 MD5 | raw file
  1<?php
  2/**
  3 * Zend Framework
  4 *
  5 * LICENSE
  6 *
  7 * This source file is subject to the new BSD license that is bundled
  8 * with this package in the file LICENSE.txt.
  9 * It is also available through the world-wide-web at this URL:
 10 * http://framework.zend.com/license/new-bsd
 11 * If you did not receive a copy of the license and are unable to
 12 * obtain it through the world-wide-web, please send an email
 13 * to license@zend.com so we can send you a copy immediately.
 14 *
 15 * @category   Zend
 16 * @package    Zend_View
 17 * @subpackage Helper
 18 * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
 19 * @version    $Id: Doctype.php 16971 2009-07-22 18:05:45Z mikaelkael $
 20 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 21 */
 22
 23/** Zend_View_Helper_HtmlElement */
 24require_once 'Zend/View/Helper/HtmlElement.php';
 25
 26/**
 27 * Helper for retrieving avatars from gravatar.com
 28 *
 29 * @package    Zend_View
 30 * @subpackage Helper
 31 * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
 32 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 33 * @link http://pl.gravatar.com/site/implement/url
 34 */
 35class Zend_View_Helper_Gravatar extends Zend_View_Helper_HtmlElement
 36{
 37
 38    /**
 39     * URL to gravatar service
 40     */
 41    const GRAVATAR_URL = 'http://www.gravatar.com/avatar';
 42    /**
 43     * Secure URL to gravatar service
 44     */
 45    const GRAVATAR_URL_SECURE = 'https://secure.gravatar.com/avatar';
 46
 47    /**
 48     * Gravatar rating
 49     */
 50    const RATING_G  = 'g';
 51    const RATING_PG = 'pg';
 52    const RATING_R  = 'r';
 53    const RATING_X  = 'x';
 54
 55    /**
 56     * Default gravatar image value constants
 57     */
 58    const DEFAULT_404       = '404';
 59    const DEFAULT_MM        = 'mm';
 60    const DEFAULT_IDENTICON = 'identicon';
 61    const DEFAULT_MONSTERID = 'monsterid';
 62    const DEFAULT_WAVATAR   = 'wavatar';
 63
 64    /**
 65     * Options
 66     *
 67     * @var array
 68     */
 69    protected $_options = array(
 70        'img_size'    => 80,
 71        'default_img' => self::DEFAULT_MM,
 72        'rating'      => self::RATING_G,
 73        'secure'      => null,
 74    );
 75
 76    /**
 77     * Email Adress
 78     *
 79     * @var string
 80     */
 81    protected $_email;
 82
 83    /**
 84     * Attributes for HTML image tag
 85     *
 86     * @var array
 87     */
 88    protected $_attribs;
 89
 90    /**
 91     * Returns an avatar from gravatar's service.
 92     *
 93     * $options may include the following:
 94     * - 'img_size' int height of img to return
 95     * - 'default_img' string img to return if email adress has not found
 96     * - 'rating' string rating parameter for avatar
 97     * - 'secure' bool load from the SSL or Non-SSL location
 98     *
 99     * @see    http://pl.gravatar.com/site/implement/url
100     * @see    http://pl.gravatar.com/site/implement/url More information about gravatar's service.
101     * @param  string|null $email Email adress.
102     * @param  null|array $options Options
103     * @param  array $attribs Attributes for image tag (title, alt etc.)
104     * @return Zend_View_Helper_Gravatar
105     */
106    public function gravatar($email = "", $options = array(), $attribs = array())
107    {
108        $this->setEmail($email);
109        $this->setOptions($options);
110        $this->setAttribs($attribs);
111        return $this;
112    }
113
114    /**
115     * Configure state
116     *
117     * @param  array $options
118     * @return Zend_View_Helper_Gravatar
119     */
120    public function setOptions(array $options)
121    {
122        foreach ($options as $key => $value) {
123            $method = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
124            if (method_exists($this, $method)) {
125                $this->{$method}($value);
126            }
127        }
128        return $this;
129    }
130
131    /**
132     * Get img size
133     *
134     * @return int The img size
135     */
136    public function getImgSize()
137    {
138        return $this->_options['img_size'];
139    }
140
141    /**
142     * Set img size in pixels
143     *
144     * @param int $imgSize Size of img must be between 1 and 512
145     * @return Zend_View_Helper_Gravatar
146     */
147    public function setImgSize($imgSize)
148    {
149        $this->_options['img_size'] = (int) $imgSize;
150        return $this;
151    }
152
153    /**
154     * Get default img
155     *
156     * @return string
157     */
158    public function getDefaultImg()
159    {
160        return $this->_options['default_img'];
161    }
162
163    /**
164     * Set default img
165     *
166     * Can be either an absolute URL to an image, or one of the DEFAULT_* constants
167     *
168     * @param string $defaultImg
169     * @link http://pl.gravatar.com/site/implement/url More information about default image.
170     * @return Zend_View_Helper_Gravatar
171     */
172    public function setDefaultImg($defaultImg)
173    {
174        $this->_options['default_img'] = urlencode($defaultImg);
175        return $this;
176    }
177
178    /**
179     *  Set rating value
180     *
181     * Must be one of the RATING_* constants
182     *
183     * @param string $rating Value for rating. Allowed values are: g, px, r,x
184     * @link http://pl.gravatar.com/site/implement/url More information about rating.
185     * @throws Zend_View_Exception
186     */
187    public function setRating($rating)
188    {
189        switch ($rating) {
190            case self::RATING_G:
191            case self::RATING_PG:
192            case self::RATING_R:
193            case self::RATING_X:
194                $this->_options['rating'] = $rating;
195                break;
196            default:
197                require_once 'Zend/View/Exception.php';
198                throw new Zend_View_Exception(sprintf(
199                    'The rating value "%s" is not allowed',
200                    $rating
201                ));
202        }
203        return $this;
204    }
205
206    /**
207     * Get rating value
208     *
209     * @return string
210     */
211    public function getRating()
212    {
213        return $this->_options['rating'];
214    }
215
216    /**
217     * Set email adress
218     *
219     * @param string $email
220     * @return Zend_View_Helper_Gravatar
221     */
222    public function setEmail( $email )
223    {
224        $this->_email = $email;
225        return $this;
226    }
227
228    /**
229     * Get email adress
230     *
231     * @return string
232     */
233    public function getEmail()
234    {
235        return $this->_email;
236    }
237
238    /**
239     * Load from an SSL or No-SSL location?
240     *
241     * @param bool $flag
242     * @return Zend_View_Helper_Gravatar
243     */
244    public function setSecure($flag)
245    {
246        $this->_options['secure'] = ($flag === null) ? null : (bool) $flag;
247        return $this;
248    }
249
250    /**
251     * Get an SSL or a No-SSL location
252     *
253     * @return bool
254     */
255    public function getSecure()
256    {
257        if ($this->_options['secure'] === null) {
258            return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off');
259        }
260        return $this->_options['secure'];
261    }
262
263    /**
264     * Get attribs of image
265     *
266     * Warning!
267     * If you set src attrib, you get it, but this value will be overwritten in
268     * protected method _setSrcAttribForImg(). And finally your get other src
269     * value!
270     *
271     * @return array
272     */
273    public function getAttribs()
274    {
275        return $this->_attribs;
276    }
277
278    /**
279     * Set attribs for image tag
280     *
281     * Warning! You shouldn't set src attrib for image tag.
282     * This attrib is overwritten in protected method _setSrcAttribForImg().
283     * This method(_setSrcAttribForImg) is called in public method getImgTag().
284
285     * @param array $attribs
286     * @return Zend_View_Helper_Gravatar
287     */
288    public function setAttribs(array $attribs)
289    {
290        $this->_attribs = $attribs;
291        return $this;
292    }
293
294    /**
295     * Get URL to gravatar's service.
296     *
297     * @return string URL
298     */
299    protected function _getGravatarUrl()
300    {
301        return ($this->getSecure() === false) ? self::GRAVATAR_URL : self::GRAVATAR_URL_SECURE;
302    }
303
304    /**
305     * Get avatar url (including size, rating and default image oprions)
306     *
307     * @return string
308     */
309    protected function _getAvatarUrl()
310    {
311        $src = $this->_getGravatarUrl()
312             . '/'
313             . md5($this->getEmail())
314             . '?s='
315             . $this->getImgSize()
316             . '&d='
317             . $this->getDefaultImg()
318             . '&r='
319             . $this->getRating();
320        return $src;
321    }
322
323    /**
324     * Set src attrib for image.
325     *
326     * You shouldn't set a own url value!
327     * It sets value, uses protected method _getAvatarUrl.
328     *
329     * If already exsist overwritten.
330     */
331    protected function _setSrcAttribForImg()
332    {
333        $attribs        = $this->getAttribs();
334        $attribs['src'] = $this->_getAvatarUrl();
335        $this->setAttribs($attribs);
336    }
337
338    /**
339     * Return valid image tag
340     *
341     * @return string
342     */
343    public function getImgTag()
344    {
345        $this->_setSrcAttribForImg();
346        $html = '<img'
347              . $this->_htmlAttribs($this->getAttribs())
348              . $this->getClosingBracket();
349
350        return $html;
351    }
352
353    /**
354     * Return valid image tag
355     *
356     * @return string
357     */
358    public function  __toString()
359    {
360        return $this->getImgTag();
361
362    }
363}