Jump to content

Custom Hook Code


ianj844

Recommended Posts

Hi,

 

I am working on a system for my client. I have created a hook code that creates a random key and inserts it into MYSQL. The code is working except it is creating 4 codes and inserting them every time it runs. What could be the cause of this?

 

Thanks,

Ian

Link to comment
Share on other sites

<?php

if (!defined("WHMCS"))

die("This file cannot be accessed directly");

 

/* Load the Database */

$db_host = "localhost";

$db_username = "REMOVED";

$db_password = "REMOVED";

$db_database = "REMOVED";

 

$connect = mysql_connect($db_host,$db_username,$db_password);

$database = mysql_select_db($db_database,$connect);

 

 

/* Functions */

/*Key Generator*/

function KeyGen(){

$key = md5(microtime());

$new_key = 'SMVA-';

for($i=1; $i <= 12; $i ++ ){

$new_key .= $key[$i];

if ( $i%4==0 && $i != 12) $new_key.='-';

}

return strtoupper($new_key);

}

/*Assign Key And Email */

function hook_send_key_email()

{

 

$line = KeyGen();

$link = mysql_query("INSERT INTO api_keys (key_code) VALUES ('$line')") or die(mysql_error());

$merge_fields['pull_key'] = $line;

return $merge_fields;

}

 

add_hook("EmailPreSend", 1,"hook_send_key_email");

?>

Link to comment
Share on other sites

First of all why are you establishing a connection to database? You're already connected. Secondly why attaching this task to EmailPreSend hook? This hook point runs every single time WHMCS sends an email. I don't know what is the purpose of KeyGen() function but I find pretty impossible that this is the right hook point for you. What's the point?

Link to comment
Share on other sites

I don't that's the problem we have one product that requires a key to use its an instant issue key the keygen generates the key then we need to email it to the customer

as it generates the "key" when sending email, you may need to modify it so your key will be generated when the specified email template used, well to be clear you need to do this for example:

/*Assign Key And Email */
function hook_send_key_email($vars)
{

if ($vars['messagename']=="Generated Key Template Or Something Else"){

   $line = KeyGen();
   $link = mysql_query("INSERT INTO api_keys (key_code) VALUES ('$line')") or die(mysql_error());
   $merge_fields['pull_key'] = $line;
   return $merge_fields;

}
}

 

this way key is generated once and when needed

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated