* @copyright (cc) creative commons - attribution-shareAlike 3.0 unported * @version 1.2 * @package qoob * @subpackage utils */ final class syndication { var $feed = ''; var $type = 0; var $link = ''; var $title = ''; var $posts = array(); var $author = ''; var $description = ''; var $descriptionHtml = true; /** * feed type setter function * * @param int $type * @example $this->syndication->setType(feed_types::ATOM); */ public function setType($type) { $this->type = $type; } /** * feed description setter function * args is an array of values. * * @public * @param array $args * @example $this->syndication->setDescrip(array( * 'link' => 'http://blog.xero.nu/', * 'title' => 'blog.xero.nu', * 'description' => 'a blog about code, art, hacks, technology,fonts video games, life and other random stuff.', * 'descriptionHtml' => false * )); */ public function setDescrip($args) { $this->link = isset($args['link']) ? $args['link'] : ''; $this->title = isset($args['title']) ? $args['title'] : ''; $this->author = isset($args['author']) ? $args['author'] : ''; $this->description = isset($args['description']) ? $args['description'] : ''; $this->descriptionHtml = isset($args['descriptionHtml']) ? $args['descriptionHtml'] : ''; } /** * feed data setter function * the data param is a multi-dimensional array of post data. * * @public * @param array $data * @example $blog = $this->model("blogModel"); * $result = $blog->getNewest(); * $posts = array(); * if(count($result) > 0) { * for($i = 0; $i < count($result); $i++) { * $posts[$i]['title'] = $result[$i]['title']; * $posts[$i]['link'] = 'http://blog.xero.nu/'.$result[$i]['url']; * $posts[$i]['description'] = $result[$i]['excerpt']; * $posts[$i]['descriptionHtml'] = true; * $posts[$i]['date'] = $result[$i]['date']; * $posts[$i]['author'] = 'xero harrison'; * } * } * $this->syndication->setData($posts); */ public function setData($data) { if(is_array($data)) { $count = count($data); if($count > 0) { for($i=0; $iposts[$i]['title'] = isset($data[$i]['title']) ? $data[$i]['title'] : ''; $this->posts[$i]['link'] = isset($data[$i]['link']) ? $data[$i]['link'] : ''; $this->posts[$i]['description'] = isset($data[$i]['description']) ? $data[$i]['description'] : ''; $this->posts[$i]['descriptionHtml'] = isset($data[$i]['descriptionHtml']) ? $data[$i]['descriptionHtml'] : ''; $this->posts[$i]['date'] = isset($data[$i]['date']) ? $data[$i]['date'] : ''; $this->posts[$i]['author'] = isset($data[$i]['author']) ? $data[$i]['author'] : ''; } } else { $this->posts[0]['title'] = 'no posts found'; $this->posts[0]['link'] = $link; $this->posts[0]['description'] = $description; $this->posts[0]['descriptionHtml'] = $descriptionHtml; $this->posts[0]['date'] = time(); $this->posts[0]['author'] = ''; } } else { throw new Exception("data must be in array format", statusCodes::HTTP_INTERNAL_SERVER_ERROR); } } /** * feed generate function * optionally set feed type, description, and post data * arrays in the same ways as their public method counterparts. * * @public * @param array $data * @example $descrip = array( * 'link' => 'http://blog.xero.nu/', * 'title' => 'blog.xero.nu', * 'description' => 'a blog about code, art, hacks, technology, video games, life and random stuff.', * 'descriptionHtml' => false * ); * $blog = $this->model("blogModel"); * $result = $blog->getNewest(); * $posts = array(); * if(count($result) > 0) { * for($i = 0; $i < count($result); $i++) { * $posts[$i]['title'] = $result[$i]['title']; * $posts[$i]['link'] = 'http://blog.xero.nu/'.$result[$i]['url']; * $posts[$i]['description'] = $result[$i]['excerpt']; * $posts[$i]['descriptionHtml'] = true; * $posts[$i]['date'] = $result[$i]['date']; * $posts[$i]['author'] = 'xero harrison'; * } * } * $this->library(qoob_types::utility, "syndication"); * $type = strtolower(library::catalog()->feedtype) == "atom" ? feed_types::ATOM : feed_types::RSS; * die ($this->syndication->generate($type, $descrip, $posts)); */ public function generate($type, $descrip, $data) { if(isset($type)) $this->setType($type); if(isset($descrip)) $this->setDescrip($descrip); if(isset($data)) $this->setData($data); switch($this->type) { case feed_types::ATOM: $this->makeATOM(); break; case feed_types::RSS: //--fall through default: $this->makeRSS(); break; } return $this->feed; } /** * make RSS feed function * internal function to mine variables * into an RSS 2.0 feed * * @private */ private function makeRSS() { //header('Content-Type: application/rss+xml; charset=ISO-8859-1'); header('Content-Type: text/xml; charset=ISO-8859-1'); //header $this->feed = ''.PHP_EOL; $this->feed.= ''.PHP_EOL; $this->feed.= ''.PHP_EOL; //channel $this->feed.= ' '.PHP_EOL; $this->feed.= ' '.$this->title.''.PHP_EOL; if($this->descriptionHtml){ $this->feed.= ' description.']]>'.PHP_EOL; } else { $this->feed.= ' '.$this->description.''.PHP_EOL; } $this->feed.= ' '.$this->link.''.PHP_EOL; $this->feed.= ' '.date("r", time()).''.PHP_EOL; $this->feed.= ' open[qoob]'.PHP_EOL; $this->feed.= ' '.PHP_EOL; //body for($i=0; $iposts); $i++) { $this->feed.= ' '.PHP_EOL; $this->feed.= ' '.$this->posts[$i]['title'].''.PHP_EOL; $this->feed.= ' '.$this->posts[$i]['link'].''.PHP_EOL; $this->feed.= ' '.$this->posts[$i]['link'].''.PHP_EOL; if($this->posts[$i]['descriptionHtml']){ $this->feed.= ' posts[$i]['description'].']]>'.PHP_EOL; } else { $this->feed.= ' '.$this->posts[$i]['description'].''.PHP_EOL; } $this->feed.= ' '.$this->posts[$i]['author'].''.PHP_EOL; //$this->feed.= ' technology'.PHP_EOL; $this->feed.= ' '.date("r", $this->posts[$i]['date']).''.PHP_EOL; $this->feed.= ' '.PHP_EOL; } //footer $this->feed.= ' '.PHP_EOL; $this->feed.= ''.PHP_EOL; } /** * make ATOM feed function * internal function to mine variables * into an ATOM 1.0 feed * * @private */ private function makeATOM() { //header('Content-type: application/atom+xml; charset=ISO-8859-1'); header('Content-Type: text/xml; charset=ISO-8859-1'); //header $this->feed = ''.PHP_EOL; $this->feed.= ''.PHP_EOL; //channel $this->feed.= ''.PHP_EOL; $this->feed.= ' '.$this->title.''.PHP_EOL; if($this->descriptionHtml){ $this->feed.= ' '.htmlspecialchars($this->description).''.PHP_EOL; } else { $this->feed.= ' '.htmlspecialchars($this->description).''.PHP_EOL; } $this->feed.= ' '.PHP_EOL; $this->feed.= ' '.date("c", time()).''.PHP_EOL; if($this->author != ''){ $this->feed.= ' '.PHP_EOL; $this->feed.= ' '.$this->author.''.PHP_EOL; $this->feed.= ' '.PHP_EOL; } $this->feed.= ' '.$this->link.''.PHP_EOL; $this->feed.= ' '.PHP_EOL; $this->feed.= ' open[qoob]'.PHP_EOL; //body for($i=0; $iposts); $i++) { $this->feed.= ' '.PHP_EOL; $this->feed.= ' '.$this->posts[$i]['title'].''.PHP_EOL; $this->feed.= ' '.$this->posts[$i]['link'].''.PHP_EOL; $this->feed.= ' '.PHP_EOL; if($this->posts[$i]['descriptionHtml']){ $this->feed.= ' '.htmlspecialchars($this->posts[$i]['description']).''.PHP_EOL; } else { $this->feed.= ' '.htmlspecialchars($this->posts[$i]['description']).''.PHP_EOL; } $this->feed.= ' '.PHP_EOL; $this->feed.= ' '.$this->posts[$i]['author'].''.PHP_EOL; $this->feed.= ' '.PHP_EOL; //$this->feed.= ' '.PHP_EOL; $this->feed.= ' '.date("c", $this->posts[$i]['date']).''.PHP_EOL; $this->feed.= ' '.PHP_EOL; } //footer $this->feed.= ''.PHP_EOL; } } /** * feed types * constants used for feed generation code hinting * * @author xero harrison * @copyright (c)opyright MMXII xero.nu * @version 1.0 * @package qoob * @subpackage utils */ final class feed_types { /** * @var RSS */ const RSS = 0; /** * @var ATOM */ const ATOM = 1; } ?>