Пример #1
0
 /**
  * Set the link.
  * If the link is an internal link the sites URL will be prepended.
  *
  * @param string $link The link for the item.
  */
 public function setLink($link)
 {
     // redefine var
     $link = (string) $link;
     // if link doesn't start with http, we prepend the URL of the site
     if (mb_substr($link, 0, 7) != 'http://') {
         $link = SITE_URL . $link;
     }
     // call parent
     parent::setLink($link);
 }
Пример #2
0
 /**
  * Reads an feed into a SpoonRSS object.
  *
  * @return	SpoonRSS					Returns as an instance of SpoonRSS.
  * @param	string $URL					An URL where the feed is located or the XML of the feed.
  * @param	string[optional] $type		The type of feed, possible values are: url, string.
  */
 public static function readFromFeed($URL, $type = 'url')
 {
     // redefine var
     $URL = (string) $URL;
     $type = (string) SpoonFilter::getValue($type, array('url', 'string'), 'url');
     // validate
     if ($type == 'url' && !SpoonFilter::isURL($URL)) {
         throw new SpoonFeedException('This (' . SpoonFilter::htmlentities($URL) . ') isn\'t a valid URL.');
     }
     if (!self::isValid($URL, $type)) {
         throw new SpoonFeedException('Invalid feed');
     }
     // load xmlstring
     if ($type == 'url') {
         $xmlString = SpoonHTTP::getContent($URL);
     } else {
         $xmlString = $URL;
     }
     // convert to simpleXML
     $XML = @simplexml_load_string($xmlString);
     // validate the feed
     if ($XML === false) {
         throw new SpoonFeedException('Invalid rss-string.');
     }
     // get title, link and description
     $title = (string) $XML->channel->title;
     $link = (string) $XML->channel->link;
     $description = (string) $XML->channel->description;
     // create instance
     $RSS = new SpoonFeedRSS($title, $link, $description);
     // add items
     foreach ($XML->channel->item as $item) {
         // try to read
         try {
             // read xml
             $item = SpoonFeedRSSItem::readFromXML($item);
             $RSS->addItem($item);
         } catch (Exception $e) {
             // ignore exceptions
         }
     }
     // add category
     if (isset($XML->channel->category)) {
         foreach ($XML->channel->category as $category) {
             if (isset($category['domain'])) {
                 $RSS->addCategory((string) $category, (string) $category['domain']);
             } else {
                 $RSS->addCategory((string) $category);
             }
         }
     }
     // add skip day
     if (isset($XML->channel->skipDays)) {
         // loop ski-days
         foreach ($XML->channel->skipDays->day as $day) {
             // try to add
             try {
                 // add skip-day
                 $RSS->addSkipDay((string) $day);
             } catch (Exception $e) {
                 // ignore exceptions
             }
         }
     }
     // add skip hour
     if (isset($XML->channel->skipHours)) {
         foreach ($XML->channel->skipHours->hour as $hour) {
             // try to add
             try {
                 // add skip hour
                 $RSS->addSkipHour((int) $hour);
             } catch (Exception $e) {
                 // ignore exceptions
             }
         }
     }
     // set cloud
     if (isset($XML->channel->cloud['domain']) && isset($XML->channel->cloud['port']) && isset($XML->channel->cloud['path']) && isset($XML->channel->cloud['registerProce-dure']) && isset($XML->channel->cloud['protocol'])) {
         // read attributes
         $cloudDomain = (string) $XML->channel->cloud['domain'];
         $cloudPort = (int) $XML->channel->cloud['port'];
         $cloudPath = (string) $XML->channel->cloud['path'];
         $cloudRegisterProcedure = (string) $XML->channel->cloud['registerProce-dure'];
         $cloudProtocol = (string) $XML->channel->cloud['protocol'];
         // set property
         $RSS->setCloud($cloudDomain, $cloudPort, $cloudPath, $cloudRegisterProcedure, $cloudProtocol);
     }
     // set copyright
     if (isset($XML->channel->copyright)) {
         $copyright = (string) $XML->channel->copyright;
         $RSS->setCopyright($copyright);
     }
     // set docs
     if (isset($XML->channel->docs)) {
         $docs = (string) $XML->channel->docs;
         $RSS->setDocs($docs);
     }
     // set generator if it is present
     if (isset($XML->channel->generator)) {
         $generator = (string) $XML->channel->generator;
         $RSS->setGenerator($generator);
     }
     // set image if it is present
     if (isset($XML->channel->image->title) && isset($XML->channel->image->url) && isset($XML->channel->image->link)) {
         // read properties
         $imageTitle = (string) $XML->channel->image->title;
         $imageURL = (string) $XML->channel->image->url;
         $imageLink = (string) $XML->channel->image->link;
         // read optional properties
         if (isset($XML->channel->image->width)) {
             $imageWidth = (int) $XML->channel->image->width;
         } else {
             $imageWidth = null;
         }
         if (isset($XML->channel->image->height)) {
             $imageHeight = (int) $XML->channel->image->height;
         } else {
             $imageHeight = null;
         }
         if (isset($XML->channel->image->description)) {
             $imageDescription = (string) $XML->channel->image->description;
         } else {
             $imageDescription = null;
         }
         // try to set image
         try {
             // set image
             $RSS->setImage($imageURL, $imageTitle, $imageLink, $imageWidth, $imageHeight, $imageDescription);
         } catch (Exception $e) {
             // ignore exceptions
         }
     }
     // set language if its is present
     if (isset($XML->channel->language)) {
         $language = (string) $XML->channel->language;
         $RSS->setLanguage($language);
     }
     // set last build date if it is present
     if (isset($XML->channel->lastBuildDate)) {
         $lastBuildDate = (int) strtotime($XML->channel->lastBuildDate);
         $RSS->setLastBuildDate($lastBuildDate);
     }
     // set managing editor
     if (isset($XML->channel->managingEditor)) {
         $managingEditor = (string) $XML->channel->managingEditor;
         $RSS->setManagingEditor($managingEditor);
     }
     // set publication date
     if (isset($XML->channel->pubDate)) {
         $publicationDate = (int) strtotime($XML->channel->pubDate);
         $RSS->setPublicationDate($publicationDate);
     }
     // set rating
     if (isset($XML->channel->rating)) {
         $rating = (string) $XML->channel->rating;
         $RSS->setRating($rating);
     }
     // set ttl
     if (isset($XML->channel->ttl)) {
         $ttl = (int) $XML->channel->ttl;
         $RSS->setTTL($ttl);
     }
     // set webmaster
     if (isset($XML->channel->webmaster)) {
         $webmaster = (string) $XML->channel->webmaster;
         $RSS->setWebmaster($webmaster);
     }
     // return
     return $RSS;
 }
