/**
  * Class constructor
  *
  * @param   array  $options  Associative array of options
  *
  * @since  11.1
  */
 public function __construct($options = array())
 {
     parent::__construct($options);
     // Set document type
     $this->_type = 'opensearch';
     // Set mime type
     $this->_mime = 'application/opensearchdescription+xml';
     // Add the URL for self updating
     $update = new Opensearch\Url();
     $update->type = 'application/opensearchdescription+xml';
     $update->rel = 'self';
     $update->template = Route::_(Uri::getInstance());
     $this->addUrl($update);
     // Add the favicon as the default image
     // Try to find a favicon by checking the template and root folder
     $app = Factory::getApplication();
     $dirs = array(JPATH_THEMES . '/' . $app->getTemplate(), JPATH_BASE);
     foreach ($dirs as $dir) {
         if (file_exists($dir . '/favicon.ico')) {
             $path = str_replace(JPATH_BASE . '/', '', $dir);
             $path = str_replace('\\', '/', $path);
             $favicon = new Opensearch\Image();
             $favicon->data = Uri::base() . $path . '/favicon.ico';
             $favicon->height = '16';
             $favicon->width = '16';
             $favicon->type = 'image/vnd.microsoft.icon';
             $this->addImage($favicon);
             break;
         }
     }
 }
 /**
  * Render the document
  *
  * @param   boolean  $cache   If true, cache the output
  * @param   array    $params  Associative array of attributes
  *
  * @return  string   The rendered data
  *
  * @since   11.1
  */
 public function render($cache = false, $params = array())
 {
     // If no error object is set return null
     if (!isset($this->_error)) {
         return;
     }
     // Set the status header
     Response::setHeader('status', $this->_error->getCode() . ' ' . str_replace("\n", ' ', $this->_error->getMessage()));
     $file = 'error.php';
     // Check template
     $directory = isset($params['directory']) ? $params['directory'] : 'templates';
     $template = isset($params['template']) ? FilterInput::getInstance()->clean($params['template'], 'cmd') : 'system';
     if (!file_exists($directory . '/' . $template . '/' . $file)) {
         $template = 'system';
     }
     // Set variables
     $this->baseurl = Uri::base(true);
     $this->template = $template;
     $this->debug = isset($params['debug']) ? $params['debug'] : false;
     $this->error = $this->_error;
     // Load
     $data = $this->_loadTemplate($directory . '/' . $template, $file);
     parent::render();
     return $data;
 }
 /**
  * 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;
 }
 /**
  * Fetch the template, and initialise the params
  *
  * @param   array  $params  Parameters to determine the template
  *
  * @return  JDocumentHTML instance of $this to allow chaining
  *
  * @since   11.1
  */
 protected function _fetchTemplate($params = array())
 {
     // Check
     $directory = isset($params['directory']) ? $params['directory'] : 'templates';
     $filter = FilterInput::getInstance();
     $template = $filter->clean($params['template'], 'cmd');
     $file = $filter->clean($params['file'], 'cmd');
     if (!file_exists($directory . '/' . $template . '/' . $file)) {
         $template = 'system';
     }
     // Load the language file for the template
     $lang = JFactory::getLanguage();
     // 1.5 or core then 1.6
     $lang->load('tpl_' . $template, JPATH_BASE, null, false, false) || $lang->load('tpl_' . $template, $directory . '/' . $template, null, false, false) || $lang->load('tpl_' . $template, JPATH_BASE, $lang->getDefault(), false, false) || $lang->load('tpl_' . $template, $directory . '/' . $template, $lang->getDefault(), false, false);
     // Assign the variables
     $this->template = $template;
     $this->baseurl = Uri::base(true);
     $this->params = isset($params['params']) ? $params['params'] : new JRegistry();
     // Load
     $this->_template = $this->_loadTemplate($directory . '/' . $template, $file);
     return $this;
 }
 /**
  * Convert links in a text from relative to absolute
  *
  * @param   string  $text  The text processed
  *
  * @return  string   Text with converted links
  *
  * @since   11.1
  */
 public function _relToAbs($text)
 {
     $base = Uri::base();
     $text = preg_replace("/(href|src)=\"(?!http|ftp|https|mailto|data)([^\"]*)\"/", "\$1=\"{$base}\$2\"", $text);
     return $text;
 }