profile.module 13.8 KB
Newer Older
Dries's avatar
   
Dries committed
1
<?php
Dries's avatar
   
Dries committed
2
// $Id$
Dries's avatar
   
Dries committed
3
4
5
6
7
8
9
10
11

function _profile_init() {
  /*
  ** Add here any field you might need.  Leave array[0] blank if you
  ** need a special tool (like birthday or avatar).
  **  TODO: add a clear description/explanation.
  */

  $GLOBALS["profile_fields"] = array(
Dries's avatar
   
Dries committed
12
    "realname"      => array("textfield", t("Name"), "", 64, 64, ""),
Dries's avatar
   
Dries committed
13
14
    "address"       => array("textfield", t("Address"), "", 64, 64, ""),
    "city"          => array("textfield", t("City"), "", 64, 64, ""),
Dries's avatar
   
Dries committed
15
16
17
    "state"         => array("textfield", t("State, province or region"), "", 64, 64, ""),
    "zip"           => array("textfield", t("Zip or postal code"), "", 7, 10, ""),
    "country"       => array("textfield", t("Country"), "", 64, 64, ""),
Dries's avatar
   
Dries committed
18
19
    "birthday"      => array("", t("Birthday"), ""),
    "gender"        => array("select", t("Gender"), "", array(0 => "-", "m" => t("male"), "f" => t("female")), "", 0, 0),
Dries's avatar
   
Dries committed
20
    "job"           => array("textfield", t("Job title"), "", 64, 64, ""),
Dries's avatar
   
Dries committed
21
22
23
24
25
26
    "icq"           => array("textfield", t("ICQ messenger ID"), "", 12, 12, ""),
    "msn"           => array("textfield", t("MSN messenger ID"), "", 64, 64, ""),
    "yahoo"         => array("textfield", t("Yahoo messenger ID"), "", 64, 64, ""),
    "aim"           => array("textfield", t("AIM messenger ID"), "", 64, 64, ""),
    "homepage"      => array("textfield", t("URL of homepage"), "", 64, 64, t("Make sure you enter a fully qualified URL: remember to include \"http://\".")),
    "biography"     => array("textarea", t("Biography"), "", 64, 4, ""),
Dries's avatar
   
Dries committed
27
    "interests"     => array("textarea", t("Interests"), "", 64, 4, ""),
Dries's avatar
   
Dries committed
28
    "publickey"     => array("textarea", t("Public key"), "", 64, 4, ""),
Dries's avatar
   
Dries committed
29
    "avatar"        => array("", t("Avatar or picture"), t("Your virtual face or picture.  Maximum dimensions are %dimensions and the maximum size is %size kB.", array("%dimensions" => variable_get("profile_avatar_dimensions", "85x85"), "%size" => variable_get("profile_avatar_file_size", "30"))))
Dries's avatar
   
Dries committed
30
  );
Dries's avatar
   
Dries committed
31
32

  $GLOBALS["profile_days"][0] = t("day");
Dries's avatar
   
Dries committed
33
  for ($n = 1; $n <= 31; $n++) {
Dries's avatar
   
Dries committed
34
35
36
37
38
39
    $GLOBALS["profile_days"][$n] = $n;
  }

  $GLOBALS["profile_months"] = array(0 => t("month"), 1 => t("January"), 2 => t("February"), 3 => t("March"), 4 => t("April"), 5 => t("May"), 6 => t("June"), 7 => t("July"), 8 => t("August"), 9 => t("September"), 10 => t("October"), 11 => t("November"), 12 => t("December"));
}

Dries's avatar
   