Пример #3
0
 /**
  * Read an item from a SimpleXMLElement.
  *
  * @return	SpoonRSSItem				An instance of SpoonRSS.
  * @param	SimpleXMLElement $item		The XML-element that represents a single item in the feed.
  */
 public static function readFromXML(SimpleXMLElement $item)
 {
     // get title, link and description
     $title = (string) $item->title;
     $link = (string) $item->link;
     $description = (string) $item->description;
     // create instance
     $rssItem = new SpoonFeedRSSItem($title, $link, $description);
     // add categories
     if (isset($item->category)) {
         foreach ($item->category as $category) {
             // set property
             $rssItem->addCategory((string) $category, $category['domain']);
         }
     }
     // set author
     if (isset($item->author)) {
         $rssItem->setAuthor((string) $item->author);
     }
     // set commentslink
     if (isset($item->comments)) {
         // try to set the commentslink
         try {
             // set commentslink
             $rssItem->setCommentsLink((string) $item->comments);
         } catch (Exception $e) {
             // ignore exceptions
         }
     }
     // set enclosure
     if (isset($item->enclosure['url']) && isset($item->enclosure['length']) && isset($item->enclosure['type'])) {
         // read data
         $URL = (string) $item->enclosure['url'];
         $length = (int) $item->enclosure['length'];
         $type = (string) $item->enclosure['type'];
         // try to set enclosure
         try {
             // set enclosure
             $rssItem->setEnclosure($URL, $length, $type);
         } catch (Exception $e) {
             // ignore exceptions
         }
     }
     // set guid
     if (isset($item->guid)) {
         // read data
         $URL = (string) $item->guid;
         $isPermaLink = (bool) $item->guid['isPermaLink'];
         // try to set GUID
         try {
             // set GUID
             $rssItem->setGuid($URL, $isPermaLink);
         } catch (Exception $e) {
             // ignore exceptions
         }
     }
     // set publication date
     if (isset($item->pubDate)) {
         $rssItem->setPublicationDate((int) strtotime($item->pubDate));
     }
     // set source
     if (isset($item->source)) {
         // read data
         $name = (string) $item->source;
         $URL = (string) $item->source['url'];
         // try to set source
         try {
             // set source
             $rssItem->setSource($name, $URL);
         } catch (Exception $e) {
             // ignore exceptions
         }
     }
     return $rssItem;
 }