Exemplo n.º 1
0
 /**
  * Generate a feed (ATOM 1.0 or RSS 2.0 using Zend\Feed\Writer\Feed
  *
  * @param string $section Tiki feature the feed is related to
  * @param string $uniqueid
  * @param string $feed_version DEPRECATED
  * @param array $changes the content that will be used to generate the feed
  * @param string $itemurl base url for items (e.g. "tiki-view_blog_post.php?postId=%s")
  * @param string $urlparam
  * @param string $id name of the id field used to identify each feed item (e.g. "postId")
  * @param string $title title for the feed
  * @param string $titleId name of the key in the $changes array with the title of each item
  * @param string $desc description for the feed
  * @param string $descId name of the key in the $changes array with the description of each item
  * @param string $dateId name of the key in the $changes array with the date of each item
  * @param string $authorId name of the key in the $changes array with the author of each item
  * @param bool $fromcache if true recover the feed from cache
  *
  * @return array the generated feed
  */
 function generate_feed($section, $uniqueid, $feed_version, $changes, $itemurl, $urlparam, $id, $title, $titleId, $desc, $descId, $dateId, $authorId, $fromcache = false)
 {
     global $tiki_p_admin, $prefs;
     $userlib = TikiLib::lib('user');
     $tikilib = TikiLib::lib('tiki');
     $smarty = TikiLib::lib('smarty');
     // both title and description fields cannot be null
     if (empty($title) || empty($desc)) {
         $msg = tra('The fields title and description are mandatory to generate a feed.');
         if ($tiki_p_admin) {
             $msg .= ' ' . tra('To fix this error go to Admin -> Feeds.');
         } else {
             $msg .= ' ' . tra('Please contact the site administrator and ask him to fix this error');
         }
         $smarty->assign('msg', $msg);
         $smarty->display('error.tpl');
         die;
     }
     $feed_format = $this->get_current_feed_format();
     $feed_format_name = $this->get_current_feed_format_name();
     if ($prefs['feed_cache_time'] < 1) {
         $fromcache = false;
     }
     // only get cache data if rss cache is enabled
     if ($fromcache) {
         $output = $this->get_from_cache($uniqueid, $feed_format);
         if ($output['data'] != 'EMPTY') {
             return $output;
         }
     }
     $urlarray = parse_url($_SERVER["REQUEST_URI"]);
     $rawPath = str_replace('\\', '/', dirname($urlarray["path"]));
     $URLPrefix = $tikilib->httpPrefix() . $rawPath;
     if ($rawPath != "/") {
         $URLPrefix .= "/";
         // Append a slash unless Tiki is in the document root. dirname() removes a slash except in that case.
     }
     if (isset($prefs['feed_' . $section . '_index']) && $prefs['feed_' . $section . '_index'] != '') {
         $feedLink = $prefs['feed_' . $section . '_index'];
     } else {
         $feedLink = htmlspecialchars($tikilib->httpPrefix() . $_SERVER["REQUEST_URI"]);
     }
     $img = htmlspecialchars($URLPrefix . $prefs['feed_img']);
     $title = htmlspecialchars($title);
     $desc = htmlspecialchars($desc);
     $read = $URLPrefix . $itemurl;
     $feed = new Zend\Feed\Writer\Feed();
     $feed->setTitle($title);
     $feed->setDescription($desc);
     if (!empty($prefs['feed_language'])) {
         $feed->setLanguage($prefs['feed_language']);
     }
     $feed->setLink($tikilib->tikiUrl(''));
     $feed->setFeedLink($feedLink, $feed_format_name);
     $feed->setDateModified($tikilib->now);
     if ($feed_format_name == 'atom') {
         $author = array();
         if (!empty($prefs['feed_atom_author_name'])) {
             $author['name'] = $prefs['feed_atom_author_name'];
         }
         if (!empty($prefs['feed_atom_author_email'])) {
             $author['email'] = $prefs['feed_atom_author_email'];
         }
         if (!empty($prefs['feed_atom_author_url'])) {
             $author['url'] = $prefs['feed_atom_author_url'];
         }
         if (!empty($author)) {
             if (empty($author['name'])) {
                 $msg = tra('If you set feed author email or URL, you must set feed author name.');
                 $smarty->assign('msg', $msg);
                 $smarty->display('error.tpl');
                 die;
             }
             $feed->addAuthor($author);
         }
     } else {
         $authors = array();
         if (!empty($prefs['feed_rss_editor_email'])) {
             $authors['name'] = $prefs['feed_rss_editor_email'];
         }
         if (!empty($prefs['feed_rss_webmaster_email'])) {
             $authors['name'] = $prefs['feed_rss_webmaster_email'];
         }
         if (!empty($authors)) {
             $feed->addAuthors(array($authors));
         }
     }
     if (!empty($prefs['feed_img'])) {
         $image = array();
         $image['uri'] = $tikilib->tikiUrl($prefs['feed_img']);
         $image['title'] = tra('Feed logo');
         $image['link'] = $tikilib->tikiUrl('');
         $feed->setImage($image);
     }
     foreach ($changes["data"] as $data) {
         $item = $feed->createEntry();
         $item->setTitle($data[$titleId]);
         if (isset($data['sefurl'])) {
             $item->setLink($URLPrefix . $data['sefurl']);
         } elseif ($urlparam != '') {
             // 2 parameters to replace
             $item->setlink(sprintf($read, urlencode($data["{$id}"]), urlencode($data["{$urlparam}"])));
         } else {
             $item->setLink(sprintf($read, urlencode($data["{$id}"])));
         }
         if (isset($data[$descId]) && $data[$descId] != '') {
             $item->setDescription($data[$descId]);
         }
         $item->setDateCreated($data[$dateId]);
         $item->setDateModified($data[$dateId]);
         if ($authorId != '' && $prefs['feed_' . $section . '_showAuthor'] == 'y') {
             $author = $this->process_item_author($data[$authorId]);
             $item->addAuthor($author);
         }
         $feed->addEntry($item);
     }
     $data = $feed->export($feed_format_name);
     $this->put_to_cache($uniqueid, $feed_format, $data);
     $output = array();
     $output["data"] = $data;
     $output["content-type"] = 'application/xml';
     return $output;
 }