myaccount.ajax 8.26 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2000-2017 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file 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 file 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 file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#
chdir("..");
include_once("webtask.php");
chdir("apt");

# We set this in CheckPageArgs
$target_user = null;

#
# Need to check the permission, since we allow admins to edit
# other accounts.
#
function CheckPageArgs()
{
    global $this_user, $target_user;
    global $ajax_args;
    global $APTMAIL;

    if (!isset($ajax_args["formfields"])) {
	SPITAJAX_ERROR(-1, "Missing formfields");
	return -1;
    }
    $formfields = $ajax_args["formfields"];

    if (!isset($formfields["uid"])) {
	SPITAJAX_ERROR(-1, "Missing target uid");
	return -1;
    }
    $uid = $formfields["uid"];
    
    if (!TBvalid_uid($uid)) {
	SPITAJAX_ERROR(-1, "Invalid target uid");
        return -1;
    }
    $target_user = User::LookupByUid($uid);
    if (!$target_user) {
	SPITAJAX_ERROR(-1, "Unknown target uid");
        return -1;
    }
    if ($uid == $this_user->uid())
        return 0;
    
    if (!ISADMIN()) {
66
	SPITAJAX_ERROR(-1, "Not enough permission");
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
	return -1;
    }
    return 0;
}

#
# Check form arguments on the fist step, so we can halt progress
# right away.
#
function CheckForm()
{
    global $this_user, $target_user;
    global $ajax_args;
    global $APTMAIL;
    
    $formfields = $ajax_args["formfields"];
    $errors = array();

    if (!isset($formfields["name"]) ||
        strcmp($formfields["name"], "") == 0) {
        $errors["name"] = "Missing Field";
    }
    elseif (! TBvalid_usrname($formfields["name"])) {
        $errors["name"] = TBFieldErrorString();
    }
    # Make sure user name has at least two tokens
    $tokens = preg_split("/[\s]+/", $formfields["name"],
			 -1, PREG_SPLIT_NO_EMPTY);
    if (count($tokens) < 2) {
        $errors["name"] = "Please provide a first and last name";
    }
    if (!isset($formfields["email"]) ||
        strcmp($formfields["email"], "") == 0) {
        $errors["email"] = "Missing Field";
    }
    elseif (! TBvalid_email($formfields["email"])) {
        $errors["email"] = TBFieldErrorString();
    }
    else {
        $tmp = User::LookupByEmail($formfields["email"]);
        #
        # Treat this error separate. Not allowed.
        #
        if ($tmp && $tmp->uid() != $target_user->uid()) {
            $errors["email"] = "Already in use by another user";
        }
    }
    if (!isset($formfields["affiliation"]) ||
        strcmp($formfields["affiliation"], "") == 0) {
        $errors["affiliation"] = "Missing Field";
    }
    elseif (! TBvalid_affiliation($formfields["affiliation"])) {
        $errors["affiliation"] = TBFieldErrorString();
    }
    if (!isset($formfields["country"]) ||
        strcmp($formfields["country"], "") == 0) {
        $errors["country"] = "Missing Field";
    }
    elseif (! TBvalid_country($formfields["country"])) {
        $errors["country"] = TBFieldErrorString();
    }
    if (!isset($formfields["state"]) ||
        strcmp($formfields["state"], "") == 0) {
        $errors["state"] = "Missing Field";
    }
    elseif (! TBvalid_state($formfields["state"])) {
        $errors["state"] = TBFieldErrorString();
    }
    if (!isset($formfields["city"]) ||
        strcmp($formfields["city"], "") == 0) {
        $errors["city"] = "Missing Field";
    }
    elseif (! TBvalid_city($formfields["city"])) {
        $errors["city"] = TBFieldErrorString();
    }

    # Present these errors before we call out to do anything else.
    if (count($errors)) {
	SPITAJAX_ERROR(2, $errors);
        return -1;
    }

    #
    # Lets get the user to do the email verification now before
    # we go any further. We use a session variable to store the
    # key we send to the user in email.
    #
154
    if ($formfields["email"] != $target_user->email() && !ISADMIN()) {
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
        if (!isset($_SESSION["verify_key"])) {
            $_SESSION["verify_key"] = substr(GENHASH(), 0, 16);
            $_SESSION["verified"]   = 0;
            $_SESSION["codesent"]   = 0;
        }
        #
        # Once the user verifies okay, we remember that in the session
        # in case there is a later error below.
        #
        if (!$_SESSION["verified"]) {
            if (!$_SESSION["codesent"]) {
                mail($formfields["email"],
                     "Confirm your new email",
                     "Here is your email verification code. Please copy and\n".
                     "paste this code into the box on the account page.\n\n".
                     "\t" . $_SESSION["verify_key"] . "\n",
                     "From: $APTMAIL");
                $_SESSION["codesent"] = 1;
            }
            if (isset($formfields["verify"]) &&
                $formfields["verify"] == $_SESSION["verify_key"]) {
                #
                # Success. Lets remember that in case we get an error
                # below and  the form is redisplayed.
                #
                $_SESSION["verified"] = 1;
            }
            else {
                #
                # Tell caller to throw up the verification form.
                #
                SPITAJAX_ERROR(3, 0);
                return -1;
            }
        }
    }
    return 0;
}

