Forums

create license key from form values

samoht 07 Feb, 2009
hello again,

I have a client that wants a special registration form made that will create a License Key based on the value of the username + password + Date + Randomvalue
I already have some code from the old function for creating the license key but I need to include this code in a new registration form and have it create the license key from there and save it to the user table:

 public function getProductKey( ){
$data = $this->getLicenseData(); //this will no longer be holding all my values.

$strLogin = $data['username']; //I will need to grab this from the form  - I guess this will be a POST value??
$strPassword = $data['password']; //I will need to grab this from the form 

/* get the type code */

$nType = 3; //this value used to tell us which version of the software was going to be licensed but we now only have one version.

/* Convert date inputs to integer values */
$dLicense = $data['duration'] * 2592000; //this multiplies the duration by num seconds in a month - I now only want 90 days no matter what
$nTime = $dLicense + $data['start_time']; // this gives us the expiration date - should always be 90 days out

$nDay = date( 'd', $nTime );
$nMonth = date( 'm',$nTime );
$nYear = date( 'Y', $nTime );

$ourDate = $nDay.$nMonth.$nYear;

/* Generate a random number */
$nRandom = $rand & OxFFFF;

/* Generate hashes of login and password */
$nLogin = $this->getFoldedMD5( trim($strLogin) );
$nPassword = $this->getFoldedMD5( trim($strPassword) );

/* Combine date and flags */
$nClearDate = ($nType << 13) | (($nYear - 2000) << 9) | ($nMonth << 5) | $nDay;

/* Obfuscate date data */
$nDate = ($nClearDate ^ $nRandom) | ($nRandom << 16);

/* Create the check code */
$nStep = $nLogin ^ $nPassword ^ $nDate ^ 0x845E2F66;
//$nStep = 0x6c0b277b ^ 0xaab8585f ^ $nDate ^ 0x845E2F66;
$nUKey = ((($nStep >> 28) & 0xF) << 12) |
((($nStep >> 20) & 0xF) << 8) |
((($nStep >> 12) & 0xF) << 4) |
((($nStep >> 4) & 0xF) << 0);
$nVKey = ((($nStep >> 24) & 0xF) << 12) |
((($nStep >> 16) & 0xF) << 8) |
((($nStep >> 8) & 0xF) << 4) |
((($nStep >> 0) & 0xF) << 0);

$nCheck = (($nVKey << 16) | $nUKey) ^ 0x98FE1A5B;

$key = sprintf("%08x%08x%08x%08x", $nLogin, $nPassword, $nDate, $nCheck);
return $key;
}
 static function getFoldedMD5( $word ){
/* Convert string to ANSI */

$bHash = md5( $word, TRUE );

$a = ( (ord($bHash[0x0]) << 24) | (ord($bHash[0x1]) << 16) | (ord($bHash[0x2]) << 8) | (ord($bHash[0x3])) );
$b = ( (ord($bHash[0x4]) << 24) | (ord($bHash[0x5]) << 16) | (ord($bHash[0x6]) << 8) | (ord($bHash[0x7])) );
$c = ( (ord($bHash[0x8]) << 24) | (ord($bHash[0x9]) << 16) | (ord($bHash[0xA]) << 8) | (ord($bHash[0xB])) );
$d = ( (ord($bHash[0xC]) << 24) | (ord($bHash[0xD]) << 16) | (ord($bHash[0xE]) << 8) | (ord($bHash[0xF])) );

return $a ^ $b ^ $c ^ $d;
}


I tried to comment this so it all makes sense. I have a very basic registration form at this point:

<div  class="form_item">
	<div class="form_element cf_heading">
	<h1 id="" class="cf_text">Register:</h1>
	</div>
	<div class="clear"> </div>
</div>
<div  class="form_item">
	<div class="form_element cf_textbox">
	<label class="cf_label">Email:</label>
	<input class="cf_inputbox required validate-email" maxlength="150" size="30" id="text_1" name="text_1" type="text">
	</div>
	<div class="clear"> </div>
</div>
<div class="form_item">
	<div class="form_element cf_textbox">
	<label class="cf_label">Username:</label>
	<input class="cf_inputbox required" maxlength="150" size="30" id="text_2" name="text_2" type="text">
	</div>
	<div class="clear"> </div>
