Пример #1
0
 function testGetUniqueDirectory()
 {
     /* Get two random directory names; one with extra data and one
      * without.
      */
     $directoryA = FileUtility::getUniqueDirectory('attachments');
     $directoryB = FileUtility::getUniqueDirectory('attachments/', 'Extra Data!');
     /* Directories are also unique in time, with randomness added. */
     sleep(1);
     $directoryC = FileUtility::getUniqueDirectory('attachments');
     $directoryD = FileUtility::getUniqueDirectory('attachments');
     /* Make sure all directory names look like md5 strings. */
     $this->assertEqual(strlen($directoryA), 32, sprintf("'%s' should be 32 characters long", $directoryA));
     $this->assertEqual(strlen($directoryB), 32, sprintf("'%s' should be 32 characters long", $directoryB));
     $this->assertEqual(strlen($directoryC), 32, sprintf("'%s' should be 32 characters long", $directoryB));
     $this->assertEqual(strlen($directoryD), 32, sprintf("'%s' should be 32 characters long", $directoryB));
     /* Make sure extra data is actually being added (directory names
      * should not be identical).
      */
     $this->assertNotEqual($directoryA, $directoryB, sprintf("'%s' should not equal '%s'", $directoryA, $directoryB));
     $this->assertNotEqual($directoryA, $directoryC, sprintf("'%s' should not equal '%s'", $directoryA, $directoryC));
     $this->assertNotEqual($directoryA, $directoryD, sprintf("'%s' should not equal '%s'", $directoryA, $directoryD));
     $this->assertNotEqual($directoryB, $directoryC, sprintf("'%s' should not equal '%s'", $directoryB, $directoryC));
     $this->assertNotEqual($directoryB, $directoryD, sprintf("'%s' should not equal '%s'", $directoryB, $directoryD));
     $this->assertNotEqual($directoryC, $directoryD, sprintf("'%s' should not equal '%s'", $directoryC, $directoryD));
     /* No directory names should exist. */
     $this->assertFalse(file_exists('attachments/' . $directoryA), sprintf("'attachments/%s' should not exist", $directoryA));
     $this->assertFalse(file_exists('attachments/' . $directoryB), sprintf("'attachments/%s' should not exist", $directoryB));
     $this->assertFalse(file_exists('attachments/' . $directoryC), sprintf("'attachments/%s' should not exist", $directoryB));
     $this->assertFalse(file_exists('attachments/' . $directoryD), sprintf("'attachments/%s' should not exist", $directoryB));
 }
Пример #2
0
 private function _createDirectory($attachmentID, $storedFilename)
 {
     /* Make sure attachments exists. */
     if (!is_dir('./attachments')) {
         /* No? Create it. */
         @mkdir('./attachments', 0777);
         @touch('./attachments/index.php');
     }
     /* Attachments are first grouped under a directory for each site ID. */
     $siteDirectory = './attachments/site_' . $this->_siteID;
     /* Make sure the site directory exists. */
     if (!is_dir($siteDirectory)) {
         /* No? Create it. */
         @mkdir($siteDirectory, 0777);
     }
     /* Attachments are then grouped in groups of 1000 attachment IDs. */
     $IDGroupDirectory = sprintf('%s/%sxxx', $siteDirectory, (int) ($attachmentID / 1000));
     /* Make sure the attachment ID group directory exists. */
     if (!is_dir($IDGroupDirectory)) {
         /* No? Create it. */
         @mkdir($IDGroupDirectory, 0777);
     }
     /* If we had to create directories above, make sure that the creation
      * was successful.
      */
     if (!is_dir($IDGroupDirectory)) {
         $this->_isError = true;
         $this->_error = sprintf('Cannot create directory %s, and it does not already exist.', $IDGroupDirectory);
         return false;
     }
     /* Prevent webserver listing of new directories. */
     if (!file_exists($siteDirectory . '/index.php')) {
         @file_put_contents($siteDirectory . '/index.php', "\n");
     }
     if (!file_exists($IDGroupDirectory . '/index.php')) {
         @file_put_contents($IDGroupDirectory . '/index.php', "\n");
     }
     /* Get a unique directroy name in which to store the attachment. */
     $uniqueDirectory = sprintf('%s/%s/', $IDGroupDirectory, FileUtility::getUniqueDirectory($IDGroupDirectory, $storedFilename));
     /* Attempt to create a directory for this attachment ID. */
     if (!is_dir($uniqueDirectory)) {
         @mkdir($uniqueDirectory, 0777);
     }
     /* Was creation successful? */
     if (!is_dir($uniqueDirectory)) {
         $this->_isError = true;
         $this->_error = sprintf('Cannot create directory %s, and it does not already exist.', $uniqueDirectory);
         return false;
     }
     /* We probably don't have permission to chmod, but try anyway. */
     @chmod($uniqueDirectory, 0777);
     return $uniqueDirectory;
 }