function set_profile_field($userid, $field, $value)
{
    safe_require('artefact', 'internal');
    // this is a special case that replaces the primary email address with the
    // specified one
    if ($field == 'email') {
        try {
            $email = artefact_instance_from_type('email', $userid);
        } catch (ArtefactNotFoundException $e) {
            $email = new ArtefactTypeEmail();
            $email->set('owner', $userid);
        }
        $email->set('title', $value);
        $email->commit();
    } else {
        $classname = generate_artefact_class_name($field);
        $profile = new $classname(0, array('owner' => $userid));
        $profile->set('title', $value);
        $profile->commit();
    }
}
Exemple #2
0
function profileform_submit(Pieform $form, $values)
{
    global $SESSION;
    global $USER;
    global $element_list;
    global $profilefields;
    require_once 'embeddedimage.php';
    db_begin();
    $now = db_format_timestamp(time());
    $email_errors = array();
    $lockedfields = locked_profile_fields();
    foreach ($element_list as $element => $type) {
        if (isset($lockedfields[$element]) && !$USER->get('admin')) {
            continue;
        }
        if ($element == 'email') {
            if (!isset($values['email']['unsent'])) {
                $values['email']['unsent'] = array();
            }
            // find new addresses
            foreach ($values['email']['unsent'] as $email) {
                if (in_array($email, $profilefields['email']['validated']) || in_array($email, $profilefields['email']['unvalidated'])) {
                    continue;
                }
                $key = get_random_key();
                $key_url = get_config('wwwroot') . 'artefact/internal/validate.php?email=' . rawurlencode($email) . '&key=' . $key;
                $key_url_decline = $key_url . '&decline=1';
                try {
                    $sitename = get_config('sitename');
                    email_user((object) array('id' => $USER->get('id'), 'username' => $USER->get('username'), 'firstname' => $USER->get('firstname'), 'lastname' => $USER->get('lastname'), 'preferredname' => $USER->get('preferredname'), 'admin' => $USER->get('admin'), 'staff' => $USER->get('staff'), 'email' => $email), null, get_string('emailvalidation_subject', 'artefact.internal'), get_string('emailvalidation_body1', 'artefact.internal', $USER->get('firstname'), $email, $sitename, $key_url, $sitename, $key_url_decline));
                } catch (EmailException $e) {
                    $email_errors[] = $email;
                }
                insert_record('artefact_internal_profile_email', (object) array('owner' => $USER->get('id'), 'email' => $email, 'verified' => 0, 'key' => $key, 'expiry' => db_format_timestamp(time() + 86400)));
            }
            // remove old addresses
            foreach ($profilefields['email']['validated'] as $email) {
                if (in_array($email, $values['email']['validated'])) {
                    continue;
                }
                if (!empty($values['email']['unvalidated']) && in_array($email, $values['email']['unvalidated'])) {
                    continue;
                }
                $artefact_id = get_field('artefact_internal_profile_email', 'artefact', 'email', $email, 'owner', $USER->get('id'));
                delete_records('artefact_internal_profile_email', 'email', $email, 'owner', $USER->get('id'));
                if ($artefact_id) {
                    $artefact = new ArtefactTypeEmail($artefact_id);
                    $artefact->delete();
                    // this is unset here to force the destructor to run now,
                    // rather than script exit time where it doesn't like
                    // throwing exceptions properly
                    unset($artefact);
                }
            }
            foreach ($profilefields['email']['unvalidated'] as $email) {
                if (in_array($email, $values['email']['validated']) || isset($values['email']['unvalidated']) && in_array($email, $values['email']['unvalidated'])) {
                    continue;
                }
                delete_records('artefact_internal_profile_email', 'email', $email, 'owner', $USER->get('id'));
            }
            if ($profilefields['email']['default'] != $values['email']['default']) {
                update_record('artefact_internal_profile_email', (object) array('principal' => 0), (object) array('owner' => $USER->get('id'), 'email' => $profilefields['email']['default']));
                update_record('artefact_internal_profile_email', (object) array('principal' => 1), (object) array('owner' => $USER->get('id'), 'email' => $values['email']['default']));
                update_record('usr', (object) array('email' => $values['email']['default']), (object) array('id' => $USER->get('id')));
                $USER->email = $values['email']['default'];
                $USER->commit();
            }
        } else {
            if ($element == 'maildisabled') {
                continue;
            } else {
                if ($element == 'socialprofile') {
                    continue;
                } else {
                    if (!isset($profilefields[$element]) || $values[$element] != $profilefields[$element]) {
                        if ($element == 'introduction') {
                            $newintroduction = EmbeddedImage::prepare_embedded_images($values[$element], 'profileintrotext', $USER->get('id'));
                            $values[$element] = $newintroduction;
                        }
                        $classname = generate_artefact_class_name($element);
                        $profile = new $classname(0, array('owner' => $USER->get('id')));
                        $profile->set('title', $values[$element]);
                        $profile->commit();
                    }
                }
            }
        }
    }
    try {
        db_commit();
    } catch (Exception $e) {
        profileform_reply($form, PIEFORM_ERR, get_string('profilefailedsaved', 'artefact.internal'));
    }
    handle_event('updateuser', $USER->get('id'));
    if (count($email_errors)) {
        profileform_reply($form, PIEFORM_ERR, array('message' => get_string('emailingfailed', 'artefact.internal', join(', ', $email_errors))));
    }
    profileform_reply($form, PIEFORM_OK, get_string('profilesaved', 'artefact.internal'));
}
Exemple #3
0
/**
 * Save a profile field.
 * Exception is 'socialprofile' field. It is made up of 2 fields:
 * socialprofile_profileurl,
 * socialprofile_profiletype
 * @param int $userid
 * @param string $field
 * @param string (or array for socialprofile) $value
 * @param int $new - Whether the user is new (avoid unnecessary queries)
 */
