Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' of git://git.zendframework.com/zf
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelkael committed Jul 22, 2010
6 parents bb2812c + 63ddc40 + 7da425c + 1f8bc83 + c5ab2f3 + 7a52da4 commit f7972b4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -576,10 +576,14 @@ protected function _gc()
// safety guard
return;
}
$suffixLength = strlen($this->_suffix);
foreach (new \DirectoryIterator($imgdir) as $file) {
if (!$file->isDot() && !$file->isDir()) {
if ($file->getMTime() < $expire) {
unlink($file->getPathname());
// only deletes files ending with $this->_suffix
if (substr($file->getFilename(), -($suffixLength)) == $this->_suffix) {
unlink($file->getPathname());
}
}
}
}
Expand All @@ -594,6 +598,7 @@ protected function _gc()
*/
public function render(\Zend_View_Interface $view = null, $element = null)
{
return '<img width="'.$this->getWidth().'" height="'.$this->getHeight().'" alt="'.$this->getImgAlt().'" src="' . $this->getImgUrl() . $this->getId() . $this->getSuffix() . '"/><br/>';
return '<img width="' . $this->getWidth() . '" height="' . $this->getHeight() . '" alt="' . $this->getImgAlt()
. '" src="' . $this->getImgUrl() . $this->getId() . $this->getSuffix() . '" />';
}
}
21 changes: 21 additions & 0 deletions test/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,27 @@ public function testCaptchaImageCleanup()
$this->assertFalse(file_exists($filename), "File $filename was found even after GC");
}

/**
* @group ZF-10006
*/
public function testCaptchaImageCleanupOnlyCaptchaFilesIdentifiedByTheirSuffix()
{
$this->element->render($this->getView());
$filename = $this->testDir."/".$this->captcha->getId().".png";
$this->assertTrue(file_exists($filename));
//Create other cache file
$otherFile = $this->testDir . "/zf10006.cache";
file_put_contents($otherFile, '');
$this->assertTrue(file_exists($otherFile));
$this->captcha->setExpiration(1);
$this->captcha->setGcFreq(1);
sleep(2);
$this->captcha->generate();
clearstatcache();
$this->assertFalse(file_exists($filename), "File $filename was found even after GC");
$this->assertTrue(file_exists($otherFile), "File $otherFile was not found after GC");
}

public function testGenerateReturnsId()
{
$id = $this->captcha->generate();
Expand Down

0 comments on commit f7972b4

Please sign in to comment.