db = $db; $this->keygenerator_url = "http://localhost:4545"; $this->activation_limit = 3; $this->error = null; } function SetError($code, $message) { $this->error["code"] = $code; $this->error["message"] = $message; } function ActivateEx($clientproductID, $hardwareID, $instanceID, $method = 'web', $override = false, $notes = false) { if (empty($clientproductID) || empty($hardwareID) || empty($instanceID)) { $this->SetError(600, "Invalid parameters."); return false; } $this->db->AddInt($clientproductID); $this->db->open("SELECT regkey, date_expire FROM client_product WHERE ID = :clientproductID"); if ($this->db->num_rows() <= 0) { $this->SetError(613, "Unable to locate product using clientproductID"); return false; } $productdetails = $this->db->row(); $origkey = str_replace(" - ", "", $productdetails["regkey"]); //Take out any seperators //Step3: Verify that we can allow this activation if (!$override) { $activations = array(); $this->db->AddInt($clientproductID); $this->db->AddStr($hardwareID); $this->db->AddStr($instanceID); $this->db->open(" SELECT DISTINCT hardwareID, instanceID FROM client_product_activation WHERE (clientproductID = :productID) AND (counter>0) AND ((hardwareID<>:hardwareID) OR (instanceID<>:instanceID)) AND (DATE_SUB(NOW(),INTERVAL 180 DAY) < date_created)"); $activations_cnt = $this->db->num_rows(); //Check acticavion limit // if($activations_cnt>=$this->activation_limit) // { // $this->SetError(602,"Activation limit exceeded. Please contact support to manually activate your product."); // return false; // } } //Step4: Generate unlock activation code $unique_key = "$hardwareID-$instanceID"; //Use HardwareID combined with InstanceID to uniquely identify this activation $url = $this->keygenerator_url . "/?rk=$origkey&hk=$unique_key"; $activationcode = @File2Str($url); //Grab key form remote generator if (empty($activationcode) || strtolower($activationcode == "invalid") || (strpos($activationcode, "-") <= 0)) { $this->SetError(603, "Error generating activation code."); return false; } //Store the details about this activation $this->AddEntry($clientproductID, "activate", $method, $hardwareID, $instanceID, $activationcode, $notes); //Finally return generated activation code return $activationcode; } function Activate($registrationkey_md5, $hardwareID, $instanceID, $method = 'web', $override = false) { if (empty($registrationkey_md5) || empty($hardwareID) || empty($instanceID)) { $this->SetError(600, "Invalid parameters."); return false; } $sql = "SELECT ID FROM client_product WHERE MD5(REPLACE(regkey, ' - ', '')) = :key"; $this->db->AddStr(strtolower($registrationkey_md5)); $this->db->open($sql); if ($this->db->num_rows() <= 0) { $this->SetError(601, "Invalid registration key."); return false; } $productdetails = $this->db->row(); $activationcode = $this->ActivateEx($productdetails["ID"], $hardwareID, $instanceID, $method, $override); //Finally return generated activation code return $activationcode; } function DeActivate($hardwareID, $instanceID, $activationcode_MD5, $method = 'web', $notes = '') { if (empty($activationcode_MD5) || empty($hardwareID) || empty($instanceID)) { $this->SetError(600, "Invalid parameters."); return false; } $sql = "SELECT clientproductID, activationcode FROM client_product_activation WHERE (hardwareID = :hardwareID) AND (instanceID = :instanceID) AND (MD5(activationcode) = :md5ac)"; $this->db->AddStr($hardwareID); $this->db->AddStr($instanceID); $this->db->AddStr(strtolower($activationcode_MD5)); $this->db->open($sql); if ($this->db->num_rows() <= 0) { $this->SetError(611, "No previous activation found for this installation."); return false; } $productdetails = $this->db->row(); //Store the details about this activation $this->AddEntry($productdetails["clientproductID"], "deactivate", $method, $hardwareID, $instanceID, $productdetails["activationcode"], $notes); if($productdetails["activationcode"]) { return $productdetails["activationcode"]; } else { return false; } } function AddEntry($clientproductID, $action, $method, $hardwareID, $instanceID, $activationcode, $notes = '') { $data = array(); $data["clientproductID"] = $clientproductID; $data["date_created"] = 'NOW()'; $data["action"] = $action; $data["method"] = $method; $data["hardwareID"] = $hardwareID; $data["instanceID"] = $instanceID; $data["activationcode"] = $activationcode; $data["notes"] = $notes; if ($data["action"] == 'activate') { $data["counter"] = 1; } else if ($data["action"] == 'deactivate') { $data["counter"] = 0; $activationcode = ''; } $this->db->insert("client_product_activation", $data); //Update all other matching hardware entries to reflect the latest counter values $this->db->AddInt($data["counter"]); $this->db->AddInt($data["clientproductID"]); $this->db->AddStr($data["hardwareID"]); $this->db->AddStr($data["instanceID"]); $this->db->open("UPDATE client_product_activation SET counter = :counter WHERE (clientproductID = :productID) AND (hardwareID = :hardwareID) AND (instanceID = :instanceID)"); } } ?>