I have created a content stripper file that I am releasing to this newsgroup
as freeware.

The file takes a .html file that looks like this:

_________________
HEADER
_________________
CONTENT
_________________
FOOTER
_________________

and strips out the header and footer according to certain delimiters in the
.html file. Although it was made for a very specific purpose, it can be
modified to do a number of things. It recurses subdirectories, so it can be
used as a general find/replace file with an HTML interface. It is freeware
and can be freely modified. I'd appreciate it if you let me know where you
are using this file, however.

Please send any suggestions regarding this file to [EMAIL PROTECTED]
Thanks to all who helped me write the regular expressions -- I sincerely
appreciate your help.

------------------------------
-- erica douglass --
-- [EMAIL PROTECTED] --
------------------------------

<?php
#content_update.php
#erica douglass, 4/2001.
#suggestions are welcome... please mail them to [EMAIL PROTECTED]
if ($action == "strip") {
strip_files($directory);
find_subdirs($directory);
}
else {
print_html();
};

function print_html() {
?>
<html>
<head>
<title>Content Stripper</title>
<body>
<p><font face="Verdana, Arial, Helvetica, sans-serif">
Make sure all directories you are about to strip have write access for your
web server user (usually user "httpd".) To do this through the command line,
get root access and type the following:<br>
<br>chown -R httpd <i>directory</i></b>
<br>where <i>directory</i> is the directory where your HTML files are
located. The -R option means "recursive",
which changes all subdirectories of the directory you specify. Don't use -R
unless you include the full path
of the files, e.g. <i>chown -R /home/sites/home/web/test .</i>
<br><br>Enter the directory from which you would like to strip files
(example: /home/sites/home/web/test):<br>
</font></p>
<p><font face="Verdana, Arial, Helvetica, sans-serif" size="+1"><b>Security
Alert!</b> If you change your files
to be owned by your web server user, anyone with malicious intent can harm
them from the web. Therefore, it is
suggested that you immediately <b>chown -R root</b> (or your username)
<i><b>directory</b></i> from a command prompt after you run this script.
Although
the script itself cannot be hacked, we still strongly recommend making the
above change.</font></p>
<form action="<?php print $PHP_SELF; ?>" method="post">
<input type="text" size="30" name="directory">
<input type="hidden" name="action" value="strip">
<input type="submit" name="submit" value="Strip 'em!">
</form>
</body>
</html>
<?php
};

function strip_files($directory) {
$handle=opendir($directory); # open dir
print "<font face=\"Verdana, Arial, Helvetica, sans-serif\"
size=\"+1\">Reading directory <b>$directory</b>...<br>";
while (false!==($file = readdir($handle))) { # read all files in dir
 if (eregi(".*\.html$", $file)) { # as long as it ends with .html (case
insensitive)
  print "Now stripping header and footer from
<i>$directory/$file</i>...<br>";
  $file = $directory . "/" . $file; #make sure file has the proper directory
appended to it
  # get contents of a file into a string
  $fd = fopen ($file, "r");
  $contents = fread ($fd, filesize ($file));
  fclose ($fd);
  # replace header gif (any img tag with src=images/headers)
  $contents = preg_replace ("/<IMG.+SRC=\"images\/headers.*>/", "",
$contents);
  $contents = preg_replace ("/<IMG.+SRC=\"\/forte\/images\/headers.*>/", "",
$contents);
  # now, find occurrence of <!-- MAIN CONTENT --> and delete everything
before it.
  $contents = ereg_replace (".*<\!-- MAIN CONTENT -->", "", $contents);
  # replace all occurrences of <!-- ============ -->
  $contents = ereg_replace ("<\!-- ============ -->", "", $contents);
  # replace everything after <!-- END OF MAIN CONTENT -->
  $contents = ereg_replace ("<\!-- END OF MAIN CONTENT -->.*", "",
$contents);
  # rename old file to $file.old
  rename ("$file", "${file}.old");
  # write new file ($contents) to $file
  $fp = fopen (strtolower($file), "a+");
  fwrite ($fp, $contents);
  fclose ($fp);
  # print congratulations message
  print "<b>$file</b> has been stripped. Your original file is called
<i>${file}.old.</i><br><br>";

 };
}
print "</font>";
closedir($handle); # close file
}; # end function strip_files

function find_subdirs($directory) {
# load subdirectories into array
$handle=opendir($directory);
while ($file = readdir($handle))  { # read all files in dir
 if  ($file != ".." && $file != ".") {
  $file = $directory . "/" . $file; # ensure that is_dir picks the
directories up properly
  if (is_dir($file)) {
  $subdirs[count($subdirs)] = $file;
  };
 };
};

#clean up and sort
closedir($handle);
if (is_array($subdirs)) {
 sort($subdirs);
 while (list ($key, $val) = each ($subdirs)) {
  strip_files($val);
  find_subdirs($val);
 }
};
}; # end function find_subdirs



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to