]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
confirm email addresses
authorEvan Prodromou <evan@controlyourself.ca>
Fri, 20 Jun 2008 05:15:36 +0000 (01:15 -0400)
committerEvan Prodromou <evan@controlyourself.ca>
Fri, 20 Jun 2008 05:15:36 +0000 (01:15 -0400)
darcs-hash:20080620051536-5ed1f-231e427832dd20c861eb7a6dc1171315e90f455b.gz

actions/confirmemail.php [new file with mode: 0644]
actions/register.php
classes/Confirm_email.php [new file with mode: 0644]
classes/stoica.ini
db/laconica.sql
lib/action.php
lib/common.php
lib/mail.php

diff --git a/actions/confirmemail.php b/actions/confirmemail.php
new file mode 100644 (file)
index 0000000..82e3a55
--- /dev/null
@@ -0,0 +1,70 @@
+<?php
+/*
+ * Laconica - a distributed open-source microblogging tool
+ * Copyright (C) 2008, Controlez-Vous, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+if (!defined('LACONICA')) { exit(1); }
+
+class ConfirmemailAction extends Action {
+
+       function handle($args) {
+               parent::handle($args);
+               if (!common_logged_in()) {
+                       common_set_returnto($this->self_url());
+                       common_redirect(common_local_url('login'));
+                       return;
+               }
+               $code = $this->trimmed('code');
+               if (!$code) {
+                       $this->client_error(_t('No confirmation code.'));
+                       return;
+               }
+               $confirm_email = Confirm_email::staticGet('code', $code);
+               if (!$confirm_email) {
+                       $this->client_error(_t('Confirmation code not found.'));
+                       return;
+               }
+               $cur = common_current_user();
+               if ($cur->id != $confirm_email->user_id) {
+                       $this->client_error(_t('That confirmation code is not for you!'));
+                       return;
+               }
+               if ($cur->email == $confirm_email->email) {
+                       $this->client_error(_t('That email address is already confirmed.'));
+                       return;
+               }
+               $cur->query('BEGIN');
+               $orig_user = clone($cur);
+               $cur->email = $confirm_email->email;
+               $result = $cur->update($orig_user);
+               if (!$result) {
+                       $this->server_error(_t('Error setting email address.'));
+                       return;
+               }
+               $result = $confirm_email->delete();
+               if (!$result) {
+                       $this->server_error(_t('Error deleting code.'));
+                       return;
+               }
+               $cur->query('COMMIT');
+               common_show_header(_t('Confirm E-mail Address'));
+               common_element('p', NULL,
+                                          _t('The email address "') . $cur->email . 
+                                          _t('" has been confirmed for your account.'));
+               common_show_footer(_t('Confirm E-mail Address'));
+       }
+}
index cad5c2ed708745df0f4daa2c65659fc73966e773..d9315b4244b851bb36f6628359a6cad7a0b84c44 100644 (file)
@@ -89,8 +89,11 @@ class RegisterAction extends Action {
        }
 
        function register_user($nickname, $password, $email) {
-               # TODO: wrap this in a transaction!
+               
                $profile = new Profile();
+               
+               $profile->query('BEGIN');
+               
                $profile->nickname = $nickname;
                $profile->profileurl = common_profile_url($nickname);
                $profile->created = DB_DataObject_Cast::dateTime(); # current time
@@ -103,15 +106,34 @@ class RegisterAction extends Action {
                $user->id = $id;
                $user->nickname = $nickname;
                $user->password = common_munge_password($password, $id);
-               $user->email = $email;
                $user->created =  DB_DataObject_Cast::dateTime(); # current time
                $user->uri = common_mint_tag('user:'.$id);
                
                $result = $user->insert();
                if (!$result) {
-                       # Try to clean up...
-                       $profile->delete();
+                       return FALSE;
                }
+
+               if ($email) {
+                       $confirm = new Confirm_email();
+                       $confirm->code = common_good_random(16);
+                       $confirm->user_id = $user->id;
+                       $confirm->email = $email;
+                       
+                       $result = $confirm->insert();
+                       if (!$result) {
+                               return FALSE;
+                       }
+               }
+               
+               $profile->query('COMMIT');
+
+               if ($email) {
+                       mail_confirm_address($code,
+                                                                $profile->nickname,
+                                                                $email);
+               }
+               
                return $result;
        }
 
diff --git a/classes/Confirm_email.php b/classes/Confirm_email.php
new file mode 100644 (file)
index 0000000..0b13a46
--- /dev/null
@@ -0,0 +1,23 @@
+<?php
+/**
+ * Table Definition for confirm_email
+ */
+require_once 'DB/DataObject.php';
+
+class Confirm_email extends DB_DataObject 
+{
+    ###START_AUTOCODE
+    /* the code below is auto generated do not remove the above tag */
+
+    public $__table = 'confirm_email';                   // table name
+    public $code;                            // varchar(32)  primary_key not_null
+    public $user_id;                         // int(4)   not_null
+    public $email;                           // varchar(255)   not_null
+    public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
+
+    /* Static get */
+    function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Confirm_email',$k,$v); }
+
+    /* the code above is auto generated do not remove the tag below */
+    ###END_AUTOCODE
+}
index ad0925bd92f31d0e6787c2213d15717c1331c9d6..3745697d52303b8883991a6fe485fc5b0cae8455 100644 (file)
@@ -16,6 +16,15 @@ width = K
 height = K
 url = U
 
