"" ) {
$theFile = $_GET["file"];
}
if ( isset($_GET["prefix"]) && $_GET["prefix"] <> "" ) {
$thePrefix = $_GET["prefix"];
}
if ( isset($_GET["cssid"]) && $_GET["cssid"] <> "" ) {
$theCssID = "#" . $_GET["cssid"];
}
if ( isset($_GET["offset"]) && $_GET["offset"] <> "" ) {
$theFilePathOffSet = $_GET["offset"];
}
else {
$theFilePathOffSet = "";
}
$docRoot = $_SERVER['DOCUMENT_ROOT'];
$slash = "";
if (substr($docRoot, -1) <> "/") {
$slash = "/";
}
$baseFolder = "/textedit/"; // files foldername in root of server
$theFileArchive = $theFile . ".webarchive"; // This files has the HTML code
$theFileRtfd = $theFile . ".rtfd"; // This file has the images in the package
$theFileHTML = $theFile . ".html"; // If you just save the file as html
$theFile = str_replace(getcwd() . "/", "", $theFile);
$theFileArchive = str_replace(getcwd() . "/", "", $theFileArchive);
$theFileRtfd = str_replace(getcwd() . "/", "", $theFileRtfd);
$theFileHTML = str_replace(getcwd() . "/", "", $theFileHTML);
$thePathSeparator = "/"; // only for the images in the .rtfd file
$newBaseOffSet = $theFilePathOffSet;
if ($theFilePathOffSet == "") {
$theFilePathOffSet = ""; // usually when the files are in a folder.
} else {
$theFilePathOffSet = $docRoot . $theFilePathOffSet . "/";
}
$useBodyTags = 'false'; // false when included in a page.
// check if a html file exist, if so use that one
// if not, check if an archive with the same name exists
$theFileExists = false;
if ( file_exists( $theFilePathOffSet . $theFileHTML ) ) {
$theFileExists = true;
$theFileArchive = $theFileHTML;
} elseif ( file_exists( $theFilePathOffSet . $theFileArchive ) ) {
$theFileExists = true;
} else {
$theFileExists = false;
}
if ( $theFileExists ) {
$str = file_get_contents($theFilePathOffSet . $theFileArchive);
$ptn_html = "(.*<\/html>)";
$ptn_useHTML = false;
if ( isset($_GET["usehtml"]) && $_GET["usehtml"] == "true" ) {
$ptn_useHTML = true;
}
$ptn_style = "(.*<\/style>)";
$ptn_body = "((.*)<\/body>)";
$ptn_return = "/\n/";
$rpltxt = "";
// First remove all CR
$str_new = preg_replace($ptn_return, $rpltxt, $str);
preg_match_all($ptn_html, $str_new, $html);
$html = $html[0][0];
if ($ptn_useHTML) {
// Then extract the HTML
$body = $html;
$body = str_replace('css">', "css\">\n" , $body);
$body = str_replace("", "\n\n" , $body);
$body = str_replace("/title>", "/title>\n" , $body);
$body = str_replace("/>", "/>\n" , $body);
$body = str_replace("}", "}\n" , $body);
$body = str_replace("
", "\n" , $body);
$body = str_replace(" ", "\n \n" , $body);
$body = str_replace("", "\n\n\n" , $body);
// format table
$body = str_replace(" ", "\n \n" , $body);
$body = str_replace(" ", "\n " , $body);
$body = str_replace("> \n | ", "
\n " , $body);
$body = str_replace("
", "\n \n\n" , $body);
$body = str_replace("", "\n\n" , $body);
} else {
// Then extract the STYLE
preg_match_all($ptn_style, $html, $style);
$style = $style[0][0];
// To avoid style conflicts with multiple includes, give each style a uniquge #ID
// It's a mere guess that there are 4 space in the style elements
if ($theCssID <> "") {
$style = str_replace(" ", " " . $theCssID . " " , $style);
}
$style = str_replace('css">', "css\">\n" , $style);
$style = str_replace("}", "}\n" , $style);
echo $style . "\n\n";
// Then extract the BODY
preg_match_all($ptn_body, $html, $body);
$body = $body[0][0];
$body = str_replace("", "", $body);
$body = str_replace("", "", $body);
// format p and ul
$body = str_replace("", "\n" , $body);
$body = str_replace(" ", "\n \n" , $body);
// format table
$body = str_replace(" ", "\n \n" , $body);
$body = str_replace(" ", "\n " , $body);
$body = str_replace("> \n | ", "
\n " , $body);
$body = str_replace("
", "\n \n\n" , $body);
}
// Since image in a webarchive have file:// in img src
// We have to remove that. Alternatively you can add a folder.
// $body was first an array. Now it will be a textstring.
// BEGIN Vars calcualting path to images.
$newPath = substr($theFileRtfd,strrpos($theFileRtfd, $baseFolder),99). "/";
$newBasePath = substr($_SERVER["REQUEST_URI"] , 0, strripos($_SERVER["REQUEST_URI"], "/") +1 );
if ( $theFilePathOffSet == "") {
$newBase = substr($_SERVER["REQUEST_URI"] , 0, strripos($_SERVER["REQUEST_URI"], "/") + 1);
} else {
$newBase = $newBaseOffSet;
}
$newBasePath = $newPath;
// END Vars calcualting path to images.
$body = str_replace('src="file:///', 'src="' . $newPath , $body, $count);
$body = str_replace('href="file:///', 'href="' , $body, $count);
// Read dir and diplay content
if ( is_dir( $theFilePathOffSet . $theFileRtfd ) ) {
if ( $handle = opendir($theFilePathOffSet . $theFileRtfd) ) {
$idId = "te_id_";
if ($thePrefix <> "") {
$idId = $thePrefix;
}
$idCounter = 0;
/* This is the correct way to loop over the directory. */
while (false !== ($image = readdir($handle))) {
if ( !($image == "." ) && !($image == "..") && !($image == "TXT.rtf") && !($image == "@eaDir") ) {
$body = str_replace( 'src="' . $newPath . $image , 'id="' . $idId . $idCounter . '" ' . 'src="' . $newBasePath . $image , $body, $count);
$imagePath = $theFilePathOffSet . $theFileRtfd . $thePathSeparator . $image;
list($width, $height, $type, $attr) = getimagesize($imagePath);
$imgTitle = substr($image,0,strpos($image,"."));
// Use this line for responsive webpage
$body = str_replace( 'alt="' . $image . '"' , 'width="100%" ' . 'alt="' . $image . '"' . ' title="' . $imgTitle . '"' , $body, $count);
// Use this line for static webpages
// $body = str_replace( 'alt="' . $image . '"' , 'width="' . $width . '" height="' . $height . '" ' . 'alt="' . $image . '"', $body, $count);
// Set the style for max-width
$body = str_replace('alt="' . $image, 'style="max-width: ' . $width . 'px;" alt="' . $imgTitle, $body);
$idCounter = $idCounter + 1;
}
}
closedir($handle);
} // end open dir
} // end is_dir
// Display the body
echo $body;
} else {
echo "File not found
";
}
ob_end_flush();
?>