</div>
<div class="form_item">
	<div class="form_element cf_password">
	<label class="cf_label">Password</label>
	<input class="cf_inputbox required" maxlength="150" size="30" id="text_3" name="text_3" type="password">
	</div>
	<div class="clear"> </div>
</div>
<div  class="form_item">
	<div class="form_element cf_password">
	<label class="cf_label">Confirm Password:</label>
	<input class="cf_inputbox required" maxlength="150" size="30" id="text_5" name="text_5" type="password">
	</div>
	<div class="clear"> </div>
</div><div  class="form_item"><div class="form_element cf_button"><input value="Register" name="undefined" type="submit"><input value="Reset" type="reset"></div><div class="clear"> </div></div>


Any Ideas??
GreyHead 08 Feb, 2009
Hi samoht,

If I remember correctly the user info is available in the user object immediately after the Joomla Registration is complete. So, if you use the CF_Joomla_Registration plugin and then run your code after the plugin you should easily be able to pick up the username and possibly the password. (Joomla only stores the md5hash for the password but the unencoded version is available at the point of registration.)

Once you've created the hash you can use it as you like - though personally I wouldn't store it in the user table but use a new table linked by userid.

Bob
samoht 08 Feb, 2009
Thanks Bob,

I'll give that a try. So that would mean use the plugin and then put my code in the OnSubmit and make sure the run order is Plugins first then OnSubmit ?

You are right about not storing in the user table - I actually do have a custom table just for the key's that will be connected by userid, but I am not sure how I will know the correct userid to put into that form since they haven't been created yet... Any ideas?

Thanks again!
GreyHead 08 Feb, 2009
Hi samoht,

Try getting the user object after the registration and I think you will find the Userid and all the other info in there. To test use this code to dump the object to the screen (Joomla 1.5 code):
<?php
$user =& JFactory::getUser();
echo "user: ".print_r($user, true)."<br /><br />";
?>
This should work OK.

Bob

PS Sometimes though the output won't display on the screen because it's in mid-process and then you can use code like this to show a system message output:
<?php
$user =& JFactory::getUser();
global $mainframe;
$mainframe->enqueuemessage("user: ".print_r($user, true)."<br /><br />");
?>
samoht 09 Feb, 2009
Hey Bob,

I tried putting your test code in several places ("OnSubmit before sending email"; extra code before onsubmit in joomla reg plugin; extra code after onsubmit ...) but was not able to see anything dumped to the screen after submitting the form??

Where am I supposed to put this test code?
GreyHead 09 Feb, 2009
Hi samoht,

Onsubmit after is usually OK - do you have a redirect url set? That's the only reason I can think of why you wouldn't see something.

Bob
samoht 09 Feb, 2009
Oh, nope I have not set a redirect url.

do I need one to see it?
samoht 09 Feb, 2009
ok Bob,

I was able to to see the print_r by moving the code to the OnSubmit after but got this with the first code block:
<?php
$user =& JFactory::getUser();
echo "user: ".print_r($user, true)."<br /><br />";
?>

print_r returned:

You may now log in.user: JUser Object ( [id] => 0 [name] => [username] => [email] => [password] => [password_clear] => [usertype] => [block] => [sendEmail] => 0 [gid] => 0 [registerDate] => [lastvisitDate] => [activation] => [params] => [aid] => 0 [guest] => 1 [_params] => JParameter Object ( [_raw] => [_xml] => [_elements] => Array ( ) [_elementPath] => Array ( [0] => C:\xampp\htdocs\secureforms\libraries\joomla\html\parameter\element ) [_defaultNameSpace] => _default [_registry] => Array ( [_default] => Array ( [data] => stdClass Object ( ) ) ) [_errors] => Array ( ) ) [_errorMsg] => [_errors] => Array ( ) )




and this with the second code block:
<?php
$user =& JFactory::getUser();
global $mainframe;
$mainframe->enqueuemessage("user: ".print_r($user, true)."<br /><br />");
?>