+[confirm_email]
+code = 130
+user_id = 129
+email = 130
+modified = 384
+
+[confirm_email__keys]
+code = K
+
 [consumer]
 consumer_key = 130
 seed = 130
index 7935b8b071c5311e546e0788cc6a186b4b278a7f..0b07148c7ed3c0fb50d3f28a7e91b6aafd440d23 100644 (file)
@@ -145,7 +145,7 @@ create table oid_nonces (
     UNIQUE (server_url(255), timestamp, salt)
 ) ENGINE=InnoDB;
 
-create table confirmemail (
+create table confirm_email (
     code varchar(32) not null primary key comment 'good random code',
     user_id integer not null comment 'user who requested confirmation' references user (id),
     email varchar(255) not null comment 'email address for password recovery etc.',
index 06d3901d1439e6e9b90acca2710df6ca3e179f59..81b2283124575c15cbb61ceee0c54d1a031e56ff 100644 (file)
@@ -68,4 +68,11 @@ class Action { // lawsuit
                common_debug("User error '$code' on '$action': $msg", __FILE__);
                common_user_error($msg, $code);
        }
+       
+       function self_url() {
+               $action = $this->trimmed('action');
+               $args = $this->args;
+               unset($args['action']);
+               return common_local_url($action, $args);
+       }
 }
index 7435b0f4ab5369887e0341b81d5f49d5ba9bbf3e..00f6d68a7f25b19de5e3726f5c664a6926149e0c 100644 (file)
@@ -80,3 +80,4 @@ require_once(INSTALLDIR.'/classes/Profile.php');
 require_once(INSTALLDIR.'/classes/Remote_profile.php');
 require_once(INSTALLDIR.'/classes/Subscription.php');
 require_once(INSTALLDIR.'/classes/User.php');
+require_once(INSTALLDIR.'/classes/Confirm_email.php');
index 25253fd8161b043ecd6651d0d869e3067a5582e7..23fd24b25837405e91cac4e2bdb3258acd77640c 100644 (file)
@@ -54,4 +54,30 @@ function mail_notify_from() {
                return $config['site']['name'] . ' <noreply@'.$config['site']['server'].'>';
        }
 }
\ No newline at end of file
+
+# For confirming an email address
+
+function mail_confirm_address($code, $nickname, $address) {
+       $recipients = $address;
+       $headers['From'] = mail_notify_from();
+       $headers['To'] = $nickname . ' <' . $address . '>';
+       $headers['Subject'] = _t('Email address confirmation');
+
+       $body = "Hey, $nickname.";
+       $body .= "\n\n";
+       $body .= 'Someone just entered this email address on ' . common_config('site', 'name') . '.';
+       $body .= "\n\n";
+       $body .= 'If it was you, and you want to confirm your entry, use the URL below:';
+       $body .= "\n\n";
+       $body .= "\t".common_local_url('confirmemail',
+                                                                  array('code' => $code));
+       $body .= "\n\n";
+       $body .= 'If not, just ignore this message.';
+       $body .= "\n\n";
+       $body .= 'Thanks for your time, ';
+       $body .= "\n";
+       $body .= common_config('site', 'name');
+       $body .= "\n";
+       
+       mail_send($recipients, $headers, $body);
+}