Skip to content

Commit

Permalink
Merge pull request #7 from wrep/master
Browse files Browse the repository at this point in the history
Prevent overwriting/deletion of files when multiple passes are generated at the same time
  • Loading branch information
tschoffelen committed Sep 21, 2012
2 parents 8857fd3 + 064d675 commit 59fb1e4
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions PKPass.php
Expand Up @@ -63,9 +63,14 @@ class PKPass {
* Holds error info if an error occured
*/
private $sError = '';




/*
* Holds a autogenerated uniqid to prevent overwriting other processes pass files
*/
private $uniqid = null;



#################################
#########PUBLIC FUNCTIONS########
#################################
Expand Down Expand Up @@ -161,7 +166,7 @@ public function create($output = false) {
}

// Check if pass is created and valid
if(!file_exists($this->tempPath.'pass.pkpass') || filesize($this->tempPath.'pass.pkpass') < 1){
if(!file_exists($paths['pkpass']) || filesize($paths['pkpass']) < 1) {
$this->sError = 'Error while creating pass.pkpass. Check your Zip extension.';
$this->clean();
return false;
Expand Down Expand Up @@ -324,10 +329,19 @@ protected function paths() {
if(substr($this->tempPath, -1) != '/') {
$this->tempPath = $this->tempPath.'/';
}


//Generate a unique subfolder in the tempPath to support generating more passes at the same time without erasing/overwriting each others files
if (empty($this->uniqid)) {
$this->uniqid = uniqid('PKPass', true);

if (!is_dir($this->tempPath.$this->uniqid)) {
mkdir($this->tempPath.$this->uniqid);
}
}

//Add temp folder path
foreach($paths AS $pathName => $path) {
$paths[$pathName] = $this->tempPath.$path;
$paths[$pathName] = $this->tempPath.$this->uniqid.'/'.$path;
}

return $paths;
Expand All @@ -344,7 +358,12 @@ protected function clean() {
unlink($path);
}
}


//Remove our unique temporary folder
if (is_dir($this->tempPath.$this->uniqid)) {
rmdir($this->tempPath.$this->uniqid);
}

return true;
}
}

0 comments on commit 59fb1e4

Please sign in to comment.