<?php
/**
* zencart Jamroom Bridge Plugin
* @package Talldude_Library
* @subpackage Jamroom_Bridges
* @copyright 2006 by Brian Johnson / Talldude Networks LLC
* @author Brian Johnson -
bigguy@talldude.net
* @filesource
* $Id: zencart.php,v 3.11 2006/12/05 16:57:26 bigguy Exp $
*/
defined('IN_JAMROOM') or exit();
//------------------------------------------------------------
// configuration
//------------------------------------------------------------
$GLOBALS['zencart']['server'] = 'localhost'; // machine zencart is running on
$GLOBALS['zencart']['dbname'] = '**************'; // zencart Database name
$GLOBALS['zencart']['dbuser'] = '**************'; // database user name
$GLOBALS['zencart']['dbpass'] = '**************'; // database user password
$GLOBALS['zencart']['prefix'] = 'zen_'; // default zencart table prefix
$GLOBALS['zencart']['language'] = 'english'; // default zencart user language
//------------------------------------------------------------
// You should not need to edit below here
//------------------------------------------------------------
/**
* The jrBridge_check function is used before the "create" function
* as a way to "precheck" the zencart forum and see if a user account
* name already exists - this allows an error to be returned in
* Jamroom so name duplication does not occur.
*
* @param array Incoming array of User Info
*
* @return bool Returns true/false on success/fail
*/
function jrBridge_zencart_check($_data)
{
// Verify we recieved our input array
if (!is_array($_data)) {
return('ERROR: jrBridge_create() Input _data array is empty!');
}
// Now connect up to the zencart database and do our work
$con = jrBridge_zencart_connect();
if (!is_resource($con)) {
return("ERROR: unable to open the zencart database - MySQL error: ".$con."");
}
$user = dbEscapeString($_data['user_nickname']);
// check to see if the user account already exists
$req = "SELECT *
FROM ".$GLOBALS['zencart']['prefix']."customers
WHERE customers_nick='".$user."'";
$res = mysql_query($req,$con);
if ($res != "" && mysql_num_rows($res) > 0) {
return("ERROR: username ".$user." already exists in zencart user table!");
}
return(true);
}
function zen_rand($min = NULL, $max = NULL) {
static $seeded;
if (!$seeded) {
mt_srand((double)microtime()*1000000);
$seeded = true;
}
if (isset($min) && isset($max)) {
if ($min >= $max) {
return $min;
} else {
return mt_rand($min, $max);
}
} else {
return mt_rand();
}
}
function jrBridge_zencart_encrypt_password($plain) {
$password = '';
for ($i=0;$i<10;$i++) {
$password .= zen_rand();
}
$salt = substr(md5($password), 0, 2);
$password = md5($salt . $plain) . ':' . $salt;
return $password;
}
/**
* The jrBridge_create function is used for "creating" a new
* entry in the zencart forum.
*
* @param array Incoming array of User Info
*
* @return mixed Returns error string on failure, bool true on success
*/
function jrBridge_zencart_create($_data)
{
// Verify we recieved our input array
if (!is_array($_data)) {
return('ERROR: jrBridge_create() Input _data array is empty!');
}
// Now connect up to the zencart database and do our work
$con = jrBridge_zencart_connect();
if (!is_resource($con)) {
return("ERROR: unable to open the zencart database - MySQL error: ".$con."");
}
// our user password comes in unencrypted - lets MD5 it
$pass = jrBridge_zencart_encrypt_password($_data['user_password']);
$user = dbEscapeString($_data['user_nickname']);
$req1 = "SELECT * FROM jamroom_user WHERE user_id='".$_data['user_id']."'";
$res1 = mysql_query($req1,$con);
$arr1 = mysql_fetch_array($res1);
$req2 = "SELECT * FROM jamroom_band_info WHERE band_id='".$arr1['user_band_id']."'";
$res2 = mysql_query($req1,$con);
$arr2 = mysql_fetch_array($res2);
$grouppricing = dbEscapeString($arr2['band_quota']);
$mail = dbEscapeString($_data['user_emailadr']);
// get what our next customers_id is going to be
$req = "SELECT MAX(customers_id + 1) AS new_id FROM ".$GLOBALS['zencart']['prefix']."customers";
$res = mysql_query($req,$con);
if (mysql_num_rows($res) > 0) {
$_row = mysql_fetch_assoc($res);
$new_id = $_row['new_id'];
}
mysql_free_result($res);
if (!is_numeric($new_id) || $new_id <= 1) {
mysql_close($con);
return("ERROR: unable to determine the incremental user_id value from zencart - verify database connectivity");
}
// Insert our User account
$req = "INSERT INTO ".$GLOBALS['zencart']['prefix']."customers (customers_id,customers_gender,customers_nick,customers_default_address_id,customers_password,customers_email_address,customers_group_pricing)
VALUES ('".$new_id."','m','".$user."','".$new_id."','".$pass."','".$mail."','".$grouppricing."')";
mysql_query($req,$con) or $err = mysql_errno() ."-". mysql_error();
if (isset($err)) {
mysql_close($con);
return("ERROR: failed to insert new user into the zencart customers table - MySQL error: ".$err."");
}
// Insert group account
$req = "INSERT INTO ".$GLOBALS['zencart']['prefix']."address_book (address_book_id,customers_id,entry_gender,entry_country_id,entry_zone_id)
VALUES ('".$new_id."','".$new_id."','m','223','57')";
mysql_query($req,$con) or $err = mysql_errno() ."-". mysql_error();
if (isset($err)) {
mysql_close($con);
return("ERROR: failed to insert new user into the zencart address_book table<br>MySQL error: ".$err."");
}
mysql_close($con);
return(true);
}
/**
* The jrBridge_create function is used for "creating" a new
* entry in the zencart forum.
*
* @param array Incoming array of User Info
*
* @return mixed Returns error string on failure, bool true on success
*/
function jrBridge_zencart_update($_data)
{
// Verify we recieved our input array
if (!is_array($_data)) {
return('ERROR: jrBridge_create() Input _data array is empty!');
}
// Now connect up to the zencart database and do our work
$con = jrBridge_zencart_connect();
if (!is_resource($con)) {
return("ERROR: unable to open the zencart database - MySQL error: ".$con."");
}
// Update User account
$req = "UPDATE ".$GLOBALS['zencart']['prefix']."customers SET ";
if (strlen($_data['user_password']) > 0) {
$req .= "customers_password = '".jrBridge_zencart_encrypt_password($_data['user_password'])."',
customers_email_address = '".dbEscapeString($_data['user_emailadr'])."' ";
}
else {
$req .= "customers_email_address = '".dbEscapeString($_data['user_emailadr'])."' ";
}
$req .= "WHERE customers_nick = '".dbEscapeString($_data['user_nickname'])."'
LIMIT 1";
$res = mysql_query($req,$con) or $err = mysql_errno() ."-". mysql_error();
if (mysql_affected_rows($con) == 0) {
mysql_close($con);
return("ERROR: failed to update user ".$_data['user_nickname']." in zencart user table - MySQL error: ".$err."");
}
mysql_close($con);
return(true);
}
/**
* The jrBridge_delete function is used for "deleting" an entry
* in the zencart forum.
*
* @param string Username to remove from forum
*
* @return mixed Returns error string on failure, bool true on success
*/
function jrBridge_zencart_delete($_data)
{
// Verify we recieved our input array
if (!is_array($_data)) {
return('ERROR: jrBridge_delete() Input _data array is empty!');
}
// Now connect up to the zencart database and do our work
$con = jrBridge_zencart_connect();
if (!is_resource($con)) {
return("ERROR: unable to open the zencart database - MySQL error: ".$con."");
}
// Delete User account
$req = "DELETE FROM ".$GLOBALS['zencart']['prefix']."customers
WHERE customers_nick = '".dbEscapeString($_data['user_nickname'])."'
LIMIT 1";
$res = mysql_query($req,$con) or $err = mysql_errno() ."-". mysql_error();
if (mysql_affected_rows($con) == 0) {
mysql_close($con);
return("ERROR: failed to delete user ".$_data['user_nickname']." from zencart user table - MySQL error: ".$err."");
}
mysql_close($con);
return(true);
}
/**
* The jrBridge_test function is used for "testing" the Bridge Plugin.
* Any "checks" can be added to this function
*
* @return mixed Returns error string on failure, bool true on success
*/
function jrBridge_zencart_test()
{
// Test Database connection
$con = jrBridge_zencart_connect();
if (!is_resource($con)) {
return("ERROR: unable to open the zencart database - MySQL error: ".$con."");
}
// Test incremental USER ID select
$req = "SELECT MAX(customers_id + 1) AS new_id FROM ".$GLOBALS['zencart']['prefix']."customers";
$res = mysql_query($req,$con);
if (mysql_num_rows($res) > 0) {
$_row = mysql_fetch_assoc($res);
$new_id = $_row['new_id'];
}
mysql_free_result($res);
if (!is_numeric($new_id) || $new_id <= 1) {
mysql_close($con);
return("ERROR: unable to determine the incremental user_id value from zencart - verify database connectivity");
}
mysql_close($con);
return(true);
}
/**
* The jrBridge_login function is executed upon a successful login by
* a Jamroom User Account.
*
* @return mixed Returns error string on failure, bool true on success
*/
function jrBridge_zencart_login($_data)
{
return(true);
}
/**
* The jrBridge_logout function is executed upon a successful logout by
* a Jamroom User Account.
*
* @return mixed Returns error string on failure, bool true on success
*/
function jrBridge_zencart_logout($_data)
{
return(true);
}
/**
* The jrBridge_create function is used for "creating" a new
* entry in the zencart forum.
*
* @param array Incoming array of User Info
*
* @return mixed Returns error string on failure, bool true on success
*/
function jrBridge_zencart_connect()
{
// Now connect up to the zencart database and do our work
$con = mysql_connect($GLOBALS['zencart']['server'],$GLOBALS['zencart']['dbuser'],$GLOBALS['zencart']['dbpass']) or $err = mysql_errno() .' - '. mysql_error();
if (isset($err)) {
return('Invalid MySQL Server Name, Username or Password');
}
mysql_select_db($GLOBALS['zencart']['dbname'],$con) or $err = mysql_errno() .' - '. mysql_error();
if (isset($err)) {
return('Invalid MySQL Database');
}
return($con);
}
?>