A PHP Noding Library

This PHP library (and sample client) was written from the perl sources above. It was written to facilitate the creation of custom web-based noding environments for doing all sorts of stuff. It was written quickly (in about two hours); but it serves the purpose.

I am personally going to use this library to create a sort of automatic template that I can use to hammer out kanji. It will do things for me like automatically grabbing all of the readings for a kanji, an ASCII picture, etc. from sources on the web and assembling them into a node template to which I may add my own additional content; allowing me to focus more on putting my own personal writings and research into the node rather than having to do all of the fact-gathering grunt work by hand.

This was tested under PHP4, although it should work with PHP3. YMMV.


File 1 of 2:
e2auto.php.inc


<?php

define(E2DEBUG,1);

function _e2_debug($string)
{
	if (!E2DEBUG)
		return;

	echo $string;
	flush;
}

function e2_login($username, $password)
{
	_e2_debug("e2_login() Connecting...<br>");

	$fp = fsockopen ("www.everything2.com", 80, $errno, $errstr, 30);
	if (!$fp)
	{
	    echo "$errstr ($errno)<br>\n";
	    return -1;
	}
    
	_e2_debug("Connected!  Attempting to log in...<br>");

	fwrite($fp,
		 "GET /?op=login&node_id=109&lastnode_id=0"
		."&user=".urlencode($username)
		."&passwd=".urlencode($password)
		." HTTP/1.0\r\n\r\n"
	);

	while( !feof($fp) )
	{
		$buf .= fgets($fp,128);	
	}

	if ( !eregi("Set-Cookie: userpass=(.*); path=",$buf,$regs) )
	{
		echo "Unable to login!<br>";
		return -1;
	}
	$handle=$regs[1];

	_e2_debug("Login successful!<br>");

	fclose($fp);

	return $handle;
}

function _e2_writenodeshell($handle,$title)
{
	$fp = fsockopen ("www.everything2.com", 80, $errno, $errstr, 30);
	if (!$fp)
	{
	    echo "$errstr ($errno)<br>\n";
	    return -1;
	}

	$httpdata = "node=".urlencode($title)."&op=new&type=e2node";

	_e2_debug("POSTing nodeshell...<br>\n");

	fwrite($fp,
		 "POST / HTTP/1.0\r\n"
		."Cookie: userpass=".$handle."\r\n"
		."Content-length: ".strlen($httpdata)."\r\n\r\n"
		.$httpdata."\r\n"
	);

	while( !feof($fp) )
	{
		$buf .= fgets($fp,128);	
	}

	if ( !eregi("<A HREF=\"\\/index.pl\\?node_id=[0-9]+&lastnode_id=([0-9]+)\">"
		        ,$buf,$regs) )
	{
		echo "Unable to find nodeshell id!<br>";
		return -1;
	}
	$lastnode_id=$regs[1];

	_e2_debug("Nodeshell ($lastnode_id) creation successful!<br>");

	fclose($fp);

	return $lastnode_id;
}

function e2_writenode($handle,$title,$content,$type)
{

	$type=strtolower($type);

	if ( !($type == "person" || $type == "place" ||
		 $type == "thing"  || $type == "idea") )
	{
		echo "Invalide type ($type) for node \"".htmlentities($title)."\" !<br>";
		return -1;
	}

	_e2_debug("e2_writenode() Creating nodeshell...<br>");

	if ( !($node_id = _e2_writenodeshell($handle,$title)) )
		return -1;

	$fp = fsockopen ("www.everything2.com", 80, $errno, $errstr, 30);
	if (!$fp)
	{
	    echo "$errstr ($errno)<br>\n";
	    return -1;
	}

	$httpdata = "node=new+writeup"
	         . "&op=new"
			 . "&type=writeup"
			 . "&node=".$node_id
			 . "&writeup_notnew=1"
			 . "&writeup_doctext=".urlencode($content)
			 . "&writeup_parent_e2node=".$node_id
			 . "&writeuptype-".$type.".x=0"
			 . "&writeuptype-".$type.".y=0";

	fwrite($fp,
		 "POST / HTTP/1.0\r\n"
		."Cookie: userpass=".$handle."\r\n"
		."Content-length: ".strlen($httpdata)."\r\n\r\n"
		.$httpdata."\r\n"
	);

	_e2_debug("Sent:<br><pre>".htmlentities($httpdata)."</pre><br>");

	while( !feof($fp) )
	{
		$buf .= fgets($fp,128);	
	}

	_e2_debug("Node have been created...go find out!<br>");

	fclose($fp);
}

?>


File 2 of 2:
e2client.php


<?php

/*
 * Disable Magic Quotes:
 */
set_magic_quotes_runtime(0);

/*
 * Include our e2 autonoding library:
 */
include("e2auto.php.inc");

/*
 * Called to input a node; called by default.
 */
function do_getwriteup()
{
	echo "<h2><u>AP's Customizable PHP E2 Write-Up Portal:</u></h2>\n";

	echo "<form name=\"e2_ap\" action=\"index.php\" method=\"post\">\n";
	echo "<input type=\"hidden\" name=\"func\" value=\"newwriteup\">\n";

	echo "<blockquote>\n";

	echo "\t<p>\n";
	echo  "\t\t<strong>Username:</strong> "
	     ."<input type=\"text\" name=\"e2_username\" size=\"16\"><br/>\n";
	echo  "\t\t<strong>Password:</strong> "
	     ."<input type=\"password\" name=\"e2_password\" size=\"16\">\n";
	echo "\t</p>\n";

	echo "\t<p>\n";
	echo  "\t\t<strong>Title:</strong> "
	     ."\t\t<input type=\"text\" name=\"e2_title\" size=\"64\"><br/>\n";

	echo "\t<strong>Node content:<br/>\n";
	echo  "\t<textarea name=\"e2_content\""
             ."ROWS=\"30\" COLS=\"80\" wrap=virtual></textarea><br/>\n";
	echo "\t<strong>Set the type:</strong> ";
	echo "\t<select name=\"e2_type\">\n";
	echo "\t\t<option value=\"person\">person\n";
	echo "\t\t<option value=\"place\">place\n";
	echo "\t\t<option value=\"idea\">idea\n";
	echo "\t\t<option value=\"thing\">thing\n";
	echo "\t</select><br/>\n";
	echo "\t<input type=\"submit\" value=\"sumbit\">\n";
	echo "\t</p>\n";

	echo "</blockquote>\n";

	echo "</form>\n";
}

/*
 * Called to actually write the node out to e2.
 */
function do_newwriteup()
{

	global $e2_username, $e2_password;
	global $e2_title, $e2_content, $e2_type;

	if (!($e2_username) || !($e2_password) ||
		!($e2_title) || !($e2_content) || !($e2_type))
	{
		echo  "<strong>Missing some form information!"
		     ."  Please re-enter!!</strong>";
		exit();
	}

	if ( !($handle = e2_login($e2_username,$e2_password)) )
		exit();

	e2_writenode($handle,$e2_title,$e2_content,$e2_type);
}

/*
 * The Start of Execution
 * main() {
 */

if ( !isset($func) ) $func = "";
switch($func)
{
	case "newwriteup":
		do_newwriteup();
		break;
	case "getwriteup":
	default:
		do_getwriteup();
}

/*
 * }
 */

?>