Dries committed
40
41
42
43
function profile_help($section) {
  $output = "";

  switch ($section) {
Dries's avatar
   
Dries committed
44
    case 'admin/system/modules#description':
Dries's avatar
   
Dries committed
45
      $output = t("Support for configurable user profiles.");
Dries's avatar
   
Dries committed
46
47
      break;
    case 'admin/system/modules/profile':
Dries's avatar
   
Dries committed
48
      $output = t("When a user creates an account you can ask for some extra information, as well as letting the user have a small picture, called an avatar.<br />Notes:<ul><li>In order for a user to enter information you <b>must</b> check \"enable\".</li><li>In order for other people too see the entered information you must make it \"public\".</li><li>If an item is \"public\", but not enabled, the user can never give it a value and it will never be seen.  Public does <b>not</b> imply \"enable\".</li><li>If an item is enabled, but not shown in the registration form the user will have to %edit to place information in the field.</ul>", array("%edit" => l(t("edit their account"), "user/edit")));
Dries's avatar
   
Dries committed
49
50
      break;
  }
Dries's avatar
   
Dries committed
51
  return $output;
Dries's avatar
   
Dries committed
52
53
}

54
function profile_settings() {
Dries's avatar
   
Dries committed
55
56
57
58
59
  global $profile_fields;
  if (!$profile_fields) {
    _profile_init();
  }

Dries's avatar
   
Dries committed
60
61
62
63
  if (!file_check_directory(file_create_path(variable_get('profile_avatar_path', 'avatars')))) {
    $error['profile_avatar_path'] = theme('error', t('Directory does not exist, or is not writable.'));
  }

Dries's avatar
   
Dries committed
64
65
  $profile_public_fields = variable_get("profile_public_fields", array());
  $profile_private_fields = variable_get("profile_private_fields", array());
Dries's avatar
   
Dries committed
66
  $profile_required_fields = variable_get("profile_required_fields", array());
Dries's avatar
   
Dries committed
67
68
  $profile_register_fields = variable_get("profile_register_fields", array());

69
70
  $header = array (t("field"), t("enable"), t("public"), t("required"), t("show in registration form"));
  $i=0;
Dries's avatar
   
Dries committed
71
  foreach ($profile_fields as $key => $field) {
72
73
74
75
76
77
    $row[$i][] = $field[1];
    $row[$i][] = form_checkbox("", "profile_private_fields][", $key, in_array($key, $profile_private_fields));
    $row[$i][] = form_checkbox("", "profile_public_fields][", $key, in_array($key, $profile_public_fields));
    $row[$i][] = form_checkbox("", "profile_required_fields][", $key, in_array($key, $profile_required_fields));
    $row[$i][] = form_checkbox("", "profile_register_fields][", $key, in_array($key, $profile_register_fields));
    $i++;
Dries's avatar
   
Dries committed
78
  }
Dries's avatar
   
Dries committed
79
  $output .= theme("table", $header, $row);
Dries's avatar
   
Dries committed
80

Dries's avatar
   
Dries committed
81
  $output .= form_textfield(t("Avatar image path"), "profile_avatar_path", variable_get("profile_avatar_path", "avatars"), 30, 255, t("Subdirectory in the directory '%dir' where avatars will be stored.", array('%dir' => variable_get('file_directory_path', 'files') . FILE_SEPARATOR)) . $error['profile_avatar_path']);
Dries's avatar
   
Dries committed
82
  $output .= form_textfield(t("Avatar maximum dimensions"), "profile_avatar_dimensions", variable_get("profile_avatar_dimensions", "85x85"), 10, 10, t("Maximum dimensions for avatars."));
Dries's avatar
   
Dries committed
83
  $output .= form_textfield(t("Avatar maximum file size"), "profile_avatar_file_size", variable_get("profile_avatar_file_size", "30"), 10, 10, t("Maximum file size for avatars, in kB."));
Dries's avatar
   
Dries committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

  return $output;
}

function profile_user($type, $edit, &$user) {
  global $profile_fields;
  if (!$profile_fields) {
    _profile_init();
  }

  switch ($type) {
    case "register_form":
      // first registration form (to add something to just email and nick)
      return _profile_form($edit, "register");
    case "register_validate":
      // validate first registration form
Dries's avatar
   
Dries committed
100
      return _profile_validate($edit, "required", $user);
Dries's avatar
   
Dries committed
101
102
103
104
105
    case "edit_form":
      // when user tries to edit his own data
      return _profile_form(object2array($user), "private");
    case "edit_validate":
      // validate user data editing
Dries's avatar
   
Dries committed
106
      return _profile_validate($edit, "private", $user);
Dries's avatar
   
Dries committed
107
108
109
110
111
112
    case "view_public":
      // when others look at user data
      return _profile_user_view($user, "public");
    case "view_private":
      // when user looks at his own data
      return _profile_user_view($user, "private");
Dries's avatar
   
Dries committed
113
  }
Dries's avatar
   
Dries committed
114
115
116
}

function profile_required($title) {
117
  // this pleads "theme, theme" ;)
Dries's avatar
   
Dries committed
118
  return $title ." ". theme("mark");
Dries's avatar
   
Dries committed
119
120
121
122
123
124
125
126
127
128
}

function _profile_form($edit, $mode) {
  global $profile_fields, $user;

  $reg_fields = _profile_active_fields($mode);
  $required_fields = _profile_active_fields("required");

  foreach ($profile_fields as $name => $field) {
    if ($field[0] && in_array($name, $reg_fields)) {
Dries's avatar
   
Dries committed
129
130
      $f = "form_". $field[0];
      $t = "profile_". $name;
Dries's avatar
   
Dries committed
131
132
133
134
135
136
137
138
139
      $output .= $f((in_array($name, $required_fields) ? profile_required($field[1]) : $field[1]), $t, $edit[$t], $field[3], $field[4], $field[5], $field[6]);
    }
  }

  if (in_array("birthday", $reg_fields)) {
    $output .= form_item((in_array("birthday", $required_fields) ? profile_required($profile_fields["birthday"][1]) : $profile_fields["birthday"][1]), _profile_edit_birth(array2object($edit)), $profile_fields["birthday"][2]);
  }

  if (in_array("avatar", $reg_fields)) {
Dries's avatar
   
Dries committed
140
141
    if ($edit["profile_avatar"] && file_exists($edit["profile_avatar"])) {
      $output .= form_item(t("Avatar"), '<img src="'. file_create_url($edit["profile_avatar"]) .'" alt="" title="" />');
Dries's avatar
   
Dries committed
142
143
144
145
146
147
148
    }
    $output .= form_file($profile_fields["avatar"][1], "profile_avatar", 64, $profile_fields["avatar"][2]);
  }

  return $output;
}

Dries's avatar
   
Dries committed
149
function _profile_validate($edit, $mode, $user) {
Dries's avatar
   
Dries committed
150

Dries's avatar
   
Dries committed
151
  global $profile_fields;
Dries's avatar
   
Dries committed
152

Dries's avatar
   
Dries committed
153
  $enabled_fields = _profile_active_fields($mode);
Dries's avatar
   
Dries committed
154

Dries's avatar
   
Dries committed
155
  if (in_array("birthday", $enabled_fields) && ($birth_error = _profile_validate_birth($edit))) {
Dries's avatar
   
Dries committed
156
    $error .= $birth_error ."<br />";
Dries's avatar
   
Dries committed
157
158
  }

Dries's avatar
   
Dries committed
159
  if (in_array("avatar", $enabled_fields) && ($avatar_error = _profile_validate_avatar($edit, $user))) {
Dries's avatar
   
Dries committed
160
    $error .= $avatar_error ."<br />";
Dries's avatar
   
Dries committed
161
162
163
164
165
166
167
168
169
170
  }

  foreach (array_keys($profile_fields) as $field) {
    // replicate any key which was saved during registration but is not in this form
    if (!$edit[$field] && $user->$field) {
      $edit[$field] = $user->$field;
    }
  }

  // now check for required fields
Dries's avatar
   
Dries committed
171
  foreach (_profile_active_fields("required") as $required) {
Dries's avatar
   
Dries committed
172
    if ($required != "0" && in_array($required, $enabled_fields)) {
Dries's avatar
   
Dries committed
173
174
      if (!$edit["profile_". $required]) {
        $error .= t("This required field is missing: %a", array("%a" => $profile_fields[$required][1])) ."<br />";
Dries's avatar
   
Dries committed
175
176
177
178
      }
    }
  }

Dries's avatar
   
Dries committed
179
  return $error ? $error : $edit;
Dries's avatar
   
Dries committed
180
181
182
183
184
185
186
}

function _profile_user_view(&$user, $mode) {
  global $profile_fields;

  foreach (_profile_active_fields($mode) as $name) {
    $field = $profile_fields[$name];
Dries's avatar
   
Dries committed
187
    $t = "profile_". $name;
188

Dries's avatar
   
Dries committed
189
    if (!empty($user->$t)) {
Dries's avatar
   
Dries committed
190
191
      switch ($field[0]) {
        case "textfield":
Dries's avatar
   
Dries committed
192
193
        case "textarea":
        case "checkbox":
194
          $value = ($t == "profile_homepage") ? "<a href=\"". drupal_specialchars($user->$t) ."\">". check_output($user->$t) ."</a>" : check_output($user->$t);
195
          $output .= form_item($field[1], $value);
Dries's avatar
   
Dries committed
196
197
          break;
        case "select":
Dries's avatar
   
Dries committed
198
          $output .= form_item($field[1], check_output($profile_fields[$name][3][$user->$t]));
Dries's avatar
   
Dries committed
199
200
201
202
          break;
        case "":
          // special
          if ($t == "profile_avatar") {
Dries's avatar
   
Dries committed
203
204
            if (file_exists($user->$t)) {
              $output .= form_item(t("Avatar"), '<img src="'. file_create_url($user->$t) .'" alt="" title="" />');
Dries's avatar
   
Dries committed
205
206
207
            }
          }

Dries's avatar
   
Dries committed
208
209
210
211
          if ($t == "profile_birthday") {
            if (isset($user->profile_birthday) && isset($user->profile_birthmonth) && isset($user->profile_birthyear)) {
              // this is very european-centric, can we use format_date?
              $time = mktime(0, 0, 0, $user->profile_birthmonth, $user->profile_birthday, $user->profile_birthyear);
212
              $output .= form_item(t("Birthday"), format_date($time, "custom", "F j, Y"));
Dries's avatar
   
Dries committed
213
214
            }
          }
Dries's avatar
   
Dries committed
215
216
217
218
219
220
      }
    }
  }
  return $output;
}

Dries's avatar
   
Dries committed
221
function profile_file_download($file) {
Kjartan's avatar
Kjartan committed
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
  if (strpos($file, variable_get("profile_avatar_path", "avatars") . FILE_SEPARATOR . 'avatar-') === 0) {
    list($width, $height, $type, $attr) = getimagesize(file_create_path($file));
    $types = array(
      IMAGETYPE_GIF => 'image/gif',
      IMAGETYPE_JPEG => 'image/jpeg',
      IMAGETYPE_PNG => 'image/png',
      IMAGETYPE_SWF => 'application/x-shockwave-flash',
      IMAGETYPE_PSD => 'image/psd',
      IMAGETYPE_BMP => 'image/bmp',
      IMAGETYPE_TIFF_II => 'image/tiff',
      IMAGETYPE_TIFF_MM  => 'image/tiff',
      IMAGETYPE_JPC => 'application/octet-stream',
      IMAGETYPE_JP2 => 'image/jp2',
      IMAGETYPE_JPX => 'application/octet-stream',
      IMAGETYPE_JB2 => 'application/octet-stream',
      IMAGETYPE_SWC => 'application/x-shockwave-flash',
      IMAGETYPE_IFF => 'image/iff',
      IMAGETYPE_WBMP => 'image/vnd.wap.wbmp',
      IMAGETYPE_XBM => 'image/xbm'
    );
    return array('Content-type: '. $types[$type]);
Dries's avatar
   
Dries committed
243
244
245
  }
}

Dries's avatar
   
Dries committed
246
function _profile_validate_avatar(&$edit, $user) {
Dries's avatar
   
Dries committed
247
  // check that uploaded file is an image, with a maximum file size and maximum height/width
Dries's avatar
   
Dries committed
248
249
250

  unset($edit["profile_avatar"]);

Dries's avatar
   
Dries committed
251
  if (!$file = file_check_upload('profile_avatar')) {
Dries's avatar
   
Dries committed
252
    $edit["profile_avatar"] = $user->profile_avatar;
Dries's avatar
   
Dries committed
253
    return;
Dries's avatar
   
Dries committed
254
255
  }

Dries's avatar
   
Dries committed
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
  $extension = strtolower(strrchr($file->name, "."));
  $size = getimagesize($file->path);
  list($maxwidth, $maxheight) = explode("x", variable_get("profile_avatar_dimensions", "85x85"));
  if ((!in_array($size[2], array(1, 2, 3))) || (!in_array($extension, array(".gif", ".jpg", ".png", ".jpeg")))) {
    $error = t("The uploaded file was not an image.");
  }
  else if ($file->size > (variable_get("profile_avatar_file_size", "30") * 1000)) {
    $error = t("The uploaded image is too large; the maximum file size is %a kB.", array("%a" => variable_get("profile_avatar_file_size", "30")));
  }
  else if ($size[0] > $maxwidth || $size[1] > $maxheight) {
    $error = t("The uploaded image is too large; the maximum dimensions are %a pixels.", array("%a" => variable_get("profile_avatar_dimensions", "85x85")));
  }
  else if ($file = file_save_upload('profile_avatar', variable_get("profile_avatar_path", "avatars") . FILE_SEPARATOR .'avatar-'. $user->uid . $extension, 1)) {
    $edit["profile_avatar"] = $file->name;
  }
  else {
    $error = t("Failed to upload the avatar image; the '%directory' directory doesn't exist.", array("%directory" => variable_get("profile_avatar_path", "avatars")));
Dries's avatar
   
Dries committed
273
274
  }

Dries's avatar
   
Dries committed
275
  return $error ? "$error<br />" : "";
Dries's avatar
   
Dries committed
276
277
278
}

function _profile_active_fields($mode) {
Dries's avatar
   
Dries committed
279
  return variable_get("profile_". $mode ."_fields", array());
Dries's avatar
   
Dries committed
280
281
282
283
}

function _profile_edit_birth($edit = "") {
  global $profile_months, $profile_days;
Dries's avatar
   
Dries committed
284
  $output = _profile_select("profile_birthday", $edit->profile_birthday, $profile_days);
Dries's avatar
   
Dries committed
285
286
287
  $output .= "&nbsp;";
  $output .= _profile_select("profile_birthmonth", $edit->profile_birthmonth, $profile_months);
  $output .= "&nbsp;";
Dries's avatar
   
Dries committed
288
  $output .= "<input type=\"text\" maxlength=\"4\" name=\"edit[profile_birthyear]\" size=\"5\" value=\"$edit->profile_birthyear\" />";
Dries's avatar
   
Dries committed
289
290
291
292
293
294
295
296
297
298
299
300
301
  return $output;
}

function _profile_validate_birth(&$edit) {
  if (!$edit["profile_birthday"] && !$edit["profile_birthmonth"] && !$edit["profile_birthyear"]) {
    // change this if you want required birth
    return;
  }

  if ($edit["profile_birthyear"] > 1900 && checkdate($edit["profile_birthmonth"], $edit["profile_birthday"], $edit["profile_birthyear"])) {
    return;
  }
  else {
Dries's avatar
   
Dries committed
302
    return t("The specified birthday is not valid.") ."<br />";
Dries's avatar
   
Dries committed
303
304
305
306
307
308
309
310
311
312
313
314
  }
}

function _profile_select($name, $value, $options, $extra = 0, $multiple = 0) {
  if (count($options) > 0) {
    foreach ($options as $key=>$choice) {
      $select .= "<option value=\"$key\"". (is_array($value) ? (in_array($key, $value) ? " selected=\"selected\"" : "") : ($key == $value ? " selected=\"selected\"" : "")) .">". check_form($choice) ."</option>";
    }
    return "<select name=\"edit[$name]". ($multiple ? "[]" : "") ."\"". ($multiple ? " multiple " : "") . ($extra ? " $extra" : "") .">$select</select>";
  }
}

Dries's avatar
   
Dries committed
315
?>