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

Commit

Permalink
ZF-10006: Zend_Captcha_Image::_gc() removes only captcha files identi…
Browse files Browse the repository at this point in the history
…fied by their suffix

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@22589 44c647ce-9c0f-0410-b52a-842ac1e357ba
  • Loading branch information
mikaelkael committed Jul 16, 2010
1 parent c2488c3 commit 19e8e52
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -577,10 +577,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 Down
21 changes: 21 additions & 0 deletions test/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,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 19e8e52

Please sign in to comment.