public function send_already_signedup_email($details) { $data = array('to' => $details['email'], 'template' => 'alert_already_signedup'); $criteria = alert_details_to_criteria($details); $this->criteria = $criteria; $merge = array('FIRSTNAME' => 'THEY WORK FOR YOU', 'LASTNAME' => ' ALERT ALREADY SIGNED UP', 'CRITERIA' => $this->criteria_pretty()); $success = send_template_email($data, $merge); if ($success) { return true; } else { return false; } }
function add($details, $confirmation_email = false, $instantly_confirm = true) { // Adds a new alert's info into the database. // Then calls another function to send them a confirmation email. // $details is an associative array of all the alert's details, of the form: // array ( // "email" => "*****@*****.**", // "criteria" => "speaker:521", // etc... using the same keys as the object variable names. // ) // The BOOL variables confirmed and deleted will be true or false and will need to be // converted to 1/0 for MySQL. global $REMOTE_ADDR; $alerttime = gmdate("YmdHis"); $criteria = alert_details_to_criteria($details); $q = $this->db->query("SELECT * FROM alerts WHERE email='" . mysql_escape_string($details['email']) . "' AND criteria='" . mysql_escape_string($criteria) . "' AND confirmed=1"); if ($q->rows() > 0) { $deleted = $q->field(0, 'deleted'); if ($deleted) { $this->db->query("UPDATE alerts SET deleted=0 WHERE email='" . mysql_escape_string($details['email']) . "' AND criteria='" . mysql_escape_string($criteria) . "' AND confirmed=1"); return 1; } else { return -2; } } $q = $this->db->query("INSERT INTO alerts (\n\t\t\t\temail, criteria, deleted, confirmed, created\n\t\t\t) VALUES (\n\t\t\t\t'" . mysql_escape_string($details["email"]) . "',\n\t\t\t\t'" . mysql_escape_string($criteria) . "',\n\t\t\t\t'0', '0', NOW()\n\t\t\t)\n\t\t"); if ($q->success()) { // Get the alert id so that we can perform the updates for confirmation $this->alert_id = $q->insert_id(); $this->criteria = $criteria; // We have to set the alert's registration token. // This will be sent to them via email, so we can confirm they exist. // The token will be the first 16 characters of a crypt. // This gives a code for their email address which is then joined // to the timestamp so as to provide a unique ID for each alert. $token = substr(crypt($details["email"] . microtime()), 12, 16); // Full stops don't work well at the end of URLs in emails, // so replace them. We won't be doing anything clever with the crypt // stuff, just need to match this token. $this->registrationtoken = strtr($token, '.', 'X'); // Add that to the database. $r = $this->db->query("UPDATE alerts\n\t\t\t\t\t\tSET registrationtoken = '" . mysql_escape_string($this->registrationtoken) . "'\n\t\t\t\t\t\tWHERE alert_id = '" . mysql_escape_string($this->alert_id) . "'\n\t\t\t\t\t\t"); if ($r->success()) { // Updated DB OK. if ($confirmation_email) { // Right, send the email... $success = $this->send_confirmation_email($details); if ($success) { // Email sent OK return 1; } else { // Couldn't send the email. return -1; } } elseif ($instantly_confirm) { // No confirmation email needed. $s = $this->db->query("UPDATE alerts\n\t\t\t\t\t\tSET confirmed = '1'\n\t\t\t\t\t\tWHERE alert_id = '" . mysql_escape_string($this->alert_id) . "'\n\t\t\t\t\t\t"); return 1; } } else { // Couldn't add the registration token to the DB. return -1; } } else { // Couldn't add the user's data to the DB. return -1; } }