public static function extract($archivename, $extractdir) { mimport('framework.filesystem.file'); mimport('framework.filesystem.folder'); $untar = false; $result = false; $ext = MFile::getExt(strtolower($archivename)); // Check if a tar is embedded...gzip/bzip2 can just be plain files! if (MFile::getExt(MFile::stripExt(strtolower($archivename))) == 'tar') { $untar = true; } switch ($ext) { case 'zip': $adapter = MArchive::getAdapter('zip'); if ($adapter) { $result = $adapter->extract($archivename, $extractdir); } break; case 'tar': $adapter = MArchive::getAdapter('tar'); if ($adapter) { $result = $adapter->extract($archivename, $extractdir); } break; case 'tgz': // This format is a tarball gzip'd $untar = true; case 'gz': case 'gzip': // This may just be an individual file (e.g. sql script) $adapter = MArchive::getAdapter('gzip'); if ($adapter) { $config = MFactory::getConfig(); $tmpfname = $config->get('tmp_path') . '/' . uniqid('gzip'); $gzresult = $adapter->extract($archivename, $tmpfname); if ($gzresult instanceof Exception) { @unlink($tmpfname); return false; } if ($untar) { // Try to untar the file $tadapter = MArchive::getAdapter('tar'); if ($tadapter) { $result = $tadapter->extract($tmpfname, $extractdir); } } else { $path = MPath::clean($extractdir); MFolder::create($path); $result = MFile::copy($tmpfname, $path . '/' . MFile::stripExt(MFile::getName(strtolower($archivename))), null, 1); } @unlink($tmpfname); } break; case 'tbz2': // This format is a tarball bzip2'd $untar = true; case 'bz2': case 'bzip2': // This may just be an individual file (e.g. sql script) $adapter = MArchive::getAdapter('bzip2'); if ($adapter) { $config = MFactory::getConfig(); $tmpfname = $config->get('tmp_path') . '/' . uniqid('bzip2'); $bzresult = $adapter->extract($archivename, $tmpfname); if ($bzresult instanceof Exception) { @unlink($tmpfname); return false; } if ($untar) { // Try to untar the file $tadapter = MArchive::getAdapter('tar'); if ($tadapter) { $result = $tadapter->extract($tmpfname, $extractdir); } } else { $path = MPath::clean($extractdir); MFolder::create($path); $result = MFile::copy($tmpfname, $path . '/' . MFile::stripExt(MFile::getName(strtolower($archivename))), null, 1); } @unlink($tmpfname); } break; default: MError::raiseWarning(10, MText::_('MLIB_FILESYSTEM_UNKNOWNARCHIVETYPE')); return false; break; } if (!$result || $result instanceof Exception) { return false; } return true; }
public function open($filename, $mode = 'r', $use_include_path = false, $context = null, $use_prefix = false, $relative = false, $detectprocessingmode = false) { $filename = $this->_getFilename($filename, $mode, $use_prefix, $relative); if (!$filename) { $this->setError(MText::_('MLIB_FILESYSTEM_ERROR_STREAMS_FILENAME')); return false; } $this->filename = $filename; $this->_openmode = $mode; $url = parse_url($filename); $retval = false; if (isset($url['scheme'])) { // If we're dealing with a Moomla! stream, load it if (MFilesystemHelper::isMiwiStream($url['scheme'])) { require_once dirname(__FILE__) . '/streams/' . $url['scheme'] . '.php'; } // We have a scheme! force the method to be f $this->processingmethod = 'f'; } elseif ($detectprocessingmode) { $ext = strtolower(MFile::getExt($this->filename)); switch ($ext) { case 'tgz': case 'gz': case 'gzip': $this->processingmethod = 'gz'; break; case 'tbz2': case 'bz2': case 'bzip2': $this->processingmethod = 'bz'; break; default: $this->processingmethod = 'f'; break; } } // Capture PHP errors $php_errormsg = 'Error Unknown whilst opening a file'; $track_errors = ini_get('track_errors'); ini_set('track_errors', true); // Decide which context to use: switch ($this->processingmethod) { // gzip doesn't support contexts or streams case 'gz': $this->_fh = gzopen($filename, $mode, $use_include_path); break; // bzip2 is much like gzip except it doesn't use the include path // bzip2 is much like gzip except it doesn't use the include path case 'bz': $this->_fh = bzopen($filename, $mode); break; // fopen can handle streams // fopen can handle streams case 'f': default: // One supplied at open; overrides everything if ($context) { $this->_fh = fopen($filename, $mode, $use_include_path, $context); } elseif ($this->_context) { $this->_fh = fopen($filename, $mode, $use_include_path, $this->_context); } else { $this->_fh = fopen($filename, $mode, $use_include_path); } break; } if (!$this->_fh) { $this->setError($php_errormsg); } else { $retval = true; } // Restore error tracking to what it was before ini_set('track_errors', $track_errors); // Return the result return $retval; }
protected static function includeRelativeFiles($folder, $file, $relative, $detect_browser, $detect_debug) { // If http is present in filename if (strpos($file, 'http') === 0) { $includes = array($file); } else { // Extract extension and strip the file $strip = MFile::stripExt($file); $ext = MFile::getExt($file); // Detect browser and compute potential files if ($detect_browser) { $navigator = MBrowser::getInstance(); $browser = $navigator->getBrowser(); $major = $navigator->getMajor(); $minor = $navigator->getMinor(); // Try to include files named filename.ext, filename_browser.ext, filename_browser_major.ext, filename_browser_major_minor.ext // where major and minor are the browser version names $potential = array($strip, $strip . '_' . $browser, $strip . '_' . $browser . '_' . $major, $strip . '_' . $browser . '_' . $major . '_' . $minor); } else { $potential = array($strip); } // If relative search in template directory or media directory if ($relative) { // Get the template $app = MFactory::getApplication(); $template = $app->getTemplate(); // Prepare array of files $includes = array(); // For each potential files foreach ($potential as $strip) { $files = array(); // Detect debug mode if ($detect_debug && MFactory::getConfig()->get('debug')) { $files[] = $strip . '-uncompressed.' . $ext; } $files[] = $strip . '.' . $ext; // Loop on 1 or 2 files and break on first found foreach ($files as $file) { // If the file is in the template folder if (file_exists(MPATH_THEMES . "/{$template}/{$folder}/{$file}")) { $includes[] = MURI::base(true) . "/templates/{$template}/{$folder}/{$file}"; break; } else { // If the file contains any /: it can be in an media extension subfolder if (strpos($file, '/')) { // Divide the file extracting the extension as the first part before / list($extension, $file) = explode('/', $file, 2); // If the file yet contains any /: it can be a plugin if (strpos($file, '/')) { // Divide the file extracting the element as the first part before / list($element, $file) = explode('/', $file, 2); // Try to deal with plugins group in the media folder if (file_exists(MPATH_ROOT . "/media/{$extension}/{$element}/{$folder}/{$file}")) { $includes[] = MURL_WP_CNT . "/miwi/media/{$extension}/{$element}/{$folder}/{$file}"; break; } elseif (file_exists(MPATH_ROOT . "/media/{$extension}/{$folder}/{$element}/{$file}")) { $includes[] = MURL_WP_CNT . "/miwi/media/{$extension}/{$folder}/{$element}/{$file}"; break; } elseif (file_exists(MPATH_THEMES . "/{$template}/{$folder}/system/{$element}/{$file}")) { $includes[] = MURL_WP_CNT . "/miwi/templates/{$template}/{$folder}/system/{$element}/{$file}"; break; } elseif (file_exists(MPATH_ROOT . "/media/system/{$folder}/{$element}/{$file}")) { $includes[] = MURL_WP_CNT . "/miwi/media/system/{$folder}/{$element}/{$file}"; break; } } elseif (file_exists(MPATH_ROOT . "/media/{$extension}/{$folder}/{$file}")) { $includes[] = MURL_WP_CNT . "/miwi/media/{$extension}/{$folder}/{$file}"; break; } elseif (file_exists(MPATH_THEMES . "/{$template}/{$folder}/system/{$file}")) { $includes[] = MURL_WP_CNT . "/miwi/templates/{$template}/{$folder}/system/{$file}"; break; } elseif (file_exists(MPATH_ROOT . "/media/system/{$folder}/{$file}")) { $includes[] = MURL_WP_CNT . "/miwi/media/system/{$folder}/{$file}"; break; } } elseif (file_exists(MPATH_ROOT . "/media/system/{$folder}/{$file}")) { $includes[] = MURL_WP_CNT . "/miwi/media/system/{$folder}/{$file}"; break; } } } } } else { $includes = array(); foreach ($potential as $strip) { // Detect debug mode if ($detect_debug && MFactory::getConfig()->get('debug') && file_exists(MPATH_ROOT . "/{$strip}-uncompressed.{$ext}")) { $includes[] = MURI::root(false) . "/{$strip}-uncompressed.{$ext}"; } elseif (file_exists(MPATH_ROOT . "/{$strip}.{$ext}")) { $includes[] = MURI::root(false) . "/{$strip}.{$ext}"; } } } } return $includes; }