user: JUser Object ( [id] => 0 [name] => [username] => [email] => [password] => [password_clear] => [usertype] => [block] => [sendEmail] => 0 [gid] => 0 [registerDate] => [lastvisitDate] => [activation] => [params] => [aid] => 0 [guest] => 1 [_params] => JParameter Object ( [_raw] => [_xml] => [_elements] => Array ( ) [_elementPath] => Array ( [0] => C:\xampp\htdocs\secureforms\libraries\joomla\html\parameter\element ) [_defaultNameSpace] => _default [_registry] => Array ( [_default] => Array ( [data] => stdClass Object ( ) ) ) [_errors] => Array ( ) ) [_errorMsg] => [_errors] => Array ( ) )



any ideas??
GreyHead 09 Feb, 2009
Hi samoht,

Well they are showing the Guest user object with user-id = 0

Is this showing up after the Joomal Registration? I would expect to see the new user info then.

Bob

PS The OnSubmit before block doesn't execute unless Send Emails is set to Yes in the General tab.
samoht 09 Feb, 2009

Is this showing up after the Joomal Registration?



do you mean - are the users actually getting registered?... yes they are getting registered.
Oh, I remembered about the OnSubmit before only working with the emails enabled, I just remembered it wrong 😀

Thanks,
GreyHead 09 Feb, 2009
Hi samoht,

Slightly more specific question than that. If you do a test registration and have this code show up immediately after the registration does it show the new users info instead of the guest user info? You may need to change the order in the form setup so that the OnSubmit After code runs *after* the Joomla Registration PlugIn.

Bob
samoht 09 Feb, 2009
Hi Bob,

I've attached the form just in case I'm missing something.

immediately after registration I get only the guest results that I showed before. I assume I have something out of order. I also noticed that I have no auto generated code?? is this because I am using the reg plugin?
GreyHead 09 Feb, 2009
Hi samoht,

You learn something everyday :-) I just discovered that the user info is in a variable called $cf_just_registered. This will show it to you:
<?php
global $cf_just_registered;
echo "cf_just_registered: ".print_r($cf_just_registered, true)."<br /><br />";
?>

Bob
samoht 09 Feb, 2009
wow, Thanks Bob!

so if I wanted the the username and password would I just:

$strLogin = $cf_just_registered['username']; 
$strPassword = $cf_just_registered['password_clear'];  


yes?
GreyHead 09 Feb, 2009
Hi samoht,

Yes that's it. Here's what I got from the object output (ignoring a parameter block):

