예제 #1
0
 private function parserXml($oject, \SimpleXMLElement &$xml)
 {
     foreach ($oject as $k => $v) {
         if (is_array($v) || is_object($v)) {
             if (is_int($k)) {
                 $xml1 = $xml->addChild("_" . $k);
                 $this->parserXml($v, $xml1);
             } else {
                 if (is_numeric($k[0])) {
                     $xml1 = $xml->addChild("_" . $k);
                     $this->parserXml($v, $xml1);
                 } else {
                     $xml1 = $xml->addChild($k);
                     $this->parserXml($v, $xml1);
                 }
             }
         } else {
             if (is_int($k)) {
                 $xml->addChild("_" . $k . "", $v);
             } else {
                 if (is_numeric($k[0])) {
                     $xml->addChild("_" . $k, $v);
                 } else {
                     $xml->addChild($k, $v);
                 }
             }
         }
     }
 }
예제 #2
0
/**
 * Utility to Core-ify a given HTML string.
 *
 * Will use a tokenizer to scan for <img /> tags and <a /> tags.
 *
 * @param $html
 * @return string
 */
function parse_html($html){
	// Counter for the current position of the tokenizer.
	$x = 0;
	// Set to the position of the current image
	$imagestart = null;

	// @todo a rel=nofollow tags for external/untrusted links
	//       This can make use of an external utility to allow the admin to set which links are allowed.

	// @todo a tags that are absolutely resolved or have a core prefix such as core:///about-us or what not.

	while($x < strlen($html)){
		// Replace images with the optimized version.
		if(substr($html, $x, 4) == '<img'){
			$imagestart = $x;
			$x+= 3;
			continue;
		}

		$fullimagetag = null;

		if($imagestart !== null && $html{$x} == '>'){
			// This will equal the full image HTML tag, ie: <img src="blah"/>...
			$fullimagetag = substr($html, $imagestart, $x-$imagestart+1);
		}
		elseif($imagestart !== null && substr($html, $x, 2) == '/>'){
			// This will equal the full image HTML tag, ie: <img src="blah"/>...
			$fullimagetag = substr($html, $imagestart, $x-$imagestart+2);
		}

		if($imagestart !== null && $fullimagetag){
			// Convert it to a DOM element so I can process it.
			$simple = new \SimpleXMLElement($fullimagetag);
			$attributes = array();
			foreach($simple->attributes() as $k => $v){
				$attributes[$k] = (string)$v;
			}

			$file = \Core\Filestore\Factory::File($attributes['src']);

			// All images need alt tags.
			if(!isset($attributes['alt']) || $attributes['alt'] == ''){
				// Since this is usually only used to render content and the images contained therein,
				// and tinymce auto-adds a blank alt="" attribute,
				// I can safely add the alt based on the file's rendered title.
				$attributes['alt'] = $file->getTitle();
			}

			if(isset($attributes['width']) || isset($attributes['height'])){

				if(isset($attributes['width']) && isset($attributes['height'])){
					$dimension = $attributes['width'] . 'x' . $attributes['height'] . '!';
					unset($attributes['width'], $attributes['height']);
				}
				elseif(isset($attributes['width'])){
					$dimension = $attributes['width'];
					unset($attributes['width']);
				}
				else{
					$dimension = $attributes['height'];
					unset($attributes['height']);
				}

				$attributes['src'] = $file->getPreviewURL($dimension);
			}

			// And rebuild.
			$img = '<img';
			foreach($attributes as $k => $v){
				$img .= ' ' . $k . '="' . str_replace('"', '&quot;', $v) . '"';
			}
			$img .= '/>';

			$metahelper  = new \Core\Filestore\FileMetaHelper($file);
			$metacontent = $metahelper->getAsHTML();
			if($metacontent){
				$img = '<div class="image-metadata-wrapper">' . $img . $metacontent . '</div>';
			}

			// Figure out the offset for X.  I'll need to modify this after I merge it in.
			$x += strlen($img) - strlen($fullimagetag);
			// Split this string back in.
			$html = substr_replace($html, $img, $imagestart, strlen($fullimagetag));

			// Reset...
			$imagestart = null;
		}
		$x++;
	}
	//var_dump($html);

	return $html;
}