#
# Submit
#
function Do_Update()
{
    global $this_user, $target_user;
    global $ajax_args;
    
    # For email verification if needed.
    session_start();

    if (CheckPageArgs() < 0) {
        return;
    }
    if (CheckForm() < 0) {
        return;
    }
    # Allow for form precheck only. So JS code knows it will be fast.
    if (isset($ajax_args["checkonly"]) && $ajax_args["checkonly"]) {
        SPITAJAX_RESPONSE(0);
        return;
    }
    $formfields = $ajax_args["formfields"];
217
    $target_uid = $target_user->uid();
218 219 220
    $args   = array();
    $errors = array();

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
    #
    # If the info has not changed, do not send it through, the backend
    # will consider that a change (its stupid) and might try to do things
    # that are not allowed on unapproved users. Need to fix this.
    #
    if ($formfields["name"] != $target_user->name()) {
        $args["name"] = $formfields["name"];
    }
    if ($formfields["city"] != $target_user->city()) {
        $args["city"] = $formfields["city"];
    }
    if ($formfields["state"] != $target_user->state()) {
        $args["state"] = $formfields["state"];
    }
    if ($formfields["country"] != $target_user->country()) {
        $args["country"] = $formfields["country"];
    }
    if ($formfields["affiliation"] != $target_user->affil()) {
        $args["affiliation"] = $formfields["affiliation"];
    }
    if (count($args) &&
        !User::ModUserInfo($target_user,
                           $target_user->uid(), $args, $errors)) {
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
	SPITAJAX_ERROR(2, $errors);
        return;
    }
    #
    # The user can change their email on this pass, but the backend
    # does not allow that, so have to call tbacct directly with an
    # extra option to tell it to skip the admin check.
    #
    if ($formfields["email"] != $target_user->email()) {
        $safe_email = $formfields["email"];
    
        if (!HASREALACCOUNT($target_uid)) {
            $retval = SUEXEC("nobody", "nobody",
                             "webtbacct -v email $target_uid $safe_email",
                             SUEXEC_ACTION_CONTINUE);
        }
        else {
            $retval = SUEXEC($target_uid, "nobody",
                             "webtbacct -v email $target_uid $safe_email",
                             SUEXEC_ACTION_CONTINUE);
        }
        if ($retval) {
266
            $errors["error"] = "Internal error changing email";
267 268 269 270 271 272 273 274 275 276 277 278
            SPITAJAX_ERROR(2, $errors);
            return;
        }
    }
    session_destroy();
    SPITAJAX_RESPONSE(0);
}

# Local Variables:
# mode:php
# End:
?>