function set_profile_field($userid, $field, $value, $new = FALSE)
{
    safe_require('artefact', 'internal');
    // this is a special case that replaces the primary email address with the
    // specified one
    if ($field == 'email') {
        if (!$new) {
            try {
                $email = artefact_instance_from_type('email', $userid);
            } catch (ArtefactNotFoundException $e) {
                // We'll create a new artefact then.
            }
        }
        if (!isset($email)) {
            $email = new ArtefactTypeEmail(0, null, TRUE);
            $email->set('owner', $userid);
        }
        $email->set('title', $value);
        $email->commit();
    } else {
        if ($field == 'socialprofile') {
            if (in_array($value['socialprofile_profiletype'], ArtefactTypeSocialprofile::$socialnetworks)) {
                $desc = get_string($value['socialprofile_profiletype'], 'artefact.internal');
                $type = $value['socialprofile_profiletype'];
            } else {
                $desc = $value['socialprofile_profiletype'];
                $type = 'website';
            }
            $classname = generate_artefact_class_name($field);
            $profile = new $classname(0, array('owner' => $userid), $new);
            $profile->set('title', $value['socialprofile_profileurl']);
            $profile->set('description', $desc);
            $profile->set('note', $type);
            $profile->commit();
        } else {
            $classname = generate_artefact_class_name($field);
            $profile = new $classname(0, array('owner' => $userid), $new);
            $profile->set('title', $value);
            $profile->commit();
        }
    }
}
define('TITLE', get_string('emailactivation', 'artefact.internal'));
$email = param_variable('email');
$key = param_variable('key');
$decline = param_boolean('decline');
$row = get_record('artefact_internal_profile_email', 'email', $email, 'key', $key, null, null, 'owner,artefact,email,verified,' . db_format_tsfield('expiry'));
if ($row) {
    if ($decline) {
        delete_records_select('artefact_internal_profile_email', 'verified=0 AND key=? AND email=?', array($key, $email));
        $SESSION->add_ok_msg(get_string('emailactivationdeclined', 'artefact.internal'));
        redirect(get_config('wwwroot'));
    }
    if ($row->expiry > time()) {
        if ($row->artefact) {
            $artefact = new ArtefactTypeEmail($row->artefact);
        } else {
            $artefact = new ArtefactTypeEmail();
        }
        $artefact->set('owner', $row->owner);
        $artefact->set('title', $row->email);
        $artefact->commit();
        update_record('artefact_internal_profile_email', (object) array('verified' => 1, 'key' => null, 'expiry' => null, 'artefact' => $artefact->get('id')), (object) array('owner' => $row->owner, 'email' => $row->email));
        $SESSION->add_ok_msg(get_string('emailactivationsucceeded', 'artefact.internal'));
        // Update user's email if this email address is primary (principal == 1)
        if (record_exists('artefact_internal_profile_email', 'owner', $row->owner, 'email', $row->email, 'principal', 1)) {
            update_record('usr', (object) array('email' => $row->email), (object) array('id' => $row->owner));
            redirect(get_config('wwwroot'));
        } else {
            redirect(get_config('wwwroot') . 'artefact/internal/index.php?fs=contact');
        }
    } else {
        $message = get_string('verificationlinkexpired', 'artefact.internal');