JUser Object (
[id] => 67
[name] => Testing
[username] => testing4
[email] => [email]user+12@example.com[/email]
[password] => 3df24fbf92c0410763d2676603db85f3:6pUfa3dkjbs0bRtqGkbzOUFn1A8bIEZ4
[password_clear] => password
[usertype] =>
[block] => 1
[sendEmail] => 0
[gid] => 18
[registerDate] => 2009-02-09 17:47:18
[lastvisitDate] =>
[activation] => 092e46a98447af4b4706702377a60e9d
[params] =>
[aid] => 0
[guest] => 1


Bob
samoht 09 Feb, 2009
Thanks so much!

now I have the variables I need.
Just one quick question (dumb) question. How will I save to the db table from here?
GreyHead 09 Feb, 2009
Hi samoht,

Just write a little sql statement
<$php
$db =& JFactory::getDBO();
$query = "
    UPDATE `#__some_table`
        SET `name` = 'value'
        WHERE name = value;
$db->setQuery();
$db->query();
?>

Bob
samoht 09 Feb, 2009
Bob,

I am actually having some problems with this.
It complains that Fatal error: Cannot use object of type JUser as array in

when I try to echo out the values:
$strLogin = $cf_just_registered['username'];
$strPassword = $cf_just_registered['password_clear']; 

echo $strLogin;
echo $strPassword;


??
GreyHead 09 Feb, 2009
Hi samoht,

Ah must be an object. Try $cf_just_registered->username;

Bob
samoht 09 Feb, 2009
yes thanks!

but now it complains about
Warning: Missing argument 1 for JDatabase::setQuery(), called in C:\xampp\htdocs\secureforms\components\com_chronocontact\chronocontact.php(533) : eval()'d code on line 72 and defined in C:\xampp\htdocs\secureforms\libraries\joomla\database\database.php

here is the sql:
$db =& JFactory::getDBO();
$query = '
	INSERT INTO `#__vm_product_license` (user_id, license_start, license_type, license_duration, key_code)
		VALUES( "$smuser","$ourDate", "ADVANCED", "3", "$key" )';
$db->setQuery();
$db->query();
GreyHead 09 Feb, 2009
Hi samoht,

$db->setQuery($query);

Bob
samoht 10 Feb, 2009
Bob - thanks for all the help!

everything works like a charm.
spletcher 09 May, 2009
Greyhead,

I'm reading and re-reading this post since it is exactly what I'm trying to do.

I'm using either Joomla registration plugin or CB registration plugin to register folks and then I'm trying to get the new userid in order to use it to post information in other tables.

I've placed:
<?php
global $cf_just_registered;
echo "cf_just_registered: ".print_r($cf_just_registered, true)."<br /><br />";
?>
in the Onsubmit Code after email and I'm getting no output other than: cf_just_registered:

I've got the order of code being plugins 1, onsubmit 2

Can you see something else I have wrong?
Thanks.

Stan
Max_admin 10 May, 2009
Hi Stan,

The variables have changed, the new variables are in the last tab in the config page of the registration plugin!

Cheers
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
spletcher 10 May, 2009
Max,

thanks. I had tried all of these as well but am having no success.

Essentially I'm trying to pull the userid during the registration process and post some new entries into other tables.

(in JomSocial there are no way to control registration well, so I'm working on a work around)

I've tried to assign variables like this:

$userid = $MyPlugins->cf_joomla_registration['id'];
$complete = $MyPlugins->cf_joomla_registration['complete'];
$reg_username = $MyPlugins->cf_joomla_registration['username'];

I didn't know if I have a syntax issue or if it was the fact that the Joomla id isn't established until after the whole registration process is done. I'm placing this code in the Onsubmit area.

Stan
GreyHead 11 May, 2009
Hi Stan,

Try putting your code into the OnSubmit After box and then using the Plugin Order tab to set that to run after the Autogenerated Code. That way all the variables have been set.

Don't use the CB registration plugin for JomSocial, the Joomla Registration should work OK. The first three(?) steps of the JomSocial registration are pure Joomla, only the last one writes a few things to JS tables.

Bob
spletcher 11 May, 2009
Bob,

This is the code I tried (fashioned after your example)

<?php
global $cf_joomla_registration;
echo "Registered: ".print_r($cf_joomla_registration, true)."<br /><br />";
echo "Registration Info: ".print_r('$cf_joomla_registration->username', true)."<br /><br />";
?>

it is registering the user but, I'm getting this error on submitting that form:

Warning: implode() [function.implode]: Invalid arguments passed in /home/missione/public_html/libraries/joomla/database/database/mysql.php on line 538
spletcher 12 May, 2009
I've been experimenting with this to see what's going on. Seems that this error is coming because I changed the order of the autogenerated code to 2 and the Onsubmit to 3. When I change it back, I don't get the error, but the value of cf_joomla_registration->userid isn't set yet (I'm assuming)

however this doesn't make sense since the "plugin" is what is establishing the registration information I thought. I'm observing the autogenerated code only to be there when there is a database table which is connected by choosing one.

Overall I'm a bit confused - but here is a summary of what I'm trying to do:

1. Register the user
2. Grab the userid created on the fly to use to post some other profile fields from the registration that I'm posting to a jomsocial table.
3. Post the data to an outside database if possible using CURL.

Now this may sound crazy, but I'm actually hoping to use the community builder plugin to do this since I'd like to store the same profile fields in the cb tables (this part I'm having no problems with) rather than using the joomla registration plugin.

The reason that I'm utilizing both CB and Jomsocial simultaneously is that as I convert a larger database over completely to Jomsocial eventually I'm still using the functionality of CB in the backend. Jomsocial currently has no way to view user's profiles from the backend, (and of course Jomsocial has limited ability to register and send emails.)

My question is whether Chrono contact can handle doing all of these things in one form utilizing plugins and extra code. It sure seems like it's built to be a workhorse. Believe me, if we can build a registration model for folks to use with JomSocial and CB (either/or or both), we'll have a lot of happy joomla customers coming to Chrono_contact. There are a lot of frustrated users out there in Jomsocial forum that are trying to get this functionality.

If you think I should start a new thread, it might be a good idea, however this thread had the most in it about grabbing userid information during registration and how to post the information to another table at the same time.
Max_admin 12 May, 2009
Hi Stan,

The code you are trying to use is partially correct, you have the variables in the variables tab of the plugin, But to access the registered user data you need to use:


$MyPlugins->cf_joomla_registration['user']


so for example to get the username you need:

$username = $MyPlugins->cf_joomla_registration['user']->username;


I'm preparing a new ChronoProfiler component so that we can use it here on the site, no idea when will I'm going to finish it, I wanted it 2 weeks back but have been under some pressure and load!

Regards
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
spletcher 13 May, 2009
Max,

Thanks for your help so much!!

Here's my latest code in the onsubmit code box (seems like I've tried a zillion combinations) :

<?php
global $username, $MyPlugins, $cf_joomla_registration;
$username = $MyPlugins->cf_joomla_registration['user']->username;
$userid = $MyPlugins->cf_joomla_registration['user']->userid;
$_POST['VolunteerID'] = '123';
$_POST['userid'] = $userid;
$_POST['username'] = $username;
?>

My order is: Plugin, Onsubmit, Autogenerated. With this I'm getting the '123' to post in my connected table but the userid and username post as blanks.

If I change the order as Bob suggested with Autogenerated second, then I get an implode error message.

In this form, I'm using the joomla registration plugin and have this enabled. It does run and populates the jos_user table. Then I have another table connected to the form. I just seem to have problems grabbing the user information. I know that I can grab the username from the form, but the userid is what is generated on the fly and that I need.

Thanks,

Stan
Max_admin 14 May, 2009
Hi Stan,

to get the userid you need to use:
$userid = $MyPlugins->cf_joomla_registration['user']->id;
but not
$userid = $MyPlugins->cf_joomla_registration['user']->userid;


Regards
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
spletcher 14 May, 2009
Max,

Thanks for keeping trying with me.

I've tried this:

<?php
global $username, $MyPlugins, $cf_joomla_registration;
$username = $MyPlugins->cf_joomla_registration['user']->username;
$userid = $MyPlugins->cf_joomla_registration['user']->id;
$_POST['VolunteerID'] = '123';
$_POST['classification'] = $userid;
$_POST['username'] = $username;

?>

with no success. The '123' posts but neither the $userid or $username. The table is linked with the form.

I think Bob may be looking at it on Friday. I appreciate all the help.
Max_admin 15 May, 2009
Hi Stan,

#1- please remove this line:
global $username, $MyPlugins, $cf_joomla_registration;

#2- where do you put this code ? onSubmit after or before email ?

#3- do you have a field named "classification" at your table ?

#4- is DB connection enabled ?

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
spletcher 15 May, 2009
Max,

I removed that line of code.
DB connection is certainly enabled.
I have been trying primarily onsubmit after email although I just tried before email and both do populate the '123' field fine.
I do have a field named classification.

Stan
spletcher 18 May, 2009
Max,

I've reviewed the plugin code where the variable is set.

line 603: $MyPlugins->cf_cb_registration['user'] = $user;

We seem to have it right but for some reason I can't get a thing.

Do you have any form examples I could look at to see how others solved this?

Stan
Max_admin 18 May, 2009
Hi Stan,

hmm, are you using the Joomla registration plugin or the CB one ? you are referring to a CB plugin variable and we have been talking about the Joomla registration plugin one all the time!😉

this line:
$userid = $MyPlugins->cf_joomla_registration['user']->id;

should be
$userid = $MyPlugins->cf_cb_registration['user']->id;

if you are using the cb one!

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
spletcher 18 May, 2009
Max,

Sorry for the confusion, actually I've tried them both. I have forms with both plugins for registration that I'm trialing.

I'm going to want to use the cb one eventually. But I can't get either one to give me the userid as I register to use in another table.

Stan
Max_admin 19 May, 2009
Hi stan,

do you have this line at the top of the code ? its a vital one!

$MyPlugins =& CFPlugins::getInstance($MyForm->formrow->id);


regards
MAx
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
spletcher 19 May, 2009
Ta Da!!!

Success feels so grand!!

Thank you, thank you Max!

I have to say I'd never seen that line of code before, but I won't forget it now!!

Stan
Max_admin 20 May, 2009
Hi Stan,

Great, its the line to load an instance from the plugins of the form, I should not have missed it first! 🙂

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
This topic is locked and no more replies can be posted.