Back to HTMLHelper class

Method includeRelativeFiles

protected static array
includeRelativeFiles
(mixed $folder, mixed $file, mixed $relative, mixed $detectBrowser, mixed $detectDebug)
Compute the files to be included
Parameters
  • string $folder Folder name to search in (i.e. images, css, js).
  • string $file Path to file.
  • bool $relative Flag if the path to the file is relative to the /media folder (and searches in template).
  • bool $detectBrowser Flag if the browser should be detected to include specific browser files.
  • bool $detectDebug Flag if debug mode is enabled to include uncompressed files if debug is on.
Returns
  • array files to be included.
Since
  • 1.6
-
  • \Joomla\CMS\Environment\Browser
Class: HTMLHelper
Project: Joomla

Method includeRelativeFiles - Source code

/**
 * Compute the files to be included
 *
 * @param   string   $folder         Folder name to search in (i.e. images, css, js).
 * @param   string   $file           Path to file.
 * @param   boolean  $relative       Flag if the path to the file is relative to the /media folder (and searches in template).
 * @param   boolean  $detectBrowser  Flag if the browser should be detected to include specific browser files.
 * @param   boolean  $detectDebug    Flag if debug mode is enabled to include uncompressed files if debug is on.
 *
 * @return  array    files to be included.
 *
 * @see     Browser
 * @since   1.6
 */
protected static function includeRelativeFiles($folder, $file, $relative, $detectBrowser, $detectDebug)
{
    // Set debug flag
    $debugMode = false;
    // Detect debug mode
    if ($detectDebug && JDEBUG) {
        $debugMode = true;
    }
    // If http is present in filename
    if (strpos($file, 'http') === 0 || strpos($file, '//') === 0) {
        $includes = [$file];
    } else {
        // Extract extension and strip the file
        $strip = File::stripExt($file);
        $ext = File::getExt($file);
        // Prepare array of files
        $includes = [];
        // Detect browser and compute potential files
        if ($detectBrowser) {
            $navigator = Browser::getInstance();
            $browser = $navigator->getBrowser();
            $major = $navigator->getMajor();
            $minor = $navigator->getMinor();
            $minExt = '';
            if (\strlen($strip) > 4 && preg_match('#\\.min$#', $strip)) {
                $minExt = '.min';
                $strip = preg_replace('#\\.min$#', '', $strip);
            }
            // 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 = [$strip . $minExt, $strip . '_' . $browser . $minExt, $strip . '_' . $browser . '_' . $major . $minExt, $strip . '_' . $browser . '_' . $major . '_' . $minor . $minExt];
        } else {
            $potential = [$strip];
        }
        // If relative search in template directory or media directory
        if ($relative) {
            $app = Factory::getApplication();
            $template = $app->getTemplate(true);
            $templaPath = JPATH_THEMES;
            if ($template->inheritable || !empty($template->parent)) {
                $client = $app->isClient('administrator') === true ? 'administrator' : 'site';
                $templaPath = JPATH_ROOT . "/media/templates/{$client}";
            }
            // For each potential files
            foreach ($potential as $strip) {
                $files = [];
                $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 (!empty($template->parent)) {
                        $found = static::addFileToBuffer("{$templaPath}/{$template->template}/{$folder}/{$file}", $ext, $debugMode);
                        if (empty($found)) {
                            $found = static::addFileToBuffer("{$templaPath}/{$template->parent}/{$folder}/{$file}", $ext, $debugMode);
                        }
                    } else {
                        $found = static::addFileToBuffer("{$templaPath}/{$template->template}/{$folder}/{$file}", $ext, $debugMode);
                    }
                    if (!empty($found)) {
                        $includes[] = $found;
                        break;
                    } else {
                        // If the file contains any /: it can be in a 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
                                $found = static::addFileToBuffer(JPATH_ROOT . "/media/{$extension}/{$element}/{$folder}/{$file}", $ext, $debugMode);
                                if (!empty($found)) {
                                    $includes[] = $found;
                                    break;
                                }
                                // Try to deal with classical file in a media subfolder called element
                                $found = static::addFileToBuffer(JPATH_ROOT . "/media/{$extension}/{$folder}/{$element}/{$file}", $ext, $debugMode);
                                if (!empty($found)) {
                                    $includes[] = $found;
                                    break;
                                }
                                // Try to deal with system files in the template folder
                                if (!empty($template->parent)) {
                                    $found = static::addFileToBuffer("{$templaPath}/{$template->template}/{$folder}/system/{$element}/{$file}", $ext, $debugMode);
                                    if (!empty($found)) {
                                        $includes[] = $found;
                                        break;
                                    }
                                    $found = static::addFileToBuffer("{$templaPath}/{$template->parent}/{$folder}/system/{$element}/{$file}", $ext, $debugMode);
                                    if (!empty($found)) {
                                        $includes[] = $found;
                                        break;
                                    }
                                } else {
                                    // Try to deal with system files in the media folder
                                    $found = static::addFileToBuffer(JPATH_ROOT . "/media/system/{$folder}/{$element}/{$file}", $ext, $debugMode);
                                    if (!empty($found)) {
                                        $includes[] = $found;
                                        break;
                                    }
                                }
                            } else {
                                // Try to deal with files in the extension's media folder
                                $found = static::addFileToBuffer(JPATH_ROOT . "/media/{$extension}/{$folder}/{$file}", $ext, $debugMode);
                                if (!empty($found)) {
                                    $includes[] = $found;
                                    break;
                                }
                                // Try to deal with system files in the template folder
                                if (!empty($template->parent)) {
                                    $found = static::addFileToBuffer("{$templaPath}/{$template->template}/{$folder}/system/{$file}", $ext, $debugMode);
                                    if (!empty($found)) {
                                        $includes[] = $found;
                                        break;
                                    }
                                    $found = static::addFileToBuffer("{$templaPath}/{$template->parent}/{$folder}/system/{$file}", $ext, $debugMode);
                                    if (!empty($found)) {
                                        $includes[] = $found;
                                        break;
                                    }
                                } else {
                                    // Try to deal with system files in the template folder
                                    $found = static::addFileToBuffer("{$templaPath}/{$template->template}/{$folder}/system/{$file}", $ext, $debugMode);
                                    if (!empty($found)) {
                                        $includes[] = $found;
                                        break;
                                    }
                                }
                                // Try to deal with system files in the media folder
                                $found = static::addFileToBuffer(JPATH_ROOT . "/media/system/{$folder}/{$file}", $ext, $debugMode);
                                if (!empty($found)) {
                                    $includes[] = $found;
                                    break;
                                }
                            }
                        } else {
                            // Try to deal with system files in the media folder
                            $found = static::addFileToBuffer(JPATH_ROOT . "/media/system/{$folder}/{$file}", $ext, $debugMode);
                            if (!empty($found)) {
                                $includes[] = $found;
                                break;
                            }
                        }
                    }
                }
            }
        } else {
            // If not relative and http is not present in filename
            foreach ($potential as $strip) {
                $files = [];
                $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}";
                    $found = static::addFileToBuffer($path, $ext, $debugMode);
                    if (!empty($found)) {
                        $includes[] = $found;
                        break;
                    }
                }
            }
        }
    }
    return $includes;
}