//
// session.inc
//
// This file contains the Nameboy engine's session-handling code.
//
// Currently, there is only one actual session variable, named
// "$boysession". This is a class object that contains all of the
// actual session variables, such as keywords and convey settings. The
// main advantage of this is that it makes it very clear what
// variables are propagated from one page to the next.
//
//
// Instructions for adding a variable for storage in the session object:
// 1) Add a "var" statement to the Nameboy_Session_Data class
// 2) If needed, add defaults values in the Nameboy_Session_Data
// constructor function and/or reset_search_settings
// 3) Add an "if (isset...)" statement to update the value
// (The strings [1], [2], and [3] below give hints on where to edit)
//
//
// Other notes:
// - The best tutorial on PHP4 session handling I've seen is at
// http://www.zend.com/zend/tut/session.php
// The manual has a bunch of important details too.
// - /etc/httpd/conf/php.ini has a bunch of important settings in it;
// default name of the session id, cookie usage, etc etc.
// - This file was once in the nameboy engine, but that's not right;
// it's better if they're separate, because the engine does _not_
// require session handling, and it shouldn't.
// - Keep in mind that if you set any variables in this file that
// aren't in a function, they'll be seen in every file in the system,
// and might override page parameters and stuff. Complicated stuff
// should go in functions to avoid this.
//
require_once("engine/initdb.inc");
// we need the class def for the enom obj.
require_once("includes/enom.inc");
// These classes always have to be loaded for the session to
// initialize properly.
require_once("includes/domainbucket.inc");
class Nameboy_Session_Data {
// These are the variables that are stored in the session
// Search parameters; at least some of these will change whenever
// the user does a new search, or even goes to another page.
var $searchid;
var $primary;
var $secondary;
var $tld;
var $convey;
var $convey_subcat;
var $domix;
var $dorhyme;
var $forsale;
var $page;
var $hyphens;
var $selecteddomains; // an associative array with domain.tld keys
// Domain "shopping cart" and related
var $bucket;
var $login; // if set, user is logged in
// Registration/payment things
var $reginfo; // CEnomInterface (registration) object
var $SurePay; // SurePay (credit card) object
var $regdomains; // RegistrationDomainList while registering
// these are set when the user enters the site, and probably shouldn't
// change. Default is "".
var $seller; // If set, only forsale results
// from this seller will be displayed.
// Except that this isn't implemented yet,
// so never mind.
var $registrar; // An identifier specifying which registrar
// available results should go to.
// [1] (insert more var statements here)
// Constructor; sets default values for the sessions
function Nameboy_Session_Data() {
// To set stuff, you use something like $this->tld = "com"
// Not sure what I need here yet
$this->seller = "";
$this->registrar = "";
$this->reset_search_settings();
$this->bucket = new Nameboy_Domain_Bucket;
// [2] (insert initialization code here)
}
// reset_search_settings - Clear the part of the session that
// contains the search parameters
// The idea is that the search form retains the previous search
// settings unless this function is called before displaying it.
function reset_search_settings() {
unset($this->searchid);
unset($this->primary);
unset($this->secondary);
$this->tld = "com";
unset($this->convey);
unset($this->convey_subcat);
unset($this->domix);
unset($this->dorhyme);
$this->forsale = "yes";
$this->page = 1;
unset($this->hyphens);
$this->selecteddomains = array();
// [2] search parameter settings (those that appear in the search
// form) should be initialized here
}
// A cheapo debugging function
function dump() {
echo "\nSearchid: $this->searchid
Primary: $this->primary
Secondary: $this->secondary
Page: $page
Hyphens: $this->hyphens\n";
}
}
//
// Session storage functions
//
// (each-page code to set up the session is still further down)
function boy_session_open($savepath, $sessionname) {
return TRUE;
}
function boy_session_close() {
return TRUE;
}
function boy_session_read($sid) {
$qresult = mysql_query("SELECT data FROM sessions WHERE sid='$sid';");
if ( ! $qresult ) {
error_log("Error getting session in boy_session_read: " . mysql_error(), 0);
return FALSE;
}
$resultarr = mysql_fetch_row($qresult);
return $resultarr[0];
}
function boy_session_write($sid, $data) {
$curtime = time();
$qdata = str_replace("'", "\'", $data);
$qresult = mysql_query("REPLACE INTO sessions VALUES ('$sid', '$qdata', $curtime);");
if ( ! $qresult ) {
error_log("Error creating/updating session in boy_session_write: " . mysql_error(), 0);
}
return TRUE;
}
function boy_session_destroy($sid) {
// we'll probably never use this one, but...
mysql_query("DELETE FROM sessions WHERE sid='$sid';");
return TRUE;
}
function boy_session_garbagecollect($maxlife) {
$deltime = time() - $maxlife;
$rq = mysql_query("DELETE FROM sessions WHERE time < $deltime;");
if ( ! $rq ) {
error_log("Error deleting sessions in boy_session_garbagecollect: " . mysql_error(), 0);
}
return TRUE;
}
//
// Grab the existing session, or create one if necessary
//
function nameboy_setup_session() {
global $HTTP_COOKIE_VARS;
global $boysession;
if ( ! isset($boysession) ) {
// This is a new session
$boysession = new Nameboy_Session_Data;
$log_statustag = "new";
}
else {
$log_statustag = "old";
}
// log format is:
// [unix timestamp] [session id] [new/old] [cookieid] [request uri]
$logcookie = isset($boycookie) ? $boycookie : "-";
error_log(sprintf("%d %s %s %s %s\n",
time(), session_id(), $log_statustag,
$logcookie, $SCRIPT_URL),
3, "/vol/log/session_log");
//
// Automatic user login
//
if ( ! isset($boysession->login) && ($HTTP_COOKIE_VARS["boylogin"] != "") &&
($HTTP_COOKIE_VARS["boylogin"] != " ") ) {
include_once("includes/account.inc");
$id = do_automatic_userlogin($HTTP_COOKIE_VARS["boylogin"]);
if ( $id ) {
// use auto-login feature because we've already verified the
// password in do_automatic_userlogin()
if ( ! open_existing_account($id, "", TRUE) ) {
error_log(__FILE__ . ": open_existing_account() failed during auto-login: '$errmsg' (this shouldn't happen)", 0);
}
} // id
else {
error_log(__FILE__ . ": do_automatic_userlogin() failed; bad login cookie (".$HTTP_COOKIE_VARS["boylogin"].")?", 0);
}
}
return $boysession;
} // nameboy_setup_session
if ( ! isset($NAMEBOY_SESSION_INITIALIZED) ||
(!$NAMEBOY_SESSION_INITIALIZED) ) {
// Set up MySQL handling of the session info
session_set_save_handler("boy_session_open",
"boy_session_close",
"boy_session_read",
"boy_session_write",
"boy_session_destroy",
"boy_session_garbagecollect");
init_db();
// Start up the session itself
// seems the session_start/session_register stuff can't be in
// a function.
session_start();
session_register("boysession");
$boysession = nameboy_setup_session();
}
//
// Update the current session object with parameters that were passed
// in to the script. Parameters that weren't passed in to this page
// retain their old value.
//
if ( isset($dosearch) && $dosearch=="yes" ) {
unset($boysession->dorhyme);
unset($boysession->forsale);
unset($boysession->hyphens);
}
if ( isset($searchid) ) {
$boysession->searchid = $searchid;
}
if ( isset($primary) ) {
$boysession->primary = $primary;
}
if ( isset($secondary) ) {
$boysession->secondary = $secondary;
}
if ( isset($tld) ) {
$boysession->tld = $tld;
}
if ( isset($convey) ) {
$boysession->convey = $convey;
}
if ( isset($convey_subcat) ) {
$boysession->convey_subcat = $convey_subcat;
}
if ( isset($domix) ) {
$boysession->domix = $domix;
}
if ( isset($dorhyme) ) {
$boysession->dorhyme = $dorhyme;
}
if ( isset($forsale) ) {
$boysession->forsale = $forsale;
}
if ( isset($page) ) {
$boysession->page = $page;
}
if ( isset($resultsperpage) ) {
$boysession->resultsperpage = $resultsperpage;
}
if ( isset($hyphens) ) {
$boysession->hyphens = $hyphens;
}
// [3]
$NAMEBOY_SESSION_INITIALIZED = TRUE;
?>