<?php 
#
#  msa2agora - Matt's Script Archive to W-Agora
#  .0.02a  -  (buggy, crappy version)
#
#  By Ron Friedman (friedman@filmmaker.com)
#
#  DESCRIPTION:  Ok, the idea is to convert Matt's Script Archive to W-Agora.
#
#  Here's an example of what the messages were looking like for me:
#
#****************************
#
#   <!--begin-->
#   <b><font 
#   color=#101050>This is message #1 </font></b><br>
#   Tim Luzer &lt;<a href="mailto:hi@there.net">hi@there.net</a>&gt;<br>
#   Bombay, India - Thursday, January 28, 1999 at 06:49:10 (EST)<hr>
#
#   <b><font 
#   color=#101050>This is message #2</font></b><br>
#   Bob Dweeb &lt;<a href="mailto:Bob@smith.com">Bob@smith.com</a>&gt;<br>
#   USA - Tuesday, January 26, 1999 at 21:02:04 (EST)<hr>
#
#****************************
#
#  So the program's supposed to extract the notes, identify the content, then
#  insert it into a w-agora database.
#
#  Unfortunately, not all MSA's posts are the same.  Some require you to use your name,
#  your address, first name, last name..  Others people skip fields, etc.  So it's confusing,
#  but for me it worked...  You'll have to adjust the (very inefficient) parsing that I do
#  so it works for your messages.
#
#  CHANGE THESE:
#
#
#  This is the name of the forum database to insert your message into
#
$bn_db = "myforum";
#
#  This is the name of the source where Matt's Script Archive notes are.
#
$file = "test.html";
#
#  This is the e-mail address to use if your poster didn't provide one.
#
$noaddress= "NO_EMAIL_PROVIDED@myaddress.com";
#
#
#  This is set to 1 if you want to see the values this peice of crap program
#  is producing for each note.
#
$debug=1;


#   we'll need the insertnote() function

require "/w-agora/include/init.php";



Function do_one_note ($note) {

global $debug;

//strip out linefeeds
# $note = ereg_replace(10, "", $note);

//strip out message

  ereg("(101050>)(.*)(</font>)", $note, $result);
  $message = $result[2];
  $rest = ereg_replace("(101050>)(.*)(</font>)", "", $note);

//strip out linefeeds
  $rest = ereg_replace(10, "", $rest);

//strip out author, web, & email.
  
  ereg("(<br>)(.*)(<br>)", $rest, $result);
  $author= $result[2];

  ereg ("(.*) &lt;(.*)&gt;", $author, $result3);
  $author = $result3[1];
  $email = $result3[2];
  $email = ereg_replace("<a\ href=\"(.*)\">", "", $email);
  $email = ereg_replace("</a>", "", $email);

  ereg("<a\ href=\"(.*)\">", $author, $thing);
  $web = $thing[1];
  $author = ereg_replace("<a\ href=\"(.*)\">", "", $author);
  $author = ereg_replace("</a>", "", $author); 
  
  $rest = ereg_replace("(<br>)(.*)(<br>)", "", $rest);
 
 //strip out bullshit tags
  $rest = ereg_replace("<b><font", "", $rest);
  $rest = ereg_replace("color=#</b>", "", $rest);
  $rest = ereg_replace("<hr>", "", $rest);
 
 //strip out location:
  ereg("(.*) - (.*)", $rest, $result2);
  $location = $result2[1];
  $datefucked = $result2[2];
 
 //calculate date
  ereg ("(.*), (.*) (.*), (.*) at (.*):(.*):(.*) \(MST", $datefucked, $dt);
  if ($dt[2]=="January") {$mon = 1;}
  if ($dt[2]=="February") $mon = 2;
  if ($dt[2]=="March") $mon = 3;
  if ($dt[2]=="April") $mon = 4;
  if ($dt[2]=="May") $mon = 5;
  if ($dt[2]=="June") $mon = 6;
  if ($dt[2]=="July") $mon = 7;
  if ($dt[2]=="August") $mon = 8;
  if ($dt[2]=="September") $mon = 9;
  if ($dt[2]=="October") $mon = 10;
  if ($dt[2]=="November") $mon = 11;
  if ($dt[2]=="December") $mon = 12;

 
 $day = $dt[3];
 $yr = $dt[4];
 $hr = $dt[5];
 $min = $dt[6];
 $sec = $dt[7];
 
 $time = mktime($hr, $min, $sec, $mon, $day, $yr);
# echo Date("D, F d, Y  h:i:s A", $time);

#
# This part of the program kinda fills the blanks that aren't included by Matt's
# Script archive.  For example, MSA doesn't include a "subject" so I just made one
# out of the first thirty characters with a "..." at the end.  If I were serious about
# This program I'd check to see that there WERE 30 characters to use, and that they
# didn't include carriage returns,illegal characters, etc.  But of course I'm a lazy
# bastard who doesn't care...
#
# MSA doesn't force you to include an author's name either.  So when it comes up with
# no author, I use "Anonymous Coward"  (tip hat to slashdot ;) )   I also do a
# rather primitive check for a legal email address and if there isn't one I replace it.
# Same with subject, etc.  Sets the password to "" so that the admin password is used to
# make changes...
#

if (ereg("\@", $email)) {$useraddress=$email;} else {$useraddress=$noaddress;}
if ($message) {$subject = ereg_replace(10, "", (substr($message,0, 20) . "..."));}
if (!$author) {$author="Anonymous Coward";}
if (!$subject){$subject="No Subject";}

$username=$author;
$password="";
$body=$message;

# If debug is on, it'll show you the values it extracted.

if (($debug) && ($time != -1)) {echo "<hr>$message<hr>$username<hr>$web<hr>$useraddress<hr>$location<hr>$time<hr>";}

if (!$body) {return(""); exit;}

#  Set the $note array...

  $note[username]=$username;
  $note[subject]=$subject;
  $note[mail_reply]=$useraddress;
  $note[unixdate]=$time;
  $note[body]=$body;

#  These are non-standard but my script archive uses it.  If you want to use 'em,
#  modify yourdatabase to include these fields...

  $note[url]=$web;
  $note[location]=$location;

return $note;
}


#
#  This is the main body of the program.  It loads the .html file you specified
#  above, then seperates the messages by the <hr> (using the explode command) so that
#  each of the values in $messages should equal one note.  Then it calls the above
#  do_one_note() function which processes each one and gives you a lovely submit button
#  to press.  If it finds a message, it'll insert it into the forum.
#

# Load the file...

  $fn = fopen ($file,"r");
  $contents= stripslashes(fread ($fn, filesize($file)));
  fclose ($fn);

# seperate each note - each note is seperated by <hr> so we'll use that...

$messages = explode("<hr>", $contents);

# go through each one, and insert the note.  It does it in reverse order (most recent
# first) so that they'll be entered into w-agora's database oldest first (which is important)

$i=count($messages)+1;
echo "doing $i messages...<br>";
while ($i !=0) {
  $note=do_one_note($messages[$i]);
 if ($note[unixdate] > 0) { insertNote($note[unixdate], $note); }
 $i--;

}

echo "<br>Done";

?>
