The1LT
Member
So here is my first very dirty hack, sure there will be many more to come and at some point I can gain the honour of actually calling it a proper mod.. lol
So what I've done is modded the GFNProduct manager so that rather than generating random license keys it will instead fetch a key from a table of pre-existing keys which I'm batch generating from an external licencing application.
One small concern I have is regards the way that threads work on the server side.
So what happens in this scenario?
Lets say User A clicks buy and User B click buy like a millisecond behind him. If User A thread populates newLicense with the fetchrow statement, is there any chance that a thread can come in behind it from User B and grab the same license key before the first thread has updated the table record to being used status (Used = 1) ? Do I need to implement some form of thread or row locking on this?
Code:
public function createLicense($userId, $productId)
{
$db = XenForo_Application::get('db');
//Ross mod - START - 22/10/2013
//$newLicense = GFNCoders_ProductManager_Helper::generateLicenseKey();
//while($db->fetchOne("SELECT COUNT(*) AS count FROM gfnpm_license WHERE license_key='$newLicense'"))
//{
// $newLicense = GFNCoders_ProductManager_Helper::generateLicenseKey();
//}
//get licence from table instead of randomly generating from helper
$used = 0;
$row = $this->_getDb()->fetchRow("SELECT * FROM `gfnpm_license_import` WHERE Used = ? ORDER BY LicenceID", $used);
$newLicense = $row['LicenceKey'];
$newLicenseID = $row['LicenceID'];
//update fetched license
$db->query("UPDATE gfnpm_license_import SET Used = 1 WHERE LicenceID = '$newLicenseID'");
//Ross mod - END - 22/10/2013
$db->query("INSERT INTO gfnpm_license (license_key, product_id, user_id, domain, olddomains, active) VALUES (?, ?, ?, '', '', 1)", array($newLicense, $productId, $userId));
$recentlyAdded = $this->_getDb()->fetchRow("SELECT * FROM `gfnpm_license` ORDER BY license_id DESC");
return $recentlyAdded['license_id'];
}
So what I've done is modded the GFNProduct manager so that rather than generating random license keys it will instead fetch a key from a table of pre-existing keys which I'm batch generating from an external licencing application.
One small concern I have is regards the way that threads work on the server side.
So what happens in this scenario?
Lets say User A clicks buy and User B click buy like a millisecond behind him. If User A thread populates newLicense with the fetchrow statement, is there any chance that a thread can come in behind it from User B and grab the same license key before the first thread has updated the table record to being used status (Used = 1) ? Do I need to implement some form of thread or row locking on this?