/** * Checks whether this user is allowed to send these emails or not. * * @param object $user_object User Object to check. * @param int $queuesize The size of the queue to check. * @param int $queuetime The time when you are trying to send / schedule the queue. * * @return array Returns an array of status and a language variable describing why it can't be sent. This allows us to differentiate between whether it's a "maxemails" issue or a "per month" issue. */ public function CheckUserStats(User_API $user, $queueSize = 0, $queuetime=0) { // if they have no limits, then no need to do any other checks if ($user->hasUnlimitedCredit()) {return array(true, false);} $queueSize = (int) $queueSize; if (!$user->hasUnlimitedMonthlyCredit()){ $monthly = (int) API_USERS::creditAvailableThisMonth($user->userid, false, $queuetime); } if (!$user->hasUnlimitedTotalCredit()){ $total = (int) API_USERS::creditAvailableFixed($user->userid); } // do monthly credit check if (isset($monthly) && $queueSize > $monthly){return array(false, 'OverLimit_PerMonth');} // do total credit check if (isset($total) && $queueSize > $total) {return array(false, 'OverLimit_MaxEmails');} return array(true, false); }
/** * Record credit usage * This function will record credit usage for a particular user. * * @param record_Users|integer $user User record object or user ID * @param string $usagetype Usage type (see class constansts CREDIT_USAGETYPE_* for valid types) * @param integer $creditused The number of credits that are being used up * @param integer $jobid Associate job ID (OPTIONAL, default = 0) * @param integer $statid Associate statistic ID (OPTIONAL, default = 0) * @param integer $time Time of which the credit is being used (OPTIONAL, default = now) * * @return boolean Returns TRUE if successful, FALSE otherwise */ public static function creditUse($user, $usagetype, $creditused, $jobid = 0, $statid = 0, $time = 0, $evaluateWarnings = true) { $userid = 0; $usagetype = strtolower($usagetype); $creditused = intval($creditused); $jobid = intval($jobid); $statid = intval($statid); $time = intval($time); $db = IEM::getDatabase(); static $validTypes = null; if (is_null($validTypes)) { $validTypes = array(self::CREDIT_USAGETYPE_SENDAUTORESPONDER, self::CREDIT_USAGETYPE_SENDCAMPAIGN, self::CREDIT_USAGETYPE_SENDTRIGGER); } if (!$user instanceof record_Users) { $userid = intval($user); $user = API_USERS::getRecordByID($userid); } if (!$user) { trigger_error("API_USERS::creditUse -- Invalid user specified.", E_USER_NOTICE); return false; } if (!in_array($usagetype, $validTypes)) { trigger_error("API_USERS::creditUse -- Invalid credit type '{$usagetype}'.", E_USER_NOTICE); return false; } if ($creditused < 1) { trigger_error("API_USERS::creditUse -- Credit cannot be less than 1.", E_USER_NOTICE); return false; } if ($jobid < 0) { trigger_error("API_USERS::creditUse -- Invalid jobid specified.", E_USER_NOTICE); return false; } if ($statid < 0) { trigger_error("API_USERS::creditUse -- Invalid statid specified.", E_USER_NOTICE); return false; } if ($time < 0) { trigger_error("API_USERS::creditUse -- Time cannot be negative.", E_USER_NOTICE); return false; } // If user has unlimited emails credit, we don't need to record this $usersApi = new User_API($user->userid); if ($usersApi->hasUnlimitedCredit()) { return true; } // Check for cases (based on usage type) where credit does not need to be deducted switch ($usagetype) { case self::CREDIT_USAGETYPE_SENDTRIGGER: if (!SENDSTUDIO_CREDIT_INCLUDE_TRIGGERS) { return true; } break; case self::CREDIT_USAGETYPE_SENDAUTORESPONDER: if (!SENDSTUDIO_CREDIT_INCLUDE_AUTORESPONDERS) { return true; } break; } $time = $time == 0 ? time() : $time; $db->StartTransaction(); $tempStatus = $db->Query("\n\t\t\t\tINSERT INTO [|PREFIX|]user_credit (userid, transactiontype, transactiontime, credit, jobid, statid)\n\t\t\t\tVALUES ({$userid}, '{$usagetype}', {$time}, -{$creditused}, {$jobid}, {$statid})\n\t\t\t"); if (!$tempStatus) { $db->RollbackTransaction(); trigger_error("API_USERS::creditUse -- Unable to insert credit usage into database: " . $db->Error(), E_USER_NOTICE); return false; } /**@TODO REMOVE ALL REFERENCES TO OLD CREDIT SYSTEM /* // Record this in the credit summary table $tempTimeperiod = mktime(0, 0, 0, date('n'), 1, date('Y')); $tempQuery; // Since MySQL have a direct query which will insert/update in one go, we can utilzie this. if (SENDSTUDIO_DATABASE_TYPE == 'mysql') { $tempQuery = " INSERT INTO [|PREFIX|]user_credit_summary (userid, startperiod, credit_used) VALUES ({$userid}, {$tempTimeperiod}, {$creditused}) ON DUPLICATE KEY UPDATE credit_used = credit_used + {$creditused} "; // Do we need to do an INSERT or an UPDATE query ?? } else { $tempRS = $db->Query("SELECT usagesummaryid FROM [|PREFIX|]user_credit_summary WHERE userid = {$userid} AND startperiod = {$tempTimeperiod}"); if (!$tempRS) { $db->RollbackTransaction(); trigger_error("API_USERS::creditUse -- Cannot query user_credit_summary table: " . $db->Error(), E_USER_NOTICE); return false; } if ($db->CountResult($tempRS) == 0) { $tempQuery = " INSERT INTO [|PREFIX|]user_credit_summary (userid, startperiod, credit_used) VALUES ({$userid}, {$tempTimeperiod}, {$creditused}) "; } else { $tempSummaryID = $db->FetchOne($tempRS, 'usagesummaryid'); $tempQuery = " UPDATE [|PREFIX|]user_credit_summary SET credit_used = credit_used + {$creditused} WHERE usagesummaryid = {$tempSummaryID} "; } $db->FreeResult($tempRS); } $tempStatus = $db->Query($tempQuery); if (!$tempStatus) { $db->RollbackTransaction(); trigger_error("API_USERS::creditUse -- Unable to update/insert user_credit_summary table: " . $db->Error(), E_USER_NOTICE); return false; }*/ $db->CommitTransaction(); if ($evaluateWarnings) { return self::creditEvaluateWarnings($userid); } else { return true; } }