/** * Method to get the list of files for the field options. * Specify the target directory with a directory attribute * Attributes allow an exclude mask and stripping of extensions from file name. * Default attribute may optionally be set to null (no file) or -1 (use a default). * * @return array The field option objects. * * @since 1.0 */ protected function getOptions() { $options = array(); // Initialize some field attributes. $filter = (string) $this->element['filter']; $exclude = (string) $this->element['exclude']; $stripExt = (string) $this->element['stripext']; $hideNone = (string) $this->element['hide_none']; $hideDefault = (string) $this->element['hide_default']; // Get the path in which to search for file options. $path = (string) $this->element['directory']; if (!is_dir($path)) { $path = JPATH_ROOT . '/' . $path; } // Prepend some default options based on field attributes. if (!$hideNone) { $options[] = HtmlSelect::option('-1', Text::alt('JOPTION_DO_NOT_USE', preg_replace('/[^a-zA-Z0-9_\\-]/', '_', $this->fieldname))); } if (!$hideDefault) { $options[] = HtmlSelect::option('', Text::alt('JOPTION_USE_DEFAULT', preg_replace('/[^a-zA-Z0-9_\\-]/', '_', $this->fieldname))); } // Get a list of files in the search path with the given filter. $files = Folder::files($path, $filter); // Build the options list from the list of files. if (is_array($files)) { foreach ($files as $file) { // Check to see if the file is in the exclude mask. if ($exclude) { if (preg_match(chr(1) . $exclude . chr(1), $file)) { continue; } } // If the extension is to be stripped, do it. if ($stripExt) { $file = File::stripExt($file); } $options[] = HtmlSelect::option($file, $file); } } // Merge any additional options in the XML definition. $options = array_merge(parent::getOptions(), $options); return $options; }
/** * Extract an archive file to a directory. * * @param string $archivename The name of the archive file * @param string $extractdir Directory to unpack into * * @return boolean True for success * * @since 11.1 * @throws InvalidArgumentException */ public static function extract($archivename, $extractdir) { $untar = false; $result = false; $ext = File::getExt(strtolower($archivename)); // Check if a tar is embedded...gzip/bzip2 can just be plain files! if (File::getExt(File::stripExt(strtolower($archivename))) == 'tar') { $untar = true; } switch ($ext) { case 'zip': $adapter = self::getAdapter('zip'); if ($adapter) { $result = $adapter->extract($archivename, $extractdir); } break; case 'tar': $adapter = self::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 = self::getAdapter('gzip'); if ($adapter) { $config = Factory::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 = self::getAdapter('tar'); if ($tadapter) { $result = $tadapter->extract($tmpfname, $extractdir); } } else { $path = Path::clean($extractdir); Folder::create($path); $result = File::copy($tmpfname, $path . '/' . File::stripExt(basename(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 = self::getAdapter('bzip2'); if ($adapter) { $config = Factory::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 = self::getAdapter('tar'); if ($tadapter) { $result = $tadapter->extract($tmpfname, $extractdir); } } else { $path = Path::clean($extractdir); Folder::create($path); $result = File::copy($tmpfname, $path . '/' . File::stripExt(basename(strtolower($archivename))), null, 1); } @unlink($tmpfname); } break; default: throw new InvalidArgumentException('Unknown Archive Type'); } if (!$result || $result instanceof Exception) { return false; } return true; }
/** * Test makeSafe method * * @param string $fileName The name of the file with extension * @param string $nameWithoutExt Name without extension * * @return void * * @covers Joomla\Filesystem\File::stripExt * @dataProvider dataTestStripExt * @since 1.0 */ public function testStripExt($fileName, $nameWithoutExt) { $this->assertEquals($this->object->stripExt($fileName), $nameWithoutExt, 'Line:' . __LINE__ . ' file extension should be stripped.'); }
/** * Compute the files to be included * * @param string $folder folder name to search into (images, css, js, ...) * @param string $file path to file * @param boolean $relative path to file is relative to /media folder * @param boolean $detect_browser detect browser to include specific browser files * @param boolean $detect_debug detect debug to include compressed files if debug is on * * @return array files to be included * * @see JBrowser * @since 11.1 */ 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 = File::stripExt($file); $ext = File::getExt($file); // Prepare array of files $includes = array(); // Detect browser and compute potential files if ($detect_browser) { $navigator = Browser::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 = Factory::getApplication(); $template = $app->getTemplate(); // For each potential files foreach ($potential as $strip) { $files = array(); // Detect debug mode if ($detect_debug && Factory::getConfig()->get('debug')) { /* * Detect if we received a file in the format name.min.ext * If so, strip the .min part out, otherwise append -uncompressed */ if (strrpos($strip, '.min', '-4')) { $position = strrpos($strip, '.min', '-4'); $filename = str_replace('.min', '.', $strip, $position); $files[] = $filename . $ext; } else { $files[] = $strip . '-uncompressed.' . $ext; } } $files[] = $strip . '.' . $ext; /* * Loop on 1 or 2 files and break on first found. * Add the content of the MD5SUM file located in the same folder to url to ensure cache browser refresh * This MD5SUM file must represent the signature of the folder content */ foreach ($files as $file) { // If the file is in the template folder $path = JPATH_THEMES . "/{$template}/{$folder}/{$file}"; if (file_exists($path)) { $md5 = dirname($path) . '/MD5SUM'; $includes[] = Uri::base(true) . "/templates/{$template}/{$folder}/{$file}" . (file_exists($md5) ? '?' . file_get_contents($md5) : ''); 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 $path = JPATH_ROOT . "/media/{$extension}/{$element}/{$folder}/{$file}"; if (file_exists($path)) { $md5 = dirname($path) . '/MD5SUM'; $includes[] = Uri::root(true) . "/media/{$extension}/{$element}/{$folder}/{$file}" . (file_exists($md5) ? '?' . file_get_contents($md5) : ''); break; } // Try to deal with classical file in a a media subfolder called element $path = JPATH_ROOT . "/media/{$extension}/{$folder}/{$element}/{$file}"; if (file_exists($path)) { $md5 = dirname($path) . '/MD5SUM'; $includes[] = Uri::root(true) . "/media/{$extension}/{$folder}/{$element}/{$file}" . (file_exists($md5) ? '?' . file_get_contents($md5) : ''); break; } // Try to deal with system files in the template folder $path = JPATH_THEMES . "/{$template}/{$folder}/system/{$element}/{$file}"; if (file_exists($path)) { $md5 = dirname($path) . '/MD5SUM'; $includes[] = Uri::root(true) . "/templates/{$template}/{$folder}/system/{$element}/{$file}" . (file_exists($md5) ? '?' . file_get_contents($md5) : ''); break; } // Try to deal with system files in the media folder $path = JPATH_ROOT . "/media/system/{$folder}/{$element}/{$file}"; if (file_exists($path)) { $md5 = dirname($path) . '/MD5SUM'; $includes[] = Uri::root(true) . "/media/system/{$folder}/{$element}/{$file}" . (file_exists($md5) ? '?' . file_get_contents($md5) : ''); break; } } else { // Try to deals in the extension media folder $path = JPATH_ROOT . "/media/{$extension}/{$folder}/{$file}"; if (file_exists($path)) { $md5 = dirname($path) . '/MD5SUM'; $includes[] = Uri::root(true) . "/media/{$extension}/{$folder}/{$file}" . (file_exists($md5) ? '?' . file_get_contents($md5) : ''); break; } // Try to deal with system files in the template folder $path = JPATH_THEMES . "/{$template}/{$folder}/system/{$file}"; if (file_exists($path)) { $md5 = dirname($path) . '/MD5SUM'; $includes[] = Uri::root(true) . "/templates/{$template}/{$folder}/system/{$file}" . (file_exists($md5) ? '?' . file_get_contents($md5) : ''); break; } // Try to deal with system files in the media folder $path = JPATH_ROOT . "/media/system/{$folder}/{$file}"; if (file_exists($path)) { $md5 = dirname($path) . '/MD5SUM'; $includes[] = Uri::root(true) . "/media/system/{$folder}/{$file}" . (file_exists($md5) ? '?' . file_get_contents($md5) : ''); break; } } } else { $path = JPATH_ROOT . "/media/system/{$folder}/{$file}"; if (file_exists($path)) { $md5 = dirname($path) . '/MD5SUM'; $includes[] = Uri::root(true) . "/media/system/{$folder}/{$file}" . (file_exists($md5) ? '?' . file_get_contents($md5) : ''); break; } } } } } } else { foreach ($potential as $strip) { $files = array(); // Detect debug mode if ($detect_debug && Factory::getConfig()->get('debug')) { $files[] = $strip . '-uncompressed.' . $ext; } $files[] = $strip . '.' . $ext; /* * Loop on 1 or 2 files and break on first found. * Add the content of the MD5SUM file located in the same folder to url to ensure cache browser refresh * This MD5SUM file must represent the signature of the folder content */ foreach ($files as $file) { $path = JPATH_ROOT . "/{$file}"; if (file_exists($path)) { $md5 = dirname($path) . '/MD5SUM'; $includes[] = Uri::root(true) . "/{$file}" . (file_exists($md5) ? '?' . file_get_contents($md5) : ''); break; } } } } } return $includes; }