<?php
  $phpgw_info["flags"]["currentapp"] = "cdb";
  $phpgw_info["server"]["site_title"] = "Import from Outlook";
  $phpgw_info["flags"]["nonavbar"] = True;
  $phpgw_info["flags"]["noheader"] = True;
  include("../../header.inc.php");

/*
  while (list($key, $val) = each($phpgw_info)) {
    if (is_array($val)) {
      while (list($key2, $val2) = each($val)) {
        if (is_array($val2)) {
          while (list($key3, $val3) = each ($val2)) {
            if (is_array($val3)) {
              while (list($key4, $val4) = each ($val3)) {
                echo "phpgw_info[$key][$key2][$key3][$key4]=$val4<br>";
              }
            } else {
              echo "phpgw_info[$key][$key2][$key3]=$val3<br>";
            }
          }
        } else {
          echo "phpgw_info[$key][$key2]=$val2<br>";
        }
      }
    } else {
      echo "phpgw_info[$key]=$val<br>";
    }
  }
*/

$sep = $phpgw_info["server"]["dir_separator"];

# Construct a default basedn and context for Contacts
$tmpbasedn = split(",",$phpgw_info["server"]["ldap_context"]);
array_shift($tmpbasedn);
for ($i=0;$i<count($tmpbasedn);$i++) {
  if($i==0) {
    $fakebasedn = $tmpbasedn[$i];
  } else {
    $fakebasedn = $fakebasedn.",".$tmpbasedn[$i];
  }
}
$fakecontext = "ou=Contacts,".$fakebasedn;

if (!$convert) {
  $phpgw->common->phpgw_header();
  echo parse_navbar();
  echo "\n\n<CENTER><TABLE WIDTH=90%><TR BGCOLOR=".$phpgw_info["theme"]["navbar_bg"]."><TD>";
  echo "<B><FONT SIZE=+2 COLOR=".$phpgw_info["theme"]["navbar_text"].">";
  echo "\n<CENTER>Import from Outlook</CENTER>\n";
  echo "</FONT></B></TD></TR>\n";
  echo "<TR><TD>\n";
?>

<TABLE WIDTH=85%><TR><TD>
<FORM ENCTYPE="multipart/form-data" action=<?php echo $phpgw->link(); ?> method=post>
<OL>
  <LI>In Outlook, select your Contacts folder, select <b>Import 
      and Export...</b> from the <b>File</b> 
      menu and export your contacts into a comma separated text file.<P></LI>
  <LI>Enter the path to the exported file here:
      <INPUT NAME="tsvfile" SIZE=48 TYPE="file" VALUE="<?php echo $tsvfile_name; ?>"><P></LI>
  <LI>Select the type of conversion (Import types will perform an actual import.  Debug will display output in browser or via a download.):<BR>
      <SELECT NAME="conv_type">
        <OPTION VALUE="none">&lt;none&gt;</OPTION>
        <?php
          $dir_handle=opendir($phpgw_info["server"]["app_root"].$sep."outlook2xyz".$sep."conv");
	  $i=0; $myfilearray="";
          while ($file = readdir($dir_handle)) {
echo "<!-- ".is_file($phpgw_info["server"]["app_root"].$sep."outlook2xyz".$sep."conv".$sep.$file)." -->";
            if ((substr($file, 0, 1) != ".") && is_file($phpgw_info["server"]["app_root"].$sep."outlook2xyz".$sep."conv".$sep.$file) ) {
	      $myfilearray[$i] = $file;
	      $i++;
            }
          }
          closedir($dir_handle);
	  sort($myfilearray);
	  for ($i=0;$i<count($myfilearray);$i++) {
	    echo "<OPTION VALUE='$myfilearray[$i]'>$myfilearray[$i]</OPTION>";
	  }
        ?>
      </SELECT><P></LI>
  <LI><INPUT NAME="download" TYPE="checkbox" VALUE="Debug output in browser" CHECKED>Debug output in browser (Uncheck to download output.)</LI>
  <LI><INPUT NAME="private" TYPE="checkbox" VALUE="private" CHECKED>Mark records as private (Addressbook)</LI>
  <LI>Use this basedn (LDAP)<BR><INPUT NAME="basedn" TYPE="text" VALUE="<?echo $fakebasedn?>" SIZE="48"></LI>
  <LI>Use this context for storing Contacts (LDAP)<BR><INPUT NAME="context" TYPE="text" VALUE="<?echo $fakecontext?>" SIZE="48"></LI>
  <LI><INPUT NAME="convert" TYPE="submit" VALUE="Download Conversion"></LI>
</OL>
</FORM>
</TD></TR></TABLE></TABLE>

<?php
  $phpgw->common->phpgw_footer();
} else {
  include ($phpgw_info["server"]["app_root"].$sep."outlook2xyz".$sep."conv".$sep.$conv_type);

  if ($private=="") { $private="public"; }
  $row=0;
  $buffer="";
  $o = new outlook_conv;
  $buffer = $o->outlook_start_file($buffer,$basedn,$context);
  $fp=fopen($tsvfile,"r");
  while ($data = fgetcsv($fp,8000,",")) {
    $num = count($data);
    $row++;
    if ($row == 1) {
      $header = $data;
    } else {
      $buffer = $o->outlook_start_record($buffer);
      for ($c=0; $c<$num; $c++ ) {
        //Send name/value pairs along with the buffer
        if ($o->outlook[$header[$c]]!="" && $data[$c]!="") {
          $buffer = $o->outlook_new_attrib($buffer, $o->outlook[$header[$c]],$data[$c]);
        }
      }
      $buffer = $o->outlook_end_record($buffer,$private);
    }
  }
  fclose($fp);

  $buffer = $o->outlook_end_file($buffer);
  if ($download == "") {
    if($conv_type=="Debug LDAP" || $conv_type=="Debug SQL" ) {
      header("Content-disposition: attachment; filename=\"conversion.txt\"");
      //header("Content-type: text/plain");
      header("Content-type: application/octetstream");
      header("Pragma: no-cache");
      header("Expires: 0");
      echo $buffer;
    } else {
      $phpgw->common->phpgw_header();
      echo parse_navbar();
      echo "<pre>$buffer</pre>";
      $phpgw->common->phpgw_footer();
    }
  } else {
    $phpgw->common->phpgw_header();
    echo parse_navbar();
    echo "<pre>$buffer</pre>";
    $phpgw->common->phpgw_footer();
  }
}
?>
