| 1 |
<?php |
<?php |
| 2 |
// $Id: unique_avatar.module,v 1.1 2008/01/29 18:08:51 jscheel Exp $ |
// $Id: unique_avatar.module,v 1.2 2008/01/29 18:54:38 jscheel Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file unique_avatar.module |
* @file unique_avatar.module |
| 7 |
* Full module implementation, creates unique user picture filenames |
* Full module implementation, creates unique user picture filenames |
| 8 |
*/ |
*/ |
| 9 |
|
|
| 10 |
/** |
function unique_avatar_form_alter(&$form, $form_state, $form_id) { |
| 11 |
* Implementation of hook_user() |
if ($form_id == 'user_profile_form' && variable_get('user_pictures', 0) && !$register) { |
| 12 |
*/ |
$key = array_search('user_validate_picture', $form['#validate']); |
| 13 |
function unique_avatar_user($type, &$edit, &$user, $category = NULL) { |
if ($key !== FALSE) { |
| 14 |
if ($type == 'submit' && $category == 'account') { |
$form['#validate'][$key] = 'unique_avatar_validate_picture'; |
| 15 |
|
} |
| 16 |
//Is the user picture being modified? |
} |
| 17 |
$file = file_check_upload('picture_upload'); |
} |
| 18 |
if ($file) { |
|
| 19 |
$info = image_get_info($file->filepath); |
function unique_avatar_validate_picture(&$form, &$form_state) { |
| 20 |
|
// If required, validate the uploaded picture. |
| 21 |
//Rename picture with unique id |
$validators = array( |
| 22 |
$dest = variable_get('user_picture_path', 'pictures') .'/picture-'. $user->uid . '-' . md5(time()) . '.' . $info['extension']; |
'file_validate_is_image' => array(), |
| 23 |
if (file_move($file->filepath, $dest)) { |
'file_validate_image_resolution' => array(variable_get('user_picture_dimensions', '85x85')), |
| 24 |
|
'file_validate_size' => array(variable_get('user_picture_file_size', '30') * 1024), |
| 25 |
//Clean up existing picture |
); |
| 26 |
|
if ($file = file_save_upload('picture_upload', $validators)) { |
| 27 |
|
// The image was saved using file_save_upload() and was added to the |
| 28 |
|
// files table as a temporary file. We'll make a copy and let the garbage |
| 29 |
|
// collector delete the original upload. |
| 30 |
|
$info = image_get_info($file->filepath); |
| 31 |
|
$destination = variable_get('user_picture_path', 'pictures') .'/picture-'. $form['#uid'] . '-' . md5(time()) . '.' . $info['extension']; |
| 32 |
|
if (file_copy($file, $destination, FILE_EXISTS_REPLACE)) { |
| 33 |
|
$form_state['values']['picture'] = $file->filepath; |
| 34 |
|
// Remove the old picture. |
| 35 |
|
if (isset($form_state['values']['_account']->picture)) { |
| 36 |
if (module_exists('imagecache')) { |
if (module_exists('imagecache')) { |
| 37 |
imagecache_image_flush($user->picture); |
imagecache_image_flush($form_state['values']['_account']->picture); |
| 38 |
|
} |
| 39 |
|
if (file_exists($form_state['values']['_account']->picture)) { |
| 40 |
|
file_delete($form_state['values']['_account']->picture); |
| 41 |
} |
} |
|
file_delete($user->picture); |
|
|
|
|
|
//Assign new picture |
|
|
$edit['picture'] = file_directory_path() . '/' . $dest; |
|
|
$user->picture = file_directory_path() . '/' . $dest; |
|
| 42 |
} |
} |
| 43 |
} |
} |
| 44 |
|
else { |
| 45 |
|
form_set_error('picture_upload', t("Failed to upload the picture image; the %directory directory doesn't exist or is not writable.", array('%directory' => variable_get('user_picture_path', 'pictures')))); |
| 46 |
|
} |
| 47 |
} |
} |
| 48 |
} |
} |