mirrored from git://git.moodle.org/moodle.git
/
edit.php
165 lines (123 loc) · 5.67 KB
/
edit.php
1
2
3
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
66
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
154
155
156
157
158
159
160
161
162
163
164
165
<?PHP // $Id$
require("../config.php");
require("lib.php");
require_variable($id); // user id
require_variable($course); // course id
if (! $user = get_record("user", "id", $id)) {
error("User ID was incorrect");
}
if (! $course = get_record("course", "id", $course)) {
error("User ID was incorrect");
}
require_login($course->id);
if ($USER->id <> $user->id) {
error("You can only edit your own information");
}
/// If data submitted, then process and store.
if (match_referer() && isset($HTTP_POST_VARS)) {
$usernew = (object)$HTTP_POST_VARS;
if (!find_form_errors($user, $usernew, $err) ) {
$timenow = time();
if ($imagefile && $imagefile!="none") {
$imageinfo = GetImageSize($imagefile);
$image->width = $imageinfo[0];
$image->height = $imageinfo[1];
$image->type = $imageinfo[2];
switch ($image->type) {
case 2: $im = ImageCreateFromJPEG($imagefile); break;
case 3: $im = ImageCreateFromPNG($imagefile); break;
default: error("Image must be in JPG or PNG format");
}
if (function_exists("ImageCreateTrueColor")) {
$im1 = ImageCreateTrueColor(100,100);
$im2 = ImageCreateTrueColor(35,35);
} else {
$im1 = ImageCreate(100,100);
$im2 = ImageCreate(35,35);
}
$cx = $image->width / 2;
$cy = $image->height / 2;
if ($image->width < $image->height) {
$half = floor($image->width / 2.0);
} else {
$half = floor($image->height / 2.0);
}
if (!file_exists("$CFG->dataroot/users")) {
if (! mkdir("$CFG->dataroot/users", 0777)) {
$badpermissions = true;
}
}
if (!file_exists("$CFG->dataroot/users/$USER->id")) {
if (! mkdir("$CFG->dataroot/users/$USER->id", 0777)) {
$badpermissions = true;
}
}
if ($badpermissions) {
$usernew->picture = "0";
} else {
ImageCopyBicubic($im1, $im, 0, 0, $cx-$half, $cy-$half, 100, 100, $half*2, $half*2);
ImageCopyBicubic($im2, $im, 0, 0, $cx-$half, $cy-$half, 35, 35, $half*2, $half*2);
// Draw borders over the top.
$black1 = ImageColorAllocate ($im1, 0, 0, 0);
$black2 = ImageColorAllocate ($im2, 0, 0, 0);
ImageLine ($im1, 0, 0, 0, 99, $black1);
ImageLine ($im1, 0, 99, 99, 99, $black1);
ImageLine ($im1, 99, 99, 99, 0, $black1);
ImageLine ($im1, 99, 0, 0, 0, $black1);
ImageLine ($im2, 0, 0, 0, 34, $black2);
ImageLine ($im2, 0, 34, 34, 34, $black2);
ImageLine ($im2, 34, 34, 34, 0, $black2);
ImageLine ($im2, 34, 0, 0, 0, $black2);
ImageJpeg($im1, "$CFG->dataroot/users/$USER->id/f1.jpg", 90);
ImageJpeg($im2, "$CFG->dataroot/users/$USER->id/f2.jpg", 95);
$usernew->picture = "1";
}
} else {
$usernew->picture = $user->picture;
}
$usernew->timemodified = time();
if (update_record("user", $usernew)) {
add_to_log($course->id, "user", "update", "view.php?user=$user->id&course=$course->id", "");
// Copy data into $USER session variable
$usernew = (array)$usernew;
foreach ($usernew as $variable => $value) {
$USER->$variable = $value;
}
redirect("view.php?id=$user->id&course=$course->id", "Changes saved");
} else {
error("Could not update the user record ($user->id)");
}
}
}
/// Otherwise fill and print the form.
if ($course->category) {
print_header("Edit user profile", "Edit user profile",
"<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A>
-> <A HREF=\"index.php?id=$course->id\">Participants</A>
-> <A HREF=\"view.php?id=$USER->id&course=$course->id\">$USER->firstname $USER->lastname</A>
-> Edit profile", "");
} else {
print_header("Edit user profile", "Edit user profile",
"<A HREF=\"view.php?id=$USER->id&course=$course->id\">$USER->firstname $USER->lastname</A>
-> Edit profile", "");
}
print_simple_box_start("center", "", "$THEME->cellheading");
echo "<H2>User profile for $user->firstname $user->lastname</H2>";
include("edit.html");
print_simple_box_end();
print_footer($course);
/// FUNCTIONS ////////////////////
function find_form_errors(&$user, &$usernew, &$err) {
if (empty($usernew->email))
$err["email"] = "Missing email address";
else if (! validate_email($usernew->email))
$err["email"] = "Invalid email address, check carefully";
else if ($otheruser = get_record("user", "email", $usernew->email)) {
if ($otheruser->id <> $user->id) {
$err["email"] = "Email address already in use by someone else.";
}
}
$user->email = $usernew->email;
return count($err);
}
?>