/** * Set the value of a checkbos DOMNode * @param DOMNode $checkbox * @param boolean $value */ public function set_value_checkbox(\DOMNode $checkbox, $value) { if ($value) { $checkbox->setAttribute('checked', 'checked'); } else { $checkbox->removeAttribute('checked'); } }
/** * Determine if node contains HTML signature image data. * * @param DOMNode $node The node to check. * @param boolean $strip Strip attribute from the node? * * @return boolean True if node contains image data. */ public static function isSigImage(DOMNode $node, $strip = false) { if (strcasecmp($node->tagName, 'IMG') === 0 && $node->hasAttribute(self::HTMLSIG_ATTR)) { if ($strip) { $node->removeAttribute(self::HTMLSIG_ATTR); } return true; } return false; }
/** * Vraci attributy * @param string $name * @throws LBoxExceptionConfig */ public function __set($name = "", $value = "") { if (strlen($name) < 1) { throw new LBoxExceptionConfig(LBoxExceptionConfig::MSG_PARAM_STRING_NOTNULL, LBoxExceptionConfig::CODE_BAD_PARAM); } if ($value === NULL) { $this->node->removeAttribute($name); } else { if (!$this->node->setAttribute($name, $value)) { throw new LBoxExceptionConfig(LBoxExceptionConfig::MSG_ATTRIBUTE_CANNOT_CHANGE, LBoxExceptionConfig::CODE_ATTRIBUTE_CANNOT_CHANGE); } } }
/** * updates the STRUCTURE node * * @access private * @param domnode $node * @return void */ private function structureNodeUpdate(DOMNode &$node) { /* * Add/remove attributes */ $node->removeAttribute('visible'); $node->setAttribute('group', 'main'); /* * Appends icon element */ $element = $this->_doc->createElement('icon'); $structureId = $node->getAttribute('id'); if (!empty($structureId)) { $element->setAttribute('id', 'icon-' . strtolower($structureId)); } $node->appendChild($element); }
protected static function attrClass($action, DOMNode $node, $class) { $classes = $node->getAttribute('class'); $found = stripos($classes, $class) !== false && in_array(strtolower($class), explode(' ', strtolower($classes))); if ($action == 'has') { return $found; } if ($action == 'toggle') { $action = $found ? 'remove' : 'add'; } if ($action == 'add' && !$found) { $node->setAttribute('class', trim(preg_replace('/\\s{2,}/i', ' ', $classes . ' ' . $class))); } if ($action == 'remove' && $found) { $classes = trim(preg_replace('/\\s{2,}/i', ' ', preg_replace('/(^|\\s)' . preg_quote($class, '/') . '(?:\\s|$)/i', ' ', $classes))); if ($classes !== '') { $node->setAttribute('class', $classes); } else { $node->removeAttribute('class'); } } return $node; }
/** * Adds a report selector * @param DOMNode $node The node we are adding the selector on. * @param array $options. Array ofoptions. should include keys: 'printf', 'printfargs', 'reportiew' or they should be set as attributes on the node. 'reportform' the form in the report view we want to select (Defaults to primary_form) * other optional keys are 'updateval' and 'updatedisp' which are names of elements to update the name and id of. If not set, then it is 'updateval'=$id:value and 'updatedisp=$id:display * and 'value' which contains the current db value * and 'display' which contains the current display value */ public function addReportSelector($obj, $node, $options = array()) { if ($obj instanceof I2CE_Page) { $template = $obj->getTemplate(); } else { $template = $obj; } if (!$template instanceof I2CE_Template) { return false; } if (!$node instanceof DOMElement) { return false; } if (!$node->hasAttribute('id') || !($id = $node->getAttribute('id'))) { return false; } if (!is_array($options)) { return false; } if ($node->hasAttribute('reportview')) { $options['reportview'] = $node->getAttribute('reportview'); $node->removeAttribute('reportview'); } if ($node->hasAttribute('reportform')) { $options['reportform'] = $node->getAttribute('reportform'); $node->removeAttribute('reportform'); } if ($node->hasAttribute('printf')) { $options['printf'] = $node->getAttribute('printf'); $node->removeAttribute('printf'); } if ($node->hasAttribute('printfargs')) { $options['printfargs'] = explode(',', $node->getAttribute('printfargs')); $node->removeAttribute('printfargs'); } if ($node->hasAttribute('contentid')) { $options['contentid'] = $node->getAttribute('contentid'); $node->removeAttribute('contentid'); } if (!array_key_exists('reportview', $options) || !($reportview = $options['reportview']) || !I2CE::getConfig()->is_parent("/modules/CustomReports/reportViews/{$reportview}") || !array_key_exists('printf', $options) || !$options['printf'] || !array_key_exists('printfargs', $options) || !is_array($options['printfargs']) || count($options['printfargs']) == 0) { return false; } if (!array_key_exists('value', $options)) { $options['value'] = ''; } if (!array_key_exists('display', $options)) { $options['display'] = ''; } if (!is_string($options['display']) || strlen(trim($options['display'])) == 0) { I2CE::getConfig()->setIfIsSet($display['config'], "/modules/CustomReports/displays/Selector/display_options/select_value"); if (!is_string($options['display']) || strlen(trim($options['display'])) == 0) { $options['display'] = 'Select Value'; } } if (!array_key_exists('value', $options)) { $options['value'] = ''; } if (!($mainNode = $template->loadFile("reportselector.html", "div")) instanceof DOMElement) { return false; } if (!($windowNode = $template->getElementByName("window", 0, $mainNode)) instanceof DOMNode) { return false; } if (!($dispNode = $template->getElementByName("display", 0, $mainNode)) instanceof DOMNode) { return false; } if (!($valNode = $template->getElementByName("value", 0, $mainNode)) instanceof DOMNode) { return false; } if (!($selectNode = $template->getElementByName("selector", 0, $mainNode)) instanceof DOMNode) { return false; } $contentNode = $template->appendFileByName("reportselector_content.html", "div", "content", 0, $mainNode); if (!$contentNode instanceof DOMElement) { return false; } $node->appendChild($mainNode); //we are good to go $windowNode->setAttribute('id', $id . ':window'); $dispNode->setAttribute('id', $id . ':display'); $template->addClass($dispNode, $id . '_toggle'); $template->addTextNode($dispNode, $options['display']); $valNode->setAttribute('id', $id . ':value'); $valNode->setAttribute('value', $options['value']); $valNode->setAttribute('name', $id); $jsSelect = "if (reportselectors && reportselectors['" . addslashes($id) . "']) {reportselectors['" . addslashes($id) . "'].show(); return false;} else { return false;}"; $selectNode->setAttribute('onClick', $jsSelect); if (!array_key_exists('allow_clear', $options)) { $options['allow_clear'] = true; } if (($clearNode = $template->getElementByName("clear_value_button", 0, $mainNode)) instanceof DOMNode) { if ($options['allow_clear']) { $jsClear = "if (reportselectors && reportselectors['" . addslashes($id) . "']) {reportselectors['" . addslashes($id) . "'].clear(); return false;} else { return false;}"; $clearNode->setAttribute('onClick', $jsClear); } else { $template->removeNode($clearNode); } } $contentNode->setAttribute('id', $id . ':content'); $this->selectors[$id] = $options; return true; }
private function parseNodeAttributes(DOMNode &$node) { if ($node->hasAttributes()) { foreach ($node->attributes as $attr) { if (strpos($attr->value, '${') !== false) { $expressions = array(); preg_match_all('/(\\$\\{)(.*)(\\})/imsxU', $attr->value, $expressions); for ($i = 0; $i < count($expressions[0]); $i++) { $toReplace = $expressions[0][$i]; $expression = $expressions[2][$i]; $expressionResult = ExpressionParser::evaluate($expression, $this->dataContext); switch (strtolower($attr->name)) { case 'repeat': // Can only loop if the result of the expression was an array if (!is_array($expressionResult)) { throw new ExpressionException("Can't repeat on a singular var"); } // Make sure the repeat variable doesn't show up in the cloned nodes (otherwise it would infinit recurse on this->parseNode()) $node->removeAttribute('repeat'); // Is a named var requested? $variableName = $node->getAttribute('var') ? trim($node->getAttribute('var')) : false; // Store the current 'Cur', index and count state, we might be in a nested repeat loop $previousCount = isset($this->dataContext['Context']['Count']) ? $this->dataContext['Context']['Count'] : null; $previousIndex = isset($this->dataContext['Context']['Index']) ? $this->dataContext['Context']['Index'] : null; $previousCur = $this->dataContext['Cur']; // For information on the loop context, see http://opensocial-resources.googlecode.com/svn/spec/0.9/OpenSocial-Templating.xml#rfc.section.10.1 $this->dataContext['Context']['Count'] = count($expressionResult); foreach ($expressionResult as $index => $entry) { if ($variableName) { // this is cheating a little since we're not putting it on the top level scope, the variable resolver will check 'Cur' first though so myVar.Something will still resolve correctly $this->dataContext['Cur'][$variableName] = $entry; } $this->dataContext['Cur'] = $entry; $this->dataContext['Context']['Index'] = $index; // Clone this node and it's children $newNode = $node->cloneNode(true); // Append the parsed & expanded node to the parent $newNode = $node->parentNode->insertBefore($newNode, $node); // And parse it (using the global + loop context) $this->parseNode($newNode, true); } // Restore our previous data context state $this->dataContext['Cur'] = $previousCur; if ($previousCount) { $this->dataContext['Context']['Count'] = $previousCount; } else { unset($this->dataContext['Context']['Count']); } if ($previousIndex) { $this->dataContext['Context']['Index'] = $previousIndex; } else { unset($this->dataContext['Context']['Index']); } return $node; break; case 'if': if (!$expressionResult) { return $node; } else { $node->removeAttribute('if'); } break; // These special cases that only apply for certain tag types // These special cases that only apply for certain tag types case 'selected': if ($node->tagName == 'option') { if ($expressionResult) { $node->setAttribute('selected', 'selected'); } else { $node->removeAttribute('selected'); } } else { throw new ExpressionException("Can only use selected on an option tag"); } break; case 'checked': if ($node->tagName == 'input') { if ($expressionResult) { $node->setAttribute('checked', 'checked'); } else { $node->removeAttribute('checked'); } } else { throw new ExpressionException("Can only use checked on an input tag"); } break; case 'disabled': $disabledTags = array('input', 'button', 'select', 'textarea'); if (in_array($node->tagName, $disabledTags)) { if ($expressionResult) { $node->setAttribute('disabled', 'disabled'); } else { $node->removeAttribute('disabled'); } } else { throw new ExpressionException("Can only use disabled on input, button, select and textarea tags"); } break; default: // On non os-template spec attributes, do a simple str_replace with the evaluated value $stringVal = htmlentities(ExpressionParser::stringValue($expressionResult), ENT_QUOTES, 'UTF-8'); $newAttrVal = str_replace($toReplace, $stringVal, $attr->value); $node->setAttribute($attr->name, $newAttrVal); break; } } } } } // if a repeat attribute was found, don't recurse on it's child nodes, the repeat handling already did that if (isset($node->childNodes) && $node->childNodes->length > 0) { $removeNodes = array(); // recursive loop to all this node's children foreach ($node->childNodes as $childNode) { if (($removeNode = $this->parseNode($childNode)) !== false) { $removeNodes[] = $removeNode; } } if (count($removeNodes)) { foreach ($removeNodes as $removeNode) { $removeNode->parentNode->removeChild($removeNode); } } } return false; }
/** * Process DOM node. * * @param DOMDocument $doc Document node. * @param DOMNode $node Node. */ protected function _node($doc, $node) { if (!$node instanceof DOMElement) { return; } switch (Horde_String::lower($node->tagName)) { case 'a': /* Strip whitespace from href links. This is bad HTML, but may * prevent viewing of the link. PHP DOM will already strip this * out for us, but if using tidy it will have URL encoded the * spaces. */ if ($node->hasAttribute('href')) { $node->setAttribute('href', preg_replace('/^(\\%20)+/', '', trim($node->getAttribute('href')))); } break; case 'base': /* Deal with <base> tags in the HTML, since they will screw up our * own relative paths. */ if ($this->_tmp['inline'] && $node->hasAttribute('href')) { $base = $node->getAttribute('href'); if (substr($base, -1) != '/') { $base .= '/'; } $this->_tmp['base'] = $base; $node->removeAttribute('href'); } break; } foreach ($node->attributes as $val) { /* Attempt to fix paths that were relying on a <base> tag. */ if (!is_null($this->_tmp['base']) && in_array($val->name, array('href', 'src'))) { $node->setAttribute($val->name, $this->_tmp['base'] . ltrim($val->value, '/')); } if ($val->name == 'href') { if ($this->_tmp['phish'] && $this->_phishingCheck($val->value, $node->textContent)) { $this->_phishWarn = true; foreach (array_merge(array($node), iterator_to_array($node->childNodes)) as $node2) { if ($node2 instanceof DOMElement) { $node2->removeAttribute('color'); $node2->removeAttribute('style'); } } $node->setAttribute('style', ($node->hasAttribute('style') ? rtrim($node->getAttribute('style'), '; ') . ';' : '') . $this->_phishCss); } if (isset($this->_params['external_callback'])) { /* Try to derefer all external references. */ $node->setAttribute('href', call_user_func($this->_params['external_callback'], $val->value)); } } } }
/** * Store style */ private function store() { $content = ''; foreach ($this->styles as $key => $value) { if ($key && $value) { $content .= trim($key) . ':' . $value . ';'; } } if ($this->node instanceof \DOMElement) { if ($content) { $this->node->setAttribute('style', $content); } else { $this->node->removeAttribute('style'); } } }
/** * Clean all atttributes but allowed * @param \DOMElement|\DOMNode $node * @param array $attributes */ private function cleanAttributes(&$node, $attributes = []) { if (($node instanceof \DOMElement or $node instanceof \DOMNode) and $node->attributes->length) { $delAttr = []; foreach ($node->attributes as $attr) { if (isset($attributes[$attr->name])) { $filter = $attributes[$attr->name]; $node->setAttribute($attr->name, $this->{$filter}($attr->value)); } elseif (isset($this->allowedAttrsForAll[$attr->name])) { $filter = $this->allowedAttrsForAll[$attr->name]; $node->setAttribute($attr->name, $this->{$filter}($attr->value)); } else { $delAttr[] = $attr->name; } } foreach ($delAttr as $da) { $node->removeAttribute($da); } } }
/** * Return the display value of this form field as a DOM Node. * @param DOMNode $node * @param I2CE_Template $template * @return DOMNode */ public function getDisplayNode($node, $template) { $text = $this->getDisplayValue(); $link = $this->getLink(); if (!$link || strlen($this->value) == 0) { //there is no linked URL to this field so we just display the text return $template->createTextNode($text); } $link_node = $template->createElement('a', array('href' => $link)); while ($node->hasChildNodes()) { $link_node->appendChild($node->firstChild); } if ($node->hasAttribute('show_text') && (strtolower($node->getAttribute('show_text')) == 'true' || strtolower($node->getAttribute('show_text')) == '!false')) { $link_node->appendChild($template->createTextNode($text)); } if (!$node->hasAttribute('show_image') || strtolower($node->getAttribute('show_image')) == 'true' || strtolower($node->getAttribute('show_image')) == '!false') { $width = null; $height = null; if ($node->hasAttribute('height')) { $height = $node->getAttribute('height'); $node->removeAttribute('height'); } if ($node->hasAttribute('width')) { $width = $node->getAttribute('width'); $node->removeAttribute('width'); } $attrs = array("src" => $link, 'alt' => $text, 'class' => 'field_image'); if (!empty($width)) { $attrs['width'] = $width; } if (!empty($height)) { $attrs['height'] = $height; } $link_node->appendChild($template->createElement('img', $attrs)); } return $link_node; }
/** * Insert content into an element - content encountered while looping within renderTemplate(). * * @param DOMNode $element An instance of DOMNode representing the element. * @param string $data_value Data to be inserted. * @param string $value_insert_fn A flag to either replace element's already existing data, to prepend or append it. * @param array $sub_runtime_props List of insertion settings compiled for this element. * * @see PHPFrontNodeList::populateNodeList() * * This is used internally * * @access private * * @return null|DOMNode Null on error. The originally provided DOMNode on success. */ private function elemSetContent($element, $data_value, $value_insert_fn = 'replace', $sub_runtime_props = array()) { if (empty($element)) { return; } # 2. $data_value is string; insert into matched elements // This is crucial! // This element may have already been parsed. Its content - the inserted data - may be also be parseable as well. // If this is so, a loop call to reparse the document may already have captured this parseable data in a nodeList. // Insert-replacing this element's parseable data will send the data out of the current document and make it unavailable. // To avoid this problem, all already-parsed elements have the 'data-phpfront-no_parse' attribute set. Detect this attribute and return! if ($this->on_reparse && $element->hasAttribute("data-phpfront-no_parse")) { $element->removeAttribute("data-phpfront-no_parse"); return; } if (!empty(trim($data_value))) { // How should we create the element's content? $content_type = $this->elemGetParam('content_type', $sub_runtime_props, $element); // If $data_value is plain text - Check if $data_value contains HTML tags if (!preg_match("/<[^<].*>/", $data_value) || strtoupper($content_type) == 'TEXT' || strtoupper($content_type) == 'CDATA') { if (strtoupper($content_type) == 'TEXT') { // Create TextNode and append $documentFragment = $this->PHPFrontDom->createTextNode($data_value); // Encodes entities - even though already encoded } else { // Create CDATA Section and append. CDATA is the default where content has not HTML tags $documentFragment = $this->PHPFrontDom->createCDATASection($data_value); } } else { $documentFragment = $this->PHPFrontDom->createDocumentFragment(); $documentFragment->appendXML($data_value); } // appendChild() throws error if $documentFragment is empty if (!empty($documentFragment)) { if ($value_insert_fn == "prepend" && $element->hasChildNodes()) { $element->insertBefore($documentFragment, $element->firstChild); } else { if ($value_insert_fn == "replace") { $element->nodeValue = ""; } $element->appendChild($documentFragment); } } } else { $on_content_empty = $this->elemGetParam('on_content_empty', $sub_runtime_props, $element); if ($on_content_empty == "clear" || $on_content_empty == "clear_and_set_flag") { $element->nodeValue = ""; } elseif ($on_content_empty == "set_flag" || $on_content_empty == "clear_and_set_flag") { $element->setAttribute("data-phpfront-content_empty", "true"); } elseif ($on_content_empty == "no_render") { $parentNode = $element->parentNode; $parentNode->removeChild($element); // This element hanle now changes to parent... This is what will be returned $element = $parentNode; } else { $element->nodeValue = $data_value; } } // This whole documen may be parsed again to parse inserted data // When this happens, the currently parsed elements should not be parsed again... so lets set the flag if ($this->parse_inserted_data && !$this->on_reparse && !isset($parentNode)) { $element->setAttribute("data-phpfront-no_reparse", "true"); } return $element; }