// RegisterX - snippet for MODx 0.9x // April 2006 // [email protected] // Version 1 // Adds and removes users from a given web user group // June 2006 // Version 1.1 - fixed the bug where an existing web user // would get signed up if he tries to sign out when he's not // already a member of the web group // options // &usergroup=`webgroup` - required! Form will not be displayed // without a valid web user group! // &sendadmin=`address` - optional, will send notification to supplied address // &senduser=`1|0` - optional, will send notification to user's address // &tpl=`chunkname` - optional, chunk to use as form template // text $nlxLabel = "Enter your e-mail address to receive our newsletter."; $nlxOutLabel = "Remove Me."; $nlxSubmit = "Submit"; $nlxMissingEmail = "Missing Email address!"; $nlxInvalidEmail = "Invalid Email address!"; $nlxInvalidAdminEmail = "Invalid Admin Email address."; $nlxAlreadyReceiving = "You are already receiving the newsletter."; $nlxRemoved = "You have been removed from the mailing list."; $nlxAdded = "Newsletter registration successful."; $nlxError = "Unable to register. Please try again later."; // the mailer function - edit the subject and the register/unregister messages if(!function_exists("sendMail")) { function sendMail($address,$about,$type,$action) { global $modx; $from = $modx->config['emailsender']; $fromname = $modx->config['site_name']; $to = $address; $subject = "Web Registration"; if($type == "admin") { $message = ($action == "in") ? "The email address $about has registered" : "The email address $about has been removed."; } else { $message = ($action == "in") ? 'This email address, '.$address.', has been registered for a newsletter. Go <a href="http://www.gamesfaction.com"> here </a>to be removed from the mailing list.' : "This email address, $address, has been removed from registration"; } include_once "manager/includes/controls/class.phpmailer.php"; $mail = new PHPMailer(); $mail->IsMail(); $mail->IsHTML(true); $mail->From = $from; $mail->FromName = $fromname; $mail->Subject = $subject; $mail->Body = $message; $mail->AddAddress($to); if(!$mail->send()) return $mail->ErrorInfo; } // end mail function } // end function_exists check // the registration form if($tpl) { $nlxForm = ($chunk=$modx->getChunk($tpl)) ? $chunk:"Chunk '$tpl' not found."; } else { $nlxForm = isset($tpl) ? $modx->getChunk($tpl) : ' <form id="nlxForm" method="post" action="[~[*id*]~]"> <fieldset id="nlxFields"> <label for="nlxEmail">'.$nlxLabel.'<br /> <input type="text" name="nlxEmail" id="nlxEmail" class="nlxText" /></label><br /> <label for="nlxOut"> <input type="radio" name="nlxOut" id="nlxOut" class="nlxRadio" />' .$nlxOutLabel.'</label> <input type="submit" name="nlxSubmit" id="nlxSubmit" class="nlxButton" value="'.$nlxSubmit.'" /> </fieldset> </form> '; } // check for valid web user group if(!isset($usergroup) || $usergroup == '') { return; } // quietly do nothing if usergroup is not set $result = $modx->db->select('id',$modx->getFullTableName('webgroup_names'),"name = '".$usergroup."'"); if($modx->db->getRecordCount($result) != 1) { return; } // quietly do nothing if usergroup does not exist // validate snippet mailing arguments if(isset($sendadmin)) { if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $sendadmin)) { return $nlxInvalidAdminEmail; } } $senduser = isset($senduser) ? $senduser : 0; if(isset($_POST['nlxSubmit'])) { // process registration // first, just check for empty field if(!isset($_POST['nlxEmail']) || $_POST['nlxEmail'] == '') { $error = '<span class="error">'.$nlxMissingEmail.'</span><br />'; return $error . $nlxForm; } // now check for invalid email if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['nlxEmail'])) { $error = '<span class="error">'.$nlxInvalidEmail.'</span><br />'; return $error . $nlxForm; } // email address is present and valid so process registration/removal $email = mysql_real_escape_string($_POST['nlxEmail']); // check for existing user $result = $modx->db->select('internalKey',$modx->getFullTableName('web_user_attributes')," email = '".$email."'"); if($modx->db->getRecordCount($result) == 1) { // check group to see if this user is already a member $uid = $modx->db->getValue($result); $result = $modx->db->select('id',$modx->getFullTableName('webgroup_names'),"name = '".$usergroup."'"); if($modx->db->getRecordCount($result) == 1) { $gid = $modx->db->getValue($result); $result = $modx->db->select('id',$modx->getFullTableName('web_groups'),"webgroup = $gid AND webuser = $uid"); if($modx->db->getRecordCount($result) != 0) { if($_POST['nlxOut'] == 'on') { // opt out if(isset($sendadmin)) { sendMail($sendadmin,$email,"admin","out"); } if($senduser != 0) { sendMail($email,$email,"user","out"); } $modx->db->delete($modx->getFullTableName('web_groups'),"webgroup = $gid AND webuser = $uid"); return '<span class="important">'.$nlxRemoved.'</span>'; } return '<span class="important">'.$nlxAlreadyReceiving.'</span>'; } else { // trying to remove non-registered account? if($_POST['nlxOut'] == 'on') { return $nlxForm; } // add the user to the group $sql = "INSERT INTO ".$modx->getFullTableName('web_groups')." (webgroup, webuser) VALUES (".$gid.",".$uid.")"; $result = $modx->db->query($sql); if($result) { if(isset($sendadmin)) { sendMail($sendadmin,$email,"admin","in"); } if($senduser != 0) { sendMail($email,$email,"user","in"); } return '<span class="important">'.$nlxAdded.'</span>'; } else { return '<span class="error">'.$nlxError.'</span><br />' . $nlxForm; } } } } // trying to remove non-registered account? if($_POST['nlxOut'] == 'on') { return $nlxForm; } // add user $pass = md5(time()); $sql = "INSERT INTO ".$modx->getFullTableName('web_users')." (username,password) VALUES ('".$email."', '".$pass."')"; $rs = $modx->db->query($sql); if(!$rs) { return '<span class="error">'.$nlxError.'</span><br />' . $nlxForm; } else { $uid = $modx->db->getInsertId(); // add data to web_user_attributes $sql = "INSERT INTO ".$modx->getFullTableName('web_user_attributes')." (internalKey,email) VALUES (".$uid.",'".$email."')"; $rs = $modx->db->query($sql); if(!$rs) { $modx->db->delete($modx->getFullTableName('web_users'),"id = ".$uid); return '<span class="error">'.$nlxError.'</span><br />' . $nlxForm; } else { // add user to the group $result = $modx->db->select('id',$modx->getFullTableName('webgroup_names'),"name = '".$usergroup."'"); if($modx->db->getRecordCount($result) == 1) { $gid = $modx->db->getValue($result); } $sql = "INSERT INTO ".$modx->getFullTableName('web_groups')." (webgroup, webuser) VALUES (".$gid.",".$uid.")"; $result = $modx->db->query($sql); if($result) { if(isset($sendadmin)) { sendMail($sendadmin,$email,"admin","in"); } if($senduser != 0) { sendMail($email,$email,"user","in"); } return '<span class="important">'.$nlxAdded.'</span>'; } else { return '<span class="error">'.$nlxError.'</span><br />' . $nlxForm; } } } } else { // display registration form return $nlxForm; }