Skip to content

Commit

Permalink
Updated Gravatar helper to use auto-detection for pre-hashed email ad…
Browse files Browse the repository at this point in the history
…dresses
  • Loading branch information
EvanDotPro authored and Mike Willbanks committed May 23, 2013
1 parent 6670c9e commit 7953028
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 35 deletions.
38 changes: 9 additions & 29 deletions library/Zend/View/Helper/Gravatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ class Gravatar extends AbstractHtmlElement
*/
protected $email;

/**
* True or false if the email address passed is already an MD5 hash
*
* @var bool
*/
protected $emailIsHashed;

/**
* Options
*
Expand All @@ -66,7 +73,6 @@ class Gravatar extends AbstractHtmlElement
'default_img' => self::DEFAULT_MM,
'rating' => self::RATING_G,
'secure' => null,
'skip_email_hash' => false,
);

/**
Expand Down Expand Up @@ -136,7 +142,7 @@ public function setOptions(array $options)
protected function getAvatarUrl()
{
$src = $this->getGravatarUrl()
. '/' . ($this->getSkipEmailHash() ? $this->getEmail() : md5($this->getEmail()))
. '/' . ($this->emailIsHashed ? $this->getEmail() : md5($this->getEmail()))
. '?s=' . $this->getImgSize()
. '&d=' . $this->getDefaultImg()
. '&r=' . $this->getRating();
Expand Down Expand Up @@ -232,6 +238,7 @@ public function getDefaultImg()
*/
public function setEmail($email)
{
$this->emailIsHashed = (bool) preg_match('/^[A-Za-z0-9]{32}$/', $email);
$this->email = $email;
return $this;
}
Expand Down Expand Up @@ -333,33 +340,6 @@ public function getSecure()
return $this->options['secure'];
}

/**
* Skip email hashing
*
* This is useful if you are using a webservice that provides you the
* gravatar hash (md5sum) of users' email addresses, but not the actual
* email addresses themselves.
*
* @param bool $flag
* @return Gravatar
*/
public function setSkipEmailHash($flag)
{
$this->options['skip_email_hash'] = $flag;
return $this;
}

/**
* Returns if the helper should pass the email through md5() or not.
*
* @return boolean
*/
public function getSkipEmailHash()
{
return $this->options['skip_email_hash'];
}


/**
* Set src attrib for image.
*
Expand Down
10 changes: 4 additions & 6 deletions tests/ZendTest/View/Helper/GravatarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ public function testGetAndSetMethods()
$this->helper->setDefaultImg('monsterid')
->setImgSize(150)
->setSecure(true)
->setSkipEmailHash(true)
->setEmail("[email protected]")
->setAttribs($attribs)
->setRating('pg');
Expand All @@ -102,7 +101,6 @@ public function testGetAndSetMethods()
$this->assertEquals($attribs, $this->helper->getAttribs());
$this->assertEquals(150, $this->helper->getImgSize());
$this->assertTrue($this->helper->getSecure());
$this->assertTrue($this->helper->getSkipEmailHash());
}

public function tesSetDefaultImg()
Expand Down Expand Up @@ -190,15 +188,15 @@ public function testGravatarOptions()
);
}

public function testSkipEmailHashingOptionTogglesMd5Hashing()
public function testPassingAnMd5HashSkipsMd5Hashing()
{
$this->assertNotContains(
'b642b4217b34b1e8d3bd915fc65c4452',
$this->helper->__invoke('b642b4217b34b1e8d3bd915fc65c4452')->__toString()
'[email protected]',
$this->helper->__invoke('[email protected]')->__toString()
);
$this->assertContains(
'b642b4217b34b1e8d3bd915fc65c4452',
$this->helper->__invoke('b642b4217b34b1e8d3bd915fc65c4452', array('skip_email_hash' => true))->__toString()
$this->helper->__invoke('b642b4217b34b1e8d3bd915fc65c4452')->__toString()
);
}

Expand Down

0 comments on commit 7953028

Please sign in to comment.