/** * Returns true if the passed {@link WizardComponent} validates against this rule. * @param ref object $component * @access public * @return boolean */ function checkValue($component) { $doc = new Harmoni_DOMDocument(); try { $doc->loadXML($component->getAllValues()); } catch (DOMException $e) { $component->setErrorText($e->getMessage()); return false; } try { $doc->schemaValidateWithException($this->xmlSchemaPath); } catch (ValidationFailedException $e) { $component->setErrorText($e->getMessage()); return false; } return true; }
/** * Answer an XML document for the options for this theme * * @return object Harmoni_DOMDocument * @access public * @since 5/15/08 */ public function getOptionsDocument() { try { $optionsString = $this->getThemeDataByType('options.xml'); $doc = new Harmoni_DOMDocument(); $doc->preserveWhiteSpace = false; if (strlen($optionsString)) { $doc->loadXML($optionsString); } return $doc; } catch (OperationFailedException $e) { return new Harmoni_DOMDocument(); } }
/** * Save the advanced step * * @param array $values * @return boolean * @access protected * @since 5/15/08 */ protected function saveAdvancedStep(array $values) { $component = $this->getSiteComponent(); $theme = $component->getTheme(); if (isset($values['create_copy']) && $values['create_copy']) { // Get the first source that supports admin. $guiMgr = Services::getService('GUIManager'); foreach ($guiMgr->getThemeSources() as $source) { if ($source->supportsThemeAdmin()) { $adminSession = $source->getThemeAdminSession(); $newTheme = $adminSession->createCopy($theme); $component->updateTheme($newTheme); $this->createdCopy = true; return true; } } // Nowhere to copy to. print "<p>" . _("Error: No available source to copy this theme to.") . "</p>"; return false; } /********************************************************* * Return if we are not editing advanced options *********************************************************/ if (!$theme->supportsModification()) { return true; } $modSess = $theme->getModificationSession(); if (!$modSess->canModify()) { return true; } /********************************************************* * Info *********************************************************/ $modSess->updateDisplayName($values['display_name']); $modSess->updateDescription($values['description']); if (!is_null($values['thumbnail']['tmp_name'])) { $file = new Harmoni_Filing_FileSystemFile($values['thumbnail']['tmp_name']); $file->setMimeType($values['thumbnail']['type']); $modSess->updateThumbnail($file); } /********************************************************* * CSS and HTML *********************************************************/ $modSess->updateGlobalCss($values['global_css']); foreach ($modSess->getComponentTypes() as $type) { $modSess->updateCssForType($type, $values[$type . '-css']); $modSess->updateTemplateForType($type, $values[$type . '-html']); } /********************************************************* * Images *********************************************************/ $missingImages = $theme->getImages(); foreach ($values['images'] as $imageVal) { // Add new images if ($imageVal['image']['tmp_name']) { $file = new Harmoni_Filing_FileSystemFile($imageVal['image']['tmp_name']); $file->setMimeType($imageVal['image']['type']); $theme->addImage($file, $imageVal['image']['name'], $imageVal['path_prefix']); } else { if ($imageVal['path_prefix'] != $imageVal['orig_path_prefix']) { $image = $modSess->getImage($imageVal['orig_path_prefix'] . '/' . $imageVal['image']['name']); $image->setPath($imageVal['path_prefix'] . '/' . $imageVal['image']['name']); } } // Mark images as existing if ($imageVal['path_prefix']) { $path = $imageVal['path_prefix'] . '/' . $imageVal['image']['name']; } else { $path = $imageVal['image']['name']; } foreach ($missingImages as $key => $image) { if ($image->getPath() == $path) { unset($missingImages[$key]); } } } // Remove any images that were removed. foreach ($missingImages as $image) { $image->delete(); } /********************************************************* * Options *********************************************************/ $optionsString = trim($values['options']); $optionsDoc = new Harmoni_DOMDocument(); $optionsDoc->preserveWhiteSpace = false; if (strlen($optionsString) && $optionsString != '<?xml version="1.0"?>') { try { $optionsDoc->loadXML($values['options']); } catch (DOMException $e) { print "<strong>" . _("Error in Options Definition:") . " </strong>"; print $e->getMessage(); return false; } } try { $modSess->updateOptionsDocument($optionsDoc); } catch (ValidationFailedException $e) { print "<strong>" . _("Error in Options Definition:") . " </strong>"; print $e->getMessage(); return false; } return true; }
/** * 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(); }
/** * Answer a DOM XML Document for the given topic * * @param string $topic * @return object DOMDocument * @access public * @since 12/9/05 */ function getTopicXmlDocument($topic) { $document = new Harmoni_DOMDocument(); $tocPart = $this->_tableOfContents->getTableOfContentsPart($topic); if (!$tocPart || !$tocPart->file || !file_exists($tocPart->file)) { ob_start(); print "<html>\n"; print "\t<head>\n"; print "\t\t<title>"; print dgettext("polyphony", "Topic Not Found"); print "</title>\n"; print "\t</head>\n"; print "\t<body>\n"; print "\t\t<h1>"; print dgettext("polyphony", "Topic Not Found"); print "</h1>\n"; print "\t\t<p>"; print dgettext("polyphony", "The topic that you requested was not found."); print "</p>\n"; print "\t</body>\n"; print "</html>\n"; $document->loadXML(ob_get_clean()); } else { $document->load($tocPart->file); } return $document; }
/** * Write an option * * @param string $groupId * @return void * @access protected * @since 2/4/09 */ protected function getOptions() { $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 . '".'); } return $doc; }
/** * 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()); }