Location: PHPKode > projects > Web-NoteTaker > notetaker/include/user.php
<?php
// NoteTaker
// Created and Coded by Bo Ahlberg
// Copyright 2008 by Bo Ahlberg ( bahlberg at mac.com )
// This code is licensed "AS-IS", No warranty is expressed or implied as to the
// suitability of this software for any purpose. Further, this code is licensed
//	under the:
//    Creative Commons Attribution-Share Alike 3.0 United States License
// This means you are free to:
//		to Share - to copy, distribute, display, and perorm work
//	  to remix - to make derivative works
//  Under the Following Conditions:
//		Attribution - You must attribute the work in the manner 
//    		specified by the author or licensor (but not in any way 
//    		that suggests that they endorse you or your use of the work).
//    Share Alike. If you alter, transform, or build upon this work, 
//				you may distribute the resulting work only under the same, 
//				similar or a compatible license.
//	For any reuse or distribution, you must make clear to others the license 
//		terms of this work. The best way to do this is with a link to this web page.
//	Any of the above conditions can be waived if you get permission from the 
//		copyright holder.
//	Apart from the remix rights granted under this license, nothing in this 
//		license impairs or restricts the author's moral rights.
//

function userMain(  )
{ global $process, $form;
	$mode = (array_key_exists( 'mode', $_REQUEST ))? $_REQUEST['mode'] : "default";
	$func = (array_key_exists( 'func', $_REQUEST ))? $_REQUEST['func'] : "default";
	$id   = (array_key_exists( 'id',   $_REQUEST ))? $_REQUEST['id']   : 0;
	$usr = "";
	switch( $mode )
	{
		case 'login' :
			$process->procLogin();
			break;
		case 'logout':
			$process->procLogout();
			break;
		case 'update':
			$process->procEditAccount();
			break;
		case 'newpass':
			$process->procForgotPass();
			break;
		case 'register':
			if ( $func == 'default' )
				$usr.= userRegister( );
			else
				$process->procRegister();
			break;
		case 'forgot':
			$usr.= userForgotPass( );
			breal;
		case 'view':
			$usr.= userInfo( );
			break;
		case 'edit':
			$usr.= userEdit( );
			break;
		default:
			$usr.= loginForm();
			break;
	}			
	$ret = makeCell( 1, "<!-- userMain:start -->\n".$usr."<!-- userMain:end -->\n" );
	
	return $usr;
}

function userMenu( )
{
	$ret.= userStatus();
	
	return $ret;
}

function userRegister( )
{ global $session, $form;
	//$reg = "";
	//$reg.= "<TABLE>";
	if( $session->logged_in )
	{
		$reg = makeRow( makeCell( 3, "Already Registered", 'kanbanTitle' ) );
		Message( 1, "Registered<BR><p>We're sorry <b>".$session->username."</b>, but you've already registered." );
	}
	else if( isset($_SESSION['regsuccess']) )
	{	/* Registration was successful */
		if(	$_SESSION['regsuccess']	)
		{
			$reg = makeRow( makeCell( 3, "Registion Successful!", 'kanbanTitle' ) );
      Message( 1, "<p>Thank you <b>".$_SESSION['reguname']."</b>, your information has been added to the database, you may now <a href='index.php?action=user'>log in</a>.</p>" );
		}
		else
		{	/* Registration failed */
      $reg = makeRow( makeCell( 3, "Registration Failed", 'kanbanTitle' ) );
      Message( 1, "<p>We're sorry, but an error has occurred and your registration for the username <b>".$_SESSION['reguname']."</b>, could not be completed.<br>Please try again at a later time.</p>" );
		}
		unset($_SESSION['regsuccess']);
		unset($_SESSION['reguname']);
	}
	else
	{
		$reg = makeRow( makeCell( 3, "Register", 'kanbanTitle' ) );
		if( $form->num_errors > 0 )
		{
		   Message(4, "<td><font size=\"2\" color=\"#ff0000\">".$form->num_errors." error(s) found</font></td>" );
		}
		//$reg.= "\n<form action='index.php?action=user&amp;mode=register&amp;func=post' method='POST'>";
		$form =  makeForm( 'user', 'regiser', '', '', '&amp;func=post', 
											makeTable( 100, 
														 makeRow(	 makeCell( 1, "Username" )
																			.makeCell( 1, makeUserInput( 'text', 'user', '30', $form->value("user") ) )
																			.makeCell( 1, $form->error("user") ) )
														.makeRow(	 makecell( 1, "Password:" )
																			.makeCell( 1, makeUserInput( 'password', 'pass', '30', $form->value("pass") ) )
																			.makeCell( 1, $form->error("pass") ) )
														.makeRow(	 makeCell( 1, "Email" )
																			.makeCell( 1, makeUserInput( 'text', 'email', '50', $form->value("email") ) )
																			.makeCell( 1, $form->error("email") ) )
														.makeRow(	 makeCell( 2,	makeUserInput( 'hidden', 'subjoin', 30, '1' ) ) )
														.makeRow(	 makeCell( 2, makeActionLink( 'user', '', '', '', 'Back to Main') ) ) ), 'Join');
		//$reg.= "\n</form>";
		$reg.= makeRow( makeCell( 1, $form ) );
	}
	//$reg.= "</TD></TR>";
	//$reg.= "</TABLE>";
	//$reg.= "";
	return makeTable( 0, $reg );
}

function userLink( $opt, $value, $text )
{
	$ret = "<a href='index.php?action=user";
	$ret.= ($opt != "")? "&amp;".$opt."=".$value."'" : "";
	$ret.= ">";
	$ret.= $text;
	$ret.= "</a>";
	
	return $ret;
}

function userStatus( )
{	global $session, $form;

	$cols = 3;
	if( !$req_user 
			|| strlen($req_user) == 0 
			|| !eregi("^([0-9a-z])+$", $req_user) 
			|| !$database->usernameTaken($req_user))
	{
	   Message( "Username not registered" );
	}

	if( $session->logged_in )
	{
		$msgTitle = "Welcome <em>".$session->username."</em>";
		$msgIn.= makeCell( 1, userLink( "mode=view&amp;user", $session->username, "View" ), 'kanban', '#FFFFCC' );
		$msgIn.= makeCell( 1, userLink( "mode", "edit", "Edit" ), 'kanban', '#FFFFCC' );
		if( $session->isAdmin() )
		{
		  $msgIn.= makeCell( 1, "<a href='index.php?action=admin'>Admin</a>", 'kanban', '#FFCC66' );
		  $cols++;
		}
		$msgIn.= makeCell( 1, "&nbsp;", 'kanban' );
		$msgIn.= makeCell( 1, "<a href='index.php?action=user&amp;mode=logout'>Logout</a>", 'kanban', '#00CCFF' );
	}
	else
	{
		$msgTitle = userLink( "mode", "default", "Login" );
		$msgIn = "";
		if($form->num_errors > 0)
		{
			Message( 4, $form->num_errors." error(s) found" );
			foreach( $form->getErrorArray() as $error )
			{
				Message( 4, $error );
			}
		}
	}
	$msg = makeTable( 0, makeRow( makeCell( 1, $msgTitle ).$msgIn) );

	return $msg;
}

function userInfo( )
{ global $database, $session, $form;
	/* Requested Username error checking */
	$ret = "";
	$req_user = trim($_GET['user']);
	if( !$req_user 
			|| strlen($req_user) == 0 
			|| !eregi("^([0-9a-z])+$", $req_user) 
			|| !$database->usernameTaken($req_user))
	{
	   Message( "Username not registered" );
	}
	else 
	{
		/* Logged in user viewing own account */
		if(strcmp($session->username,$req_user) == 0)
		{
		   $ret.= "<h1>My Account</h1>";
		}
		/* Visitor not viewing own account */
		else{
		   $ret.= "<h1>User Info</h1>";
		}
		
		/* Display requested user information */
		$req_user_info = $database->getUserInfo($req_user);
		
		/* Username */
		$ret.= "<b>Username: ".$req_user_info['username']."</b><br>";
		
		/* Email */
		$ret.= "<b>Email:</b> ".$req_user_info['email']."<br>";
		
		/**
		 * Note: when you add your own fields to the users table
		 * to hold more information, like homepage, location, etc.
		 * they can be easily accessed by the user info array.
		 *
		 * $session->user_info['location']; (for logged in users)
		 *
		 * ..and for this page,
		 *
		 * $req_user_info['location']; (for any user)
		 */
		
		/* If logged in user viewing own account, give link to edit */
		if(strcmp($session->username,$req_user) == 0){
		   $ret.= "<br><a href='index.php?action=user&amp;mode=edit'>Edit Account Information</a><br>";
		}
		
		/* Link back to main */
		$ret.= "<br>Back To [<a href='index.php'>Home</a>]<br>";
	}
	$ret = makeCell( 1, $ret );
	
	return $ret;
}

function userEdit( )
{	global $form, $session, $process;
	$ret = "";
	if($session->logged_in)
	{
		$ret.= "<TABLE>";
		$ret.=   "<TR>";
		$ret.=     makeCell( 1, "User Account Edit" );
		$ret.=     makeCell( 1, $session->username  );
		if($form->num_errors > 0)
		{
			Message( 1, "userEdit: ".$form->num_errors." error(s) found" );
		}
		$ret.=   "</TR>";
		$ret.=   "<TR>";
		$ret.=     "<TD colspan=2 class='kanban'>";
		$ret.=       "<form action='index.php?action=user&amp;mode=update' method='POST'>";
		$ret.=         "<table>";
		$ret.=           "<tr>";
		$ret.=             makeCell( 1, "Current Password" );
		$ret.=             makeCell( 1, makeUserInput( 'password', 'curpass', '30', $form->value("curpass") ) );
		$ret.=             makeCell( 1, $form->error("curpass") );
		$ret.=           "</tr>";
		$ret.=           "<tr>";
		$ret.=             makeCell( 1, "New Password" );
		$ret.=             makeCell( 1, makeUserInput( 'password','newpass', '30', $form->value("newpass") ) );
		$ret.=             makeCell( 1, $form->error("newpass") );
		$ret.=           "</tr>";
		$ret.=           "<tr>";
		$ret.=             makeCell( 1, "Email" );
		$ret.=             makeCell( 1, makeUserInput( 'text', 'email', '50', 
					                        ($form->value("email") == "")? $session->userinfo['email'] 
																	: $form->value("email") ) );
		$ret.=             makeCell( 1, $form->error("email") );
		$ret.=           "</tr>";
		$ret.=           "<tr>";
		$ret.=             makeCell( 3, makeUserInput( 'hidden', 'subedit', '', '1' )
																		.makeUserInput( 'submit', '', '', 'Edit Account' ) );
		$ret.=           "</tr>";
		$ret.=         "</table>";
		$ret.=       "</form>";
		$ret.=     "</TD>";
		$ret.=   "</TR>";
		$ret.= "</TABLE>";
	}
	
	return $ret;
}

function userForgotPass( )
{ global $form, $session, $process;
	$ret = "";
	$ret.= "<FORM action='index.php?action=user&amp;mode=newpass' method='POST'>";
	$ret.= "<TABLE>";
	$ret.=   "<TR>";
	$ret.=     makeCell( 1, "Forgot Password" );
	$ret.=     makeCell( 1, "A new password will be generated for you and sent to the email address<br>associated with your account, all you have to do is enter your username: ".$form->error("user") );
	$ret.=   "</TR>";
	$ret.=   "<TR>";
	$ret.=     makeCell( 1, "Username" );
	$ret.=     makeCell( 1, makeUserInput( 'text', 'user', '30', $form->value("user") ) );
	$ret.=   "</TR>";
	$ret.=   "<TR>";
	$ret.=     makeCell( 1, makeUserInput( 'hidden', 'subforgot', '', "1" ) );
	$ret.=     makeCell( 1, makeUserInput( 'submit', '', '', "Get New Password" ) );
	$ret.=   "</TR>";
	$ret.= "</TABLE>";
	$ret.= "</FORM>";
	
	return $ret;
}
?>
Return current item: Web-NoteTaker