Example #1
0
 /**
  * Returns an ArchiveInfo object for an embedded archive file with the contents
  * analyzed (initially without recursion). Calls to this method can also be
  * chained together to navigate the tree, e.g.:
  *
  *    $rar->getArchive('parent.rar')->getArchive('child.zip')->getFileList();
  *
  * @param   string   $filename   the embedded archive filename
  * @return  ArchiveInfo|boolean  false if an object can't be returned
  */
 public function getArchive($filename)
 {
     if (!$this->reader || !$this->allowsRecursion()) {
         return false;
     }
     // Check the cache first
     if (isset($this->archives[$filename])) {
         return $this->archives[$filename];
     }
     foreach ($this->reader->getFileList(true) as $file) {
         if ($file['name'] == $filename && isset($file['range'])) {
             // Create the new archive object
             $archive = new self();
             $archive->externalClients = $this->externalClients;
             $archive->extensions = $this->extensions;
             if ($this->inheritReaders) {
                 $archive->setReaders($this->readers, true);
             }
             // Extract any compressed data to a temporary file if supported
             if ($this->canExtract() && !empty($file['compressed']) && empty($file['pass'])) {
                 list($hash, $temp) = $this->getTempFileName("{$file['name']}:{$file['range']}");
                 if (!isset($this->tempFiles[$hash])) {
                     $this->reader->extractFile($file['name'], $temp);
                     @chmod($temp, 0777);
                     $this->tempFiles[$hash] = $temp;
                 }
                 if ($this->reader->error) {
                     $archive->error = $this->reader->error;
                     $archive->readers = array();
                 } else {
                     $archive->open($temp, $this->isFragment);
                     $archive->isTemporary = true;
                 }
                 return $archive;
             }
             // Otherwise we shouldn't process any files that are unreadable
             if (!empty($file['compressed']) || !empty($file['pass'])) {
                 $archive->readers = array();
             }
             // Try to parse the raw source file/data
             $range = explode('-', $file['range']);
             if ($this->file) {
                 $archive->open($this->file, $this->isFragment, $range);
             } else {
                 $archive->setData($this->data, $this->isFragment, $range);
             }
             // Make error messages more specific
             if (!empty($file['compressed'])) {
                 $archive->error = 'The archive is compressed and cannot be read';
             }
             if (!empty($file['pass']) || !empty($archive->isEncrypted)) {
                 $archive->error = 'The archive is encrypted and cannot be read';
             }
             return $archive;
         }
     }
     // Something went wrong
     return false;
 }