/** * Answer a element that represents the content for this Block * * @return object DOMElement * @access protected * @since 2/12/08 */ protected function getContentElement(DOMElement $mediaElement) { // Content/Description $descElement = $this->sourceXPath->query('./description', $this->sourceElement)->item(0); $descHtml = $this->getStringValue($descElement); $descHtml = $this->rewriteLocalLinks($descHtml); // Content $filename = $this->getStringValue($this->getSingleSourceElement('./filename', $this->sourceElement)); $currentContent = $this->doc->createElement('currentContent'); try { $fileUrlString = $this->attachFile($filename, $mediaElement); $fileUrlString = str_replace('asset_id', 'assetId', $fileUrlString); $fileUrlString = str_replace('record_id', 'recordId', $fileUrlString); // $fileUrlString = str_replace('&', '&', $fileUrlString); $doc = new Harmoni_DOMDocument(); $root = $doc->appendChild($doc->createElement('AudioPlayerPlugin')); $fileNode = $root->appendChild($doc->createElement('File')); $fileNode->setAttribute('show_download_link', 'true'); $idNode = $fileNode->appendChild($doc->createElement('Id', $fileUrlString)); $content = $currentContent->appendChild($this->createCDATAElement('content', $doc->saveXMLWithWhitespace())); } catch (MissingNodeException $e) { $content = $currentContent->appendChild($this->createCDATAElement('content', '')); } catch (Segue1To2_MissingFileException $e) { $content = $currentContent->appendChild($this->createCDATAElement('content', '')); } $rawDesc = $currentContent->appendChild($this->createCDATAElement('rawDescription', $this->cleanHtml($descHtml))); return $currentContent; }
/** * Answer a element that represents the content for this Block * * @return object DOMElement * @access protected * @since 2/12/08 */ protected function getContentElement(DOMElement $mediaElement) { // Content/Description $descElement = $this->sourceXPath->query('./description', $this->sourceElement)->item(0); if ($descElement) { $descHtml = $this->cleanHtml($this->getStringValue($descElement)); } else { $descHtml = ''; } $descHtml = $this->rewriteLocalLinks($descHtml); $title = $this->getDisplayName(); $url = $this->rewriteLocalLinks($this->getStringValue($this->getSingleSourceElement('./url', $this->sourceElement))); // Content $dataDoc = new Harmoni_DOMDocument(); $root = $dataDoc->appendChild($dataDoc->createElement('RssFeedPlugin')); $feed = $root->appendChild($dataDoc->createElement('RssFeed')); $urlElement = $feed->appendChild($dataDoc->createElement('Url', str_replace('&', '&', $url))); $sourceUrlElement = $this->getSingleSourceElement('./url', $this->sourceElement); $this->setAttributes($sourceUrlElement, $feed); $currentContent = $this->doc->createElement('currentContent'); $currentContent->appendChild($this->createCDATAElement('content', $dataDoc->saveXMLWithWhitespace())); $currentContent->appendChild($this->doc->createElement('rawDescription', '')); return $currentContent; }
/** * Load feed data, convert and clean it, and return its string value. * * @param string $url * @return string RSS xml * @access protected * @since 7/8/08 */ protected function loadFeedXml($url) { $feedData = @file_get_contents($url); if (!strlen($feedData)) { throw new OperationFailedException("Could not access feed, '" . $url . "'."); } $feed = new DOMDocument(); // If the encoding is not UTF-8, convert the document if (preg_match('/^<\\?xml .*encoding=[\'"]([a-zA-Z0-9-]+)[\'"].*\\?>/m', $feedData, $matches)) { $encoding = $matches[1]; if (strtoupper($encoding) != 'UTF8' && strtoupper($encoding) != 'UTF-8') { $feedData = mb_convert_encoding($feedData, 'UTF-8', strtoupper($encoding)); $feedData = preg_replace('/^(<\\?xml .*encoding=[\'"])([a-zA-Z0-9-]+)([\'"].*\\?>)/m', '\\1UTF-8\\3', $feedData); } } // Convert any non-UTF-8 characters $string = String::withValue($feedData); $string->makeUtf8(); $feedData = $string->asString(); if (!@$feed->loadXML($feedData)) { throw new OperationFailedException("Invalid feed data: \"" . $feedData . "\" for URL: " . $url); } // Handle any format conversions $feed = $this->convertToRss($feed); // Validate Feed. // $tmpFeed = $feed; // $feed = new Harmoni_DOMDocument; // $feed->loadXML($tmpFeed->saveXML()); // unset($tmpFeed); // $feed->schemaValidateWithException(dirname(__FILE__).'/rss-2_0-lax.xsd'); // Run through the titles, authors, and descriptions and clean out any unsafe HTML foreach ($feed->getElementsByTagName('title') as $element) { $element->nodeValue = strip_tags(htmlspecialchars_decode($element->nodeValue)); } foreach ($feed->getElementsByTagName('author') as $element) { $element->nodeValue = strip_tags(htmlspecialchars_decode($element->nodeValue)); } foreach ($feed->getElementsByTagName('comments') as $element) { $element->nodeValue = htmlentities(strip_tags(html_entity_decode($element->nodeValue))); } foreach ($feed->getElementsByTagName('link') as $element) { $element->nodeValue = htmlentities(strip_tags(html_entity_decode($element->nodeValue))); } foreach ($feed->getElementsByTagName('description') as $description) { $html = HtmlString::fromString(htmlspecialchars_decode($description->nodeValue)); $html->cleanXSS(); $description->nodeValue = htmlspecialchars($html->asString()); } // Move the feed into a dom document. $tmpFeed = $feed; $feed = new Harmoni_DOMDocument(); $feed->loadXML($tmpFeed->saveXML()); unset($tmpFeed); // Validate the feed again // $feed->schemaValidateWithException(dirname(__FILE__).'/rss-2_0-lax.xsd'); // Just ensure a few basic things: if (!$feed->documentElement->nodeName == 'rss') { throw new DOMDocumentException("Feed root must be an rss element"); } // Check for channels foreach ($feed->documentElement->childNodes as $element) { if ($element->nodeType == 1 && $element->nodeName != 'channel') { throw new DOMDocumentException("'" . $node->nodeName . "' is not expected, expecting 'channel'."); } } // Check dates foreach ($feed->getElementsByTagName('pubdate') as $element) { if (!preg_match('/(((Mon)|(Tue)|(Wed)|(Thu)|(Fri)|(Sat)|(Sun)), *)?\\d\\d? +((Jan)|(Feb)|(Mar)|(Apr)|(May)|(Jun)|(Jul)|(Aug)|(Sep)|(Oct)|(Nov)|(Dec)) +\\d\\d(\\d\\d)? +\\d\\d:\\d\\d(:\\d\\d)? +(([+\\-]?\\d\\d\\d\\d)|(UT)|(GMT)|(EST)|(EDT)|(CST)|(CDT)|(MST)|(MDT)|(PST)|(PDT)|\\w)/', $element->nodeValue)) { throw new DOMDocumentException("'" . $element->nodeValue . "' is not a valid date."); } } return $feed->saveXMLWithWhitespace(); }
/** * Write an option * * @param string $key * @param string $val * @return void * @access protected * @since 2/4/09 */ protected function writeOption($key, $val) { // The options will look like: /* <options> <targetNodeId>12345</targetNodeId> <defaultSortMethod>alpha</defaultSortMethod> <defaultDisplayType>cloud</defaultDisplayType> </options> */ if (!in_array($key, $this->_allowedOptions)) { throw new InvalidArgumentException("Unknown option, {$key}"); } $doc = new Harmoni_DOMDocument(); $doc->preserveWhiteSpace = false; try { $doc->loadXML($this->getContent()); } catch (DOMException $e) { $doc->appendChild($doc->createElement('options')); } if (!$doc->documentElement->nodeName == 'options') { throw new OperationFailedException('Expection root-node "options", found "' . $doc->documentElement->nodeName . '".'); } // Fetch the existing element or create a new one for this key $xpath = new DOMXPath($doc); $elements = $xpath->query('/options/' . $key); if ($elements->length) { $element = $elements->item(0); } else { $element = $doc->documentElement->appendChild($doc->createElement($key)); } // Set the value and save $element->nodeValue = $val; $this->setContent($doc->saveXMLWithWhitespace()); }