<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>RefDB add reference form</title>
        <link href="refdbqxhtml.css" type="text/css" rel="stylesheet"/>
    </head>
    <body>
        <h1>RefDB login form</h1>
        <div class="navigation">
            <h2>Navigation</h2>
            <ul class="navigationMenu">
                <li>
                    <a href="index.html">Login screen</a>
                </li>
                <li>
                    <a href="refdbquery.html">Search references</a>
                </li>
                <li>
                    <a href="refdbkajquery.html">Search keywords, authors, journals</a>
                </li>
                <li>
                    <a href="refdbadd.html">Add new references</a>
                </li>
                <li>
                    <a href="refdbdbquery.php">Change database</a>
                </li>
                <li>
                    <a href="external.php">Search external database</a>
                </li>
                <li>
                    <a href="refdbadmin.php">Administrative Interface</a>
                </li>
                <li>
                    <a href="refdblogout.php">Logout</a>
                </li>
            </ul>
        </div>
        <div class="content">
	  <h1>RefDB add reference form</h1>
	  <p>
	   <?
	     if( $submit == "Add" )
	     {
	   	//if nothing was entered into the add reference text box
	  	if( empty($addref) )
		{
			print "Error: No reference data was entered.";
		}
		//Data was entered
		else
		{
			//This set of elseifs was written based on the simple check
			//in the original access.js file included in the old RefDB
			//interface.  It checks to make sure necessary text for the
			//particular reference type is there
			$correct = false;
			if( $addformat == "ris" )  //RIS format
			{
				if( ereg("TY  - ",$addref) )
					$correct = true;
				else
					print "This does not look like RIS data.\n";
			}
			else if( $addformat == "bib2ris" ) //BibTeX format
			{
				if( ereg("@", $addref) )
					$correct = true;
				else
					print "This does not look like BibTeX data.\n";
			}
			else if( $addformat == "med2ris.pl" ) //PubMed
			{
				if( ereg("PMID", $addref) )
					$correct = true;
				else
					print "This does not look like PubMed data.\n";
			}
			//If the reference passes the checks
			if( $correct )
			{
				//create a temporary file with the reference(s) in it
				$addref = ereg_replace("\r\n","\n",$addref);
				$handle = fopen("tempref","wb");
				fwrite($handle, $addref);
				fclose($handle);
	
				$name = $_SESSION["name"];
				$passwd = $_SESSION["passwd"];
				$database = $_SESSION["database"];
				$refdbc = $_SESSION["refdbc"];

				//refdbc command to add a reference
				$cmd = "$refdbc -c stdout -u $name -w $passwd -C addref -d $database < tempref";
				exec($cmd, $lines);
				
				unlink("tempref"); //delete the temp file
	
				//print the output
				foreach($lines as $line)
				{
					print $line."<br/>\n";
				}
			}
			print "<br/><a href='refdbadd.html'>Add another reference</a>\n";
		}
	    }
	    elseif( $submit == "Add File" )
	    {
		if ($referencefile == "none")
		{
		 	echo "Problem: No file selected.";
			exit;
		}
		if ($referencefile_size==0)
		{
			echo "Problem: File is of size zero.";
			exit;
		}
		if (!is_uploaded_file($referencefile))
		{
			echo "Problem: File did not upload successfully.";
			exit;
		}
		
		if( !move_uploaded_file($referencefile, $referencefile_name) )
		{
			echo "Problem: Could not upload file onto server.";
			exit;
		}

		$refdbc = $_SESSION["refdbc"];
		$cmd = "$refdbc -u $name -w $passwd -d $database -c stdout -C addref $referencefile_name";
		exec($cmd, $lines);
		foreach($lines as $line)
		{
			print "$line<br/>";
		}
		if( isset($deletefile) )
		{
			unlink($referencefile_name);
		}

	    }
	  ?></p>
	  </div>
        <div class="footer">
            <p><a href="http://refdb.sourceforge.net">RefDB</a> reference manager and bibliography
                tool V. <!--VERSION-->0.9.8-1<!--/VERSION--></p>
        </div>	  
  </body>
</html>
