/**
  * 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 feed.
  *
  * @param   string  $name     The name of the element to render
  * @param   array   $params   Array of values
  * @param   string  $content  Override the output of the renderer
  *
  * @return  string  The output of the script
  *
  * @see JDocumentRenderer::render()
  * @since   11.1
  */
 public function render($name = '', $params = null, $content = null)
 {
     $app = Factory::getApplication();
     // Gets and sets timezone offset from site configuration
     $tz = new DateTimeZone($app->getCfg('offset'));
     $now = Factory::getDate();
     $now->setTimeZone($tz);
     $data = $this->_doc;
     $uri = Uri::getInstance();
     $url = $uri->toString(array('scheme', 'user', 'pass', 'host', 'port'));
     $syndicationURL = Route::_('&format=feed&type=atom');
     if ($app->getCfg('sitename_pagetitles', 0) == 1) {
         $title = Text::sprintf('JPAGETITLE', $app->getCfg('sitename'), $data->title);
     } elseif ($app->getCfg('sitename_pagetitles', 0) == 2) {
         $title = Text::sprintf('JPAGETITLE', $data->title, $app->getCfg('sitename'));
     } else {
         $title = $data->title;
     }
     $feed_title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8');
     $feed = "<feed xmlns=\"http://www.w3.org/2005/Atom\" ";
     if ($data->language != "") {
         $feed .= " xml:lang=\"" . $data->language . "\"";
     }
     $feed .= ">\n";
     $feed .= "\t<title type=\"text\">" . $feed_title . "</title>\n";
     $feed .= "\t<subtitle type=\"text\">" . htmlspecialchars($data->description, ENT_COMPAT, 'UTF-8') . "</subtitle>\n";
     if (empty($data->category) === false) {
         if (is_array($data->category)) {
             foreach ($data->category as $cat) {
                 $feed .= "\t<category term=\"" . htmlspecialchars($cat, ENT_COMPAT, 'UTF-8') . "\" />\n";
             }
         } else {
             $feed .= "\t<category term=\"" . htmlspecialchars($data->category, ENT_COMPAT, 'UTF-8') . "\" />\n";
         }
     }
     $feed .= "\t<link rel=\"alternate\" type=\"text/html\" href=\"" . $url . "\"/>\n";
     $feed .= "\t<id>" . str_replace(' ', '%20', $data->getBase()) . "</id>\n";
     $feed .= "\t<updated>" . htmlspecialchars($now->toISO8601(true), ENT_COMPAT, 'UTF-8') . "</updated>\n";
     if ($data->editor != "") {
         $feed .= "\t<author>\n";
         $feed .= "\t\t<name>" . $data->editor . "</name>\n";
         if ($data->editorEmail != "") {
             $feed .= "\t\t<email>" . htmlspecialchars($data->editorEmail, ENT_COMPAT, 'UTF-8') . "</email>\n";
         }
         $feed .= "\t</author>\n";
     }
     $feed .= "\t<generator uri=\"http://joomla.org\" version=\"1.6\">" . $data->getGenerator() . "</generator>\n";
     $feed .= '	<link rel="self" type="application/atom+xml" href="' . str_replace(' ', '%20', $url . $syndicationURL) . "\"/>\n";
     for ($i = 0, $count = count($data->items); $i < $count; $i++) {
         $feed .= "\t<entry>\n";
         $feed .= "\t\t<title>" . htmlspecialchars(strip_tags($data->items[$i]->title), ENT_COMPAT, 'UTF-8') . "</title>\n";
         $feed .= '		<link rel="alternate" type="text/html" href="' . $url . $data->items[$i]->link . "\"/>\n";
         if ($data->items[$i]->date == "") {
             $data->items[$i]->date = $now->toUnix();
         }
         $itemDate = Factory::getDate($data->items[$i]->date);
         $itemDate->setTimeZone($tz);
         $feed .= "\t\t<published>" . htmlspecialchars($itemDate->toISO8601(true), ENT_COMPAT, 'UTF-8') . "</published>\n";
         $feed .= "\t\t<updated>" . htmlspecialchars($itemDate->toISO8601(true), ENT_COMPAT, 'UTF-8') . "</updated>\n";
         if (empty($data->items[$i]->guid) === true) {
             $feed .= "\t\t<id>" . str_replace(' ', '%20', $url . $data->items[$i]->link) . "</id>\n";
         } else {
             $feed .= "\t\t<id>" . htmlspecialchars($data->items[$i]->guid, ENT_COMPAT, 'UTF-8') . "</id>\n";
         }
         if ($data->items[$i]->author != "") {
             $feed .= "\t\t<author>\n";
             $feed .= "\t\t\t<name>" . htmlspecialchars($data->items[$i]->author, ENT_COMPAT, 'UTF-8') . "</name>\n";
             if ($data->items[$i]->authorEmail != "") {
                 $feed .= "\t\t\t<email>" . htmlspecialchars($data->items[$i]->authorEmail, ENT_COMPAT, 'UTF-8') . "</email>\n";
             }
             $feed .= "\t\t</author>\n";
         }
         if ($data->items[$i]->description != "") {
             $feed .= "\t\t<summary type=\"html\">" . htmlspecialchars($data->items[$i]->description, ENT_COMPAT, 'UTF-8') . "</summary>\n";
             $feed .= "\t\t<content type=\"html\">" . htmlspecialchars($data->items[$i]->description, ENT_COMPAT, 'UTF-8') . "</content>\n";
         }
         if (empty($data->items[$i]->category) === false) {
             if (is_array($data->items[$i]->category)) {
                 foreach ($data->items[$i]->category as $cat) {
                     $feed .= "\t\t<category term=\"" . htmlspecialchars($cat, ENT_COMPAT, 'UTF-8') . "\" />\n";
                 }
             } else {
                 $feed .= "\t\t<category term=\"" . htmlspecialchars($data->items[$i]->category, ENT_COMPAT, 'UTF-8') . "\" />\n";
             }
         }
         if ($data->items[$i]->enclosure != null) {
             $feed .= "\t\t<link rel=\"enclosure\" href=\"" . $data->items[$i]->enclosure->url . "\" type=\"" . $data->items[$i]->enclosure->type . "\"  length=\"" . $data->items[$i]->enclosure->length . "\" />\n";
         }
         $feed .= "\t</entry>\n";
     }
     $feed .= "</feed>\n";
     return $feed;
 }
 /**
  * Checks for a form token in the request.
  *
  * Use in conjunction with JHtml::_('form.token') or JSession::getFormToken.
  *
  * @param   string  $method  The request method in which to look for the token key.
  *
  * @return  boolean  True if found and valid, false otherwise.
  *
  * @since   12.1
  */
 public static function checkToken($method = 'post')
 {
     $token = self::getFormToken();
     $app = Factory::getApplication();
     if (!$app->input->{$method}->get($token, '', 'alnum')) {
         $session = Factory::getSession();
         if ($session->isNew()) {
             // Redirect to login screen.
             $app->redirect(Route::_('index.php'), Text::_('JLIB_ENVIRONMENT_SESSION_EXPIRED'));
             $app->close();
         } else {
             return false;
         }
     } else {
         return true;
     }
 }
 /**
  * Create and return the pagination data object.
  *
  * @return  object  Pagination data object.
  *
  * @since   11.1
  */
 protected function _buildDataObject()
 {
     $data = new stdClass();
     // Build the additional URL parameters string.
     $params = '';
     if (!empty($this->additionalUrlParams)) {
         foreach ($this->additionalUrlParams as $key => $value) {
             $params .= '&' . $key . '=' . $value;
         }
     }
     $data->all = new Object(Text::_('JLIB_HTML_VIEW_ALL'), $this->prefix);
     if (!$this->viewall) {
         $data->all->base = '0';
         $data->all->link = Route::_($params . '&' . $this->prefix . 'limitstart=');
     }
     // Set the start and previous data objects.
     $data->start = new Object(Text::_('JLIB_HTML_START'), $this->prefix);
     $data->previous = new Object(Text::_('JPREV'), $this->prefix);
     if ($this->pagesCurrent > 1) {
         $page = ($this->pagesCurrent - 2) * $this->limit;
         // Set the empty for removal from route
         // @todo remove code: $page = $page == 0 ? '' : $page;
         $data->start->base = '0';
         $data->start->link = Route::_($params . '&' . $this->prefix . 'limitstart=0');
         $data->previous->base = $page;
         $data->previous->link = Route::_($params . '&' . $this->prefix . 'limitstart=' . $page);
     }
     // Set the next and end data objects.
     $data->next = new Object(Text::_('JNEXT'), $this->prefix);
     $data->end = new Object(Text::_('JLIB_HTML_END'), $this->prefix);
     if ($this->pagesCurrent < $this->pagesTotal) {
         $next = $this->pagesCurrent * $this->limit;
         $end = ($this->pagesTotal - 1) * $this->limit;
         $data->next->base = $next;
         $data->next->link = Route::_($params . '&' . $this->prefix . 'limitstart=' . $next);
         $data->end->base = $end;
         $data->end->link = Route::_($params . '&' . $this->prefix . 'limitstart=' . $end);
     }
     $data->pages = array();
     $stop = $this->pagesStop;
     for ($i = $this->pagesStart; $i <= $stop; $i++) {
         $offset = ($i - 1) * $this->limit;
         $data->pages[$i] = new Object($i, $this->prefix);
         if ($i != $this->pagesCurrent || $this->viewall) {
             $data->pages[$i]->base = $offset;
             $data->pages[$i]->link = Route::_($params . '&' . $this->prefix . 'limitstart=' . $offset);
         } else {
             $data->pages[$i]->active = true;
         }
     }
     return $data;
 }
 /**
  * Render the feed.
  *
  * @param   string  $name     The name of the element to render
  * @param   array   $params   Array of values
  * @param   string  $content  Override the output of the renderer
  *
  * @return  string  The output of the script
  *
  * @see JDocumentRenderer::render()
  * @since   11.1
  */
 public function render($name = '', $params = null, $content = null)
 {
     $app = Factory::getApplication();
     // Gets and sets timezone offset from site configuration
     $tz = new DateTimeZone($app->getCfg('offset'));
     $now = Factory::getDate();
     $now->setTimeZone($tz);
     $data = $this->_doc;
     $uri = Uri::getInstance();
     $url = $uri->toString(array('scheme', 'user', 'pass', 'host', 'port'));
     $syndicationURL = Route::_('&format=feed&type=rss');
     if ($app->getCfg('sitename_pagetitles', 0) == 1) {
         $title = Text::sprintf('JPAGETITLE', $app->getCfg('sitename'), $data->title);
     } elseif ($app->getCfg('sitename_pagetitles', 0) == 2) {
         $title = Text::sprintf('JPAGETITLE', $data->title, $app->getCfg('sitename'));
     } else {
         $title = $data->title;
     }
     $feed_title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8');
     $feed = "<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n";
     $feed .= "\t<channel>\n";
     $feed .= "\t\t<title>" . $feed_title . "</title>\n";
     $feed .= "\t\t<description><![CDATA[" . $data->description . "]]></description>\n";
     $feed .= "\t\t<link>" . str_replace(' ', '%20', $url . $data->link) . "</link>\n";
     $feed .= "\t\t<lastBuildDate>" . htmlspecialchars($now->toRFC822(true), ENT_COMPAT, 'UTF-8') . "</lastBuildDate>\n";
     $feed .= "\t\t<generator>" . $data->getGenerator() . "</generator>\n";
     $feed .= '		<atom:link rel="self" type="application/rss+xml" href="' . str_replace(' ', '%20', $url . $syndicationURL) . "\"/>\n";
     if ($data->image != null) {
         $feed .= "\t\t<image>\n";
         $feed .= "\t\t\t<url>" . $data->image->url . "</url>\n";
         $feed .= "\t\t\t<title>" . htmlspecialchars($data->image->title, ENT_COMPAT, 'UTF-8') . "</title>\n";
         $feed .= "\t\t\t<link>" . str_replace(' ', '%20', $data->image->link) . "</link>\n";
         if ($data->image->width != "") {
             $feed .= "\t\t\t<width>" . $data->image->width . "</width>\n";
         }
         if ($data->image->height != "") {
             $feed .= "\t\t\t<height>" . $data->image->height . "</height>\n";
         }
         if ($data->image->description != "") {
             $feed .= "\t\t\t<description><![CDATA[" . $data->image->description . "]]></description>\n";
         }
         $feed .= "\t\t</image>\n";
     }
     if ($data->language != "") {
         $feed .= "\t\t<language>" . $data->language . "</language>\n";
     }
     if ($data->copyright != "") {
         $feed .= "\t\t<copyright>" . htmlspecialchars($data->copyright, ENT_COMPAT, 'UTF-8') . "</copyright>\n";
     }
     if ($data->editorEmail != "") {
         $feed .= "\t\t<managingEditor>" . htmlspecialchars($data->editorEmail, ENT_COMPAT, 'UTF-8') . ' (' . htmlspecialchars($data->editor, ENT_COMPAT, 'UTF-8') . ")</managingEditor>\n";
     }
     if ($data->webmaster != "") {
         $feed .= "\t\t<webMaster>" . htmlspecialchars($data->webmaster, ENT_COMPAT, 'UTF-8') . "</webMaster>\n";
     }
     if ($data->pubDate != "") {
         $pubDate = JFactory::getDate($data->pubDate);
         $pubDate->setTimeZone($tz);
         $feed .= "\t\t<pubDate>" . htmlspecialchars($pubDate->toRFC822(true), ENT_COMPAT, 'UTF-8') . "</pubDate>\n";
     }
     if (empty($data->category) === false) {
         if (is_array($data->category)) {
             foreach ($data->category as $cat) {
                 $feed .= "\t\t<category>" . htmlspecialchars($cat, ENT_COMPAT, 'UTF-8') . "</category>\n";
             }
         } else {
             $feed .= "\t\t<category>" . htmlspecialchars($data->category, ENT_COMPAT, 'UTF-8') . "</category>\n";
         }
     }
     if ($data->docs != "") {
         $feed .= "\t\t<docs>" . htmlspecialchars($data->docs, ENT_COMPAT, 'UTF-8') . "</docs>\n";
     }
     if ($data->ttl != "") {
         $feed .= "\t\t<ttl>" . htmlspecialchars($data->ttl, ENT_COMPAT, 'UTF-8') . "</ttl>\n";
     }
     if ($data->rating != "") {
         $feed .= "\t\t<rating>" . htmlspecialchars($data->rating, ENT_COMPAT, 'UTF-8') . "</rating>\n";
     }
     if ($data->skipHours != "") {
         $feed .= "\t\t<skipHours>" . htmlspecialchars($data->skipHours, ENT_COMPAT, 'UTF-8') . "</skipHours>\n";
     }
     if ($data->skipDays != "") {
         $feed .= "\t\t<skipDays>" . htmlspecialchars($data->skipDays, ENT_COMPAT, 'UTF-8') . "</skipDays>\n";
     }
     for ($i = 0, $count = count($data->items); $i < $count; $i++) {
         if (strpos($data->items[$i]->link, 'http://') === false && strpos($data->items[$i]->link, 'https://') === false) {
             $data->items[$i]->link = str_replace(' ', '%20', $url . $data->items[$i]->link);
         }
         $feed .= "\t\t<item>\n";
         $feed .= "\t\t\t<title>" . htmlspecialchars(strip_tags($data->items[$i]->title), ENT_COMPAT, 'UTF-8') . "</title>\n";
         $feed .= "\t\t\t<link>" . str_replace(' ', '%20', $data->items[$i]->link) . "</link>\n";
         if (empty($data->items[$i]->guid) === true) {
             $feed .= "\t\t\t<guid isPermaLink=\"true\">" . str_replace(' ', '%20', $data->items[$i]->link) . "</guid>\n";
         } else {
             $feed .= "\t\t\t<guid isPermaLink=\"false\">" . htmlspecialchars($data->items[$i]->guid, ENT_COMPAT, 'UTF-8') . "</guid>\n";
         }
         $feed .= "\t\t\t<description><![CDATA[" . $this->_relToAbs($data->items[$i]->description) . "]]></description>\n";
         if ($data->items[$i]->authorEmail != "") {
             $feed .= "\t\t\t<author>" . htmlspecialchars($data->items[$i]->authorEmail . ' (' . $data->items[$i]->author . ')', ENT_COMPAT, 'UTF-8') . "</author>\n";
         }
         /*
          * @todo: On hold
          * if ($data->items[$i]->source!="") {
          *   $data.= "			<source>".htmlspecialchars($data->items[$i]->source, ENT_COMPAT, 'UTF-8')."</source>\n";
          * }
          */
         if (empty($data->items[$i]->category) === false) {
             if (is_array($data->items[$i]->category)) {
                 foreach ($data->items[$i]->category as $cat) {
                     $feed .= "\t\t\t<category>" . htmlspecialchars($cat, ENT_COMPAT, 'UTF-8') . "</category>\n";
                 }
             } else {
                 $feed .= "\t\t\t<category>" . htmlspecialchars($data->items[$i]->category, ENT_COMPAT, 'UTF-8') . "</category>\n";
             }
         }
         if ($data->items[$i]->comments != "") {
             $feed .= "\t\t\t<comments>" . htmlspecialchars($data->items[$i]->comments, ENT_COMPAT, 'UTF-8') . "</comments>\n";
         }
         if ($data->items[$i]->date != "") {
             $itemDate = Factory::getDate($data->items[$i]->date);
             $itemDate->setTimeZone($tz);
             $feed .= "\t\t\t<pubDate>" . htmlspecialchars($itemDate->toRFC822(true), ENT_COMPAT, 'UTF-8') . "</pubDate>\n";
         }
         if ($data->items[$i]->enclosure != null) {
             $feed .= "\t\t\t<enclosure url=\"";
             $feed .= $data->items[$i]->enclosure->url;
             $feed .= "\" length=\"";
             $feed .= $data->items[$i]->enclosure->length;
             $feed .= "\" type=\"";
             $feed .= $data->items[$i]->enclosure->type;
             $feed .= "\"/>\n";
         }
         $feed .= "\t\t</item>\n";
     }
     $feed .= "\t</channel>\n";
     $feed .= "</rss>\n";
     return $feed;
 }