Location: PHPKode > scripts > OpenOffice 2 HTML > openoffice-2-html/office.class.php
<?PHP
class office
	{
	public function oo_unzip($file, $save = false)
		{
		IF(!function_exists('zip_open'))
			{
			die('NO ZIP FUNCTIONS DETECTED. Do you have the PECL ZIP extensions loaded?');
			}
		IF($zip = zip_open($file))
			{
			while ($zip_entry = zip_read($zip))
				{
				$filename = zip_entry_name($zip_entry);
				IF(zip_entry_name($zip_entry) == 'content.xml' and zip_entry_open($zip, $zip_entry, "r"))
					{
					$content = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
					zip_entry_close($zip_entry);
					}
				IF(ereg('Pictures/', $filename) and !ereg('Object', $filename)  and zip_entry_open($zip, $zip_entry, "r"))
					{
					$img[$filename] = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
					zip_entry_close($zip_entry);
					}
				}
			IF(isset($content))
				{
				IF($save == false)
					{
					return array($content, $img);
					}
				else
					{
					file_put_contents('content.xml', $content);
					IF(is_array($img))
						{
						IF(!is_dir('Pictures'))
							{
							mkdir('Pictures');
							}
						foreach($img as $key => $val)
							{
							file_put_contents($key, $val);
							}
						}
					}
				}
			}
		}
	public function oo_convert($content, $ver)
		{
		IF(strip_tags($content) == $content and ereg('\.xml', $content))
			{
			$data = file_get_contents($content);
			}
		else
			{
			$data = $content;
			}
		IF($ver == 1)
			{
			IF(ereg('table:table', $data))
				{
				$data = str_replace('<table:table', '<text:p text:style-name="RKRK"><table:table', $data);
				$data = str_replace('</table:table>', '</table:table></text:p>', $data);
				$data = preg_replace('#<table:table(.*?)</table:table>#es', "base64_encode('\\1')", $data);
				}
			// get styles
			preg_match_all('#<office:automatic-styles>(.*?)</office:automatic-styles>#es', $data, $style);
			// get data
			preg_match_all('#<text:p text:style-name="([a-z A-Z_0-9]*)">(.*?)</text:p>#es', $data, $text);
			// get the XML parts
			$styles = $style[0];
			$texts1 = $text[1];
			$texts2 = $text[2];
			// take out the trash
			unset($data);
			unset($style);
			unset($text);
			// make xml strings
			$styles = implode('', $styles);
			$styles = strtr($styles, array(':' => '_', '-' => '_', '>' => ">\n"));
			$xml = simplexml_load_string($styles);
			$iter = 0;
			ob_start();
			foreach($xml->style_style as $style)
				{
				// MAKE a CSS definition
				IF($xml->style_style[$iter]['style_family'] == 'paragraph' or $xml->style_style[$iter]['style_family'] == 'text')
					{
					echo "\n.".$xml->style_style[$iter]['style_name'].' {
					font-family: '.$xml->style_style[$iter]->style_properties['style_font_name'].', verdana;
					font-size: '.$xml->style_style[$iter]->style_properties['fo_font_size'].';';
					IF(isset($xml->style_style[$iter]->style_properties['fo_color']))
						{
						echo "\ncolor: ".$xml->style_style[$iter]->style_properties['fo_color'].';';
						}
					IF(isset($xml->style_style[$iter]->style_properties['fo_text_align']))
						{
						echo "\ntext-align: ".$xml->style_style[$iter]->style_properties['fo_text_align'].';';
						}
					IF(isset($xml->style_style[$iter]->style_properties['fo_background_color']))
						{
						echo "\nbackground-color: ".$xml->style_style[$iter]->style_properties['fo_background_color'].';';
						}
					IF($xml->style_style[$iter]->style_properties['style_text_underline'] == 'single')
						{
						echo "\ntext-decoration: underline;";
						}
					echo "\nfont-weight: ".$xml->style_style[$iter]->style_properties['fo_font_weight'].';
					font-style: '.$xml->style_style[$iter]->style_properties['fo_font_style'].'
					} ';
					}
				elseIF(isset($xml->style_style[$iter]->style_properties['style_horizontal_pos']))
					{
					echo "\n.".$xml->style_style[$iter]['style_name'].' {';
					echo "\ntext-align: ".$xml->style_style[$iter]->style_properties['style_horizontal_pos'].';';
					echo '}';
					}
				$iter++;
				}
			$r_styles = ob_get_contents();
			ob_end_clean();
			// take out the trash
			unset($xml);
			unset($styles);
			unset($iter);
			// Show the document
			ob_start();
			foreach($texts1 as $key => $val)
				{
				//image
				IF(ereg('xlink:href="#Pictures/([a-z .A-Z_0-9]*)', $texts2[$key], $tab1) and ereg('draw:style-name="([a-zA-Z_0-9]*)"', $texts2[$key], $tab2))
					{
					echo '<div class="'.$tab2[1].'"><img src="Pictures/'.$tab1[1].'"></div>';
					}
				elseIF($val == 'RKRK')
					{
					$table = base64_decode($texts2[$key]);
					$table = stripslashes($table);
					$table = strtr($table, array('</table:table>' => '</table>', '<table:table-row>' => '<tr>', '</table:table-row>' => '</tr>', '</table:table-cell>' => '</td>', '</table:table-header-rows>' => '', '<table:table-header-rows>' => '', '>' => ">\n", '</text:p>'  => ''));
				
					preg_match_all('#table:name="([a-z A-Z_0-9]*)" table:style-name="([a-z A-Z_0-9]*)">#es', $table, $repl);
					foreach($repl[0] as $val)
						{
						$table = str_replace($val, '<table border="1"><tr><td>', $table);
						}
					preg_match_all('#<text:p text:style-name="([a-z A-Z_0-9]*)">#es', $table, $repl);
					foreach($repl[0] as $key => $val)
						{
						$table = str_replace($val, '', $table);
						}
					preg_match_all('#<table:table-column (.*?)">#es', $table, $repl);
					foreach($repl[0] as $val)
						{
						$table = str_replace($val, '', $table);
						}
					preg_match_all('#<table:table-cell table:style-name="([\.a-z A-Z_0-9]*)" table:value-type="([a-z A-Z_0-9]*)">#es', $table, $repl);
					foreach($repl[0] as $val)
						{
						$table = str_replace($val, '<td>', $table);
						}
					IF(ereg('xlink:href="Pictures/([a-z .A-Z_0-9]*)', $table, $tab1))
						{
						$table = str_replace('<draw:image xlink:href="Pictures/'.$tab1[1].'" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad"/>', '<img src="Pictures/'.$tab1[1].'">', $table);
						}
						
					echo '<BR>'.$table.'<BR>';
					}
				// Text
				else
					{
					echo '<div class="'.$val.'">'.$texts2[$key].'</div>';
					}
				}
			$r_text = ob_get_contents();
			ob_end_clean();
			unset($texts1);
			unset($texts2);
			unset($tab1);
			unset($tab2);
			}
		elseIF($ver == 2)
			{
			// we have tables, encode it here so all <text:p in them don't get preg_match_all few lines later
			IF(ereg('table:table', $data))
				{
				$data = str_replace('<table:table', '<text:p text:style-name="RKRK"><table:table', $data);
				$data = str_replace('</table:table>', '</table:table></text:p>', $data);
				$data = preg_replace('#<table:table(.*?)</table:table>#es', "base64_encode('\\1')", $data);
				}
			// get styles
			preg_match_all('#<office:automatic-styles>(.*?)</office:automatic-styles>#es', $data, $style);
			// get data
			preg_match_all('#<text:p text:style-name="([a-z A-Z_0-9]*)">(.*?)</text:p>#es', $data, $text);
			
			// get the XML parts
			$styles = $style[0];
			$texts1 = $text[1];
			$texts2 = $text[2];
			// take out the trash
			unset($data);
			unset($style);
			unset($text);
			// make xml strings
			$styles = implode('', $styles);
			$styles = strtr($styles, array(':' => '_', '-' => '_', '>' => ">\n"));
			$xml = simplexml_load_string($styles);
			$iter = 0;
			ob_start();
			foreach($xml->style_style as $style)
				{
				// MAKE a CSS definition
				IF( $xml->style_style[$iter]['style_family'] == 'paragraph' or  $xml->style_style[$iter]['style_family'] == 'text')
					{
					echo "\n.".$xml->style_style[$iter]['style_name'].' {
					font-family: '.$xml->style_style[$iter]->style_text_properties['style_font_name'].', verdana;';
					IF(isset($xml->style_style[$iter]->style_text_properties['fo_font_size']))
						{
						echo 'font-size: '.$xml->style_style[$iter]->style_text_properties['fo_font_size'].';';
						}
					IF(isset($xml->style_style[$iter]->style_text_properties['fo_color']))
						{
						echo "\ncolor: ".$xml->style_style[$iter]->style_text_properties['fo_color'].';';
						}
					IF(isset($xml->style_style[$iter]->style_paragraph_properties['fo_text_align']))
						{
						echo "\ntext-align: ".$xml->style_style[$iter]->style_paragraph_properties['fo_text_align'].';';
						}
					IF(isset($xml->style_style[$iter]->style_paragraph_properties['fo_background_color']))
						{
						echo "\nbackground-color: ".$xml->style_style[$iter]->style_paragraph_properties['fo_background_color'].';';
						}
					elseIF(isset($xml->style_style[$iter]->style_text_properties['fo_background_color']))
						{
						echo "\nbackground-color: ".$xml->style_style[$iter]->style_text_properties['fo_background_color'].';';
						}
					IF($xml->style_style[$iter]->style_text_properties['style_text_underline_style'] == 'solid')
						{
						echo "\ntext-decoration: underline;";
						}
					echo "\nfont-weight: ".$xml->style_style[$iter]->style_text_properties['fo_font_weight'].';';
					IF(isset($xml->style_style[$iter]->style_text_properties['fo_font_style']))
						{
						echo 'font-style: '.$xml->style_style[$iter]->style_text_properties['fo_font_style'].'';
						}
					echo '} ';
					}
				elseIF(isset($xml->style_style[$iter]->style_graphic_properties['style_horizontal_pos']))
					{
					echo "\n.".$xml->style_style[$iter]['style_name'].' {';
					echo "\ntext-align: ".$xml->style_style[$iter]->style_graphic_properties['style_horizontal_pos'].';';
					echo '}';
					}
				$iter++;
				}
			$r_styles = ob_get_contents();
			ob_end_clean();
			// take out the trash
			unset($xml);
			unset($styles);
			unset($iter);
			// Show the document
			ob_start();
			foreach($texts1 as $key => $val)
				{
				//image
				IF(ereg('xlink:href="Pictures/([a-z .A-Z_0-9]*)', $texts2[$key], $tab1) and ereg('draw:style-name="([a-zA-Z_0-9]*)"', $texts2[$key], $tab2))
					{
					echo '<div class="'.$tab2[1].'"><img src="Pictures/'.$tab1[1].'"></div>';
					}
				elseIF($val == 'RKRK')
					{
					$table = base64_decode($texts2[$key]);
					$table = stripslashes($table);
					$table = strtr($table, array('</table:table>' => '</table>', '<table:table-row>' => '<tr>', '</table:table-row>' => '</tr>', '</table:table-cell>' => '</td>', '</table:table-header-rows>' => '', '<table:table-header-rows>' => '', '>' => ">\n", '</text:p>'  => ''));
				
					preg_match_all('#table:name="([a-z A-Z_0-9]*)" table:style-name="([a-z A-Z_0-9]*)">#es', $table, $repl);
					foreach($repl[0] as $val)
						{
						$table = str_replace($val, '<table border="1"><tr><td>', $table);
						}
					preg_match_all('#<text:p text:style-name="([a-z A-Z_0-9]*)">#es', $table, $repl);
					foreach($repl[0] as $key => $val)
						{
						$table = str_replace($val, '', $table);
						}
					preg_match_all('#<table:table-column (.*?)">#es', $table, $repl);
					foreach($repl[0] as $val)
						{
						$table = str_replace($val, '', $table);
						}
					preg_match_all('#<table:table-cell table:style-name="([\.a-z A-Z_0-9]*)" office:value-type="([a-z A-Z_0-9]*)">#es', $table, $repl);
					foreach($repl[0] as $val)
						{
						$table = str_replace($val, '<td>', $table);
						}
					IF(ereg('xlink:href="Pictures/([a-z .A-Z_0-9]*)', $table, $tab1))
						{
						$table = str_replace('<draw:image xlink:href="Pictures/'.$tab1[1].'" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad"/>', '<img src="Pictures/'.$tab1[1].'">', $table);
						}
					echo '<BR>'.$table.'<BR>';
					}
				// Text
				else
					{
					echo '<div class="'.$val.'">'.$texts2[$key].'</div>';
					}
				}
			$r_text = ob_get_contents();
			ob_end_clean();
			unset($texts1);
			unset($texts2);
			unset($tab1);
			unset($tab2);
			}
		preg_match_all('#<text:span text:style-name="([A-Z 0-9]*)">#es', $r_text, $repl);
		foreach($repl[0] as $val)
			{
			$r_text = str_replace($val, '', $r_text);
			}
		$r_text  = str_replace('</text:span>', '', $r_text);
		IF(isset($r_styles) and isset($r_text))
			{
			return array($r_styles, $r_text);
			}
		else
			{
			return false;
			}
		}
	public function oo_to_file($filename, $content, $ver)
		{
		$x = office::oo_convert($content, $ver);
		$file = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><style>'.$x[0].'</style>'.$x[1].'</body></html>';
		IF(is_array($x))
			{
			$wskaz = fopen($filename, "w");
			fwrite($wskaz, $file);
			fclose($wskaz);
			return true;
			}
		else
			{
			return false;
			}
		}
	public function oo_on_screen($content, $ver)
		{
		$x = office::oo_convert($content, $ver);
		return '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><style>'.$x[0].'</style>'.$x[1].'</body></html>';
		}
	############################################
	############################################
	############################################
	public function abi_on_screen($filename, $patch = './')
		{
		$data = file_get_contents($filename);
		// tekst
		preg_match_all('#<section(.*?)>(.*?)</section>#es', $data, $text);
		$texts = $text[2];
		unset($text);
		// grafika
		preg_match_all('#<data>(.*?)</data>#es', $data, $extra);
		$extras = $extra[0];
		unset($extra);
		unset($data);
		
		$texts = implode('', $texts);
		IF(is_array($extras))
			{
			// jeżeli są grafiki /załączniki to pojedź dalej
			$extras = implode('', $extras);
			$xml = simplexml_load_string($extras);
			$iter = 0;
			IF(is_object($xml->d))
				{
				foreach($xml->d as $d)
				{
				IF(ereg('image/([a-z]*)', $xml->d['mime-type'], $tab))
					{
					$filename = md5($xml->d[$iter]['name']);
					$image_content = base64_decode($xml->d[$iter]);
					$images[$filename] = $patch.$filename.'.'.$tab[1];
					file_put_contents($patch.$filename.'.'.$tab[1], $image_content);
					$iter++;
					}
				}
				unset($iter);
				// przerabiamy wywołania do grafik na img src
				preg_match_all('#<image dataid="(.*?)" (.*?)"/>#es', $texts, $img);
				foreach($img[1] as $key => $val)
				{
				$val = md5($val);
				$texts = str_replace($img[0][$key], '<img src="'.$images[$val].'">', $texts);
				}
				unset($img);
				}
			}
		// tabelki
		preg_match_all('#<cell props="bot-attach:([0-9]*); left-attach:([0-9]*); right-attach:([0-9]*); top-attach:([0-9]*)">#es', $texts, $table);
		$tr = 0;
		foreach($table[0] as $key => $val)
			{
			IF($tr != $table[4][$key])
				{
				$texts = str_replace($val, '</tr><tr><td>', $texts);
				$tr = $table[4][$key];
				}
			else
				{
				$texts = str_replace($val, '<td>', $texts);
				}
			}
		unset($table);
		unset($tr);
		// wypunktowanie
		preg_match_all('#<c props="([a-zA-Z0-9 _;:\-]*) list-style:Bullet List; ([a-zA-Z0-9 _;:\-]*)">(.*?)</c>#es', $texts, $li);
		foreach($li[0] as $key => $val)
			{
			$texts = str_replace($val, '<LI>'.$li[3][$key].'</LI>', $texts);
			}
		
		preg_match_all('#<p level="(.*?) list-style:Numbered List; ([a-zA-Z0-9 _;:\-]*)">(.*?)</p>#es', $texts, $li2);
		foreach($li2[0] as $key => $val)
			{
			$texts = str_replace($val, '<LI>'.$li2[3][$key].'</LI>', $texts);
			}
		unset($li2);
		// czyszczenie, poprawki
		$texts= preg_replace('#<field (.*?)></field>#es', '', $texts);
		$texts = strtr($texts, array('<p style="Normal"></p>' => '', '<c props' => '<div style', '</c>' => '</div>', 'props="' => 'style="', '</cell>' => '</td>', '<table>' => '<table border="1" width="100%"><tr>', '</table>' => '</tr></table>', '</field>' => ''));
		return '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body>'.$texts;
		}
	}
?>
Return current item: OpenOffice 2 HTML