[feature/avatars] Implement avatar uploads for ucp

As above, implement avatar uploads from local and remote sources in
the UCP.

PHPBB3-10018
This commit is contained in:
Cullen Walsh 2011-04-19 11:19:47 -07:00 committed by Cullen Walsh
parent f02f621686
commit 019b9bc073
3 changed files with 82 additions and 41 deletions

View file

@ -58,43 +58,41 @@ class phpbb_avatar_driver_local extends phpbb_avatar_driver
$dh = @opendir($path); $dh = @opendir($path);
if (!$dh) if ($dh)
{ {
return $avatar_list; while (($cat = readdir($dh)) !== false) {
} if ($cat[0] != '.' && preg_match('#^[^&"\'<>]+$#i', $cat) && is_dir("$path/$cat"))
while (($cat = readdir($dh)) !== false) {
if ($cat[0] != '.' && preg_match('#^[^&"\'<>]+$#i', $cat) && is_dir("$path/$cat"))
{
if ($ch = @opendir("$path/$cat"))
{ {
while (($image = readdir($ch)) !== false) if ($ch = @opendir("$path/$cat"))
{ {
// Match all images in the gallery folder while (($image = readdir($ch)) !== false)
if (preg_match('#^[^&\'"<>]+\.(?:gif|png|jpe?g)$#i', $image))
{ {
if (function_exists('getimagesize')) // Match all images in the gallery folder
if (preg_match('#^[^&\'"<>]+\.(?:gif|png|jpe?g)$#i', $image))
{ {
$dims = getimagesize($this->phpbb_root_path . $this->config['avatar_gallery_path'] . '/' . $cat . '/' . $image); if (function_exists('getimagesize'))
{
$dims = getimagesize($this->phpbb_root_path . $this->config['avatar_gallery_path'] . '/' . $cat . '/' . $image);
}
else
{
$dims = array(0, 0);
}
$avatar_list[$cat][$image] = array(
'file' => rawurlencode($cat) . '/' . rawurlencode($image),
'filename' => rawurlencode($image),
'name' => ucfirst(str_replace('_', ' ', preg_replace('#^(.*)\..*$#', '\1', $image))),
'width' => $dims[0],
'height' => $dims[1],
);
} }
else
{
$dims = array(0, 0);
}
$avatar_list[$cat][$image] = array(
'file' => rawurlencode($cat) . '/' . rawurlencode($image),
'filename' => rawurlencode($image),
'name' => ucfirst(str_replace('_', ' ', preg_replace('#^(.*)\..*$#', '\1', $image))),
'width' => $dims[0],
'height' => $dims[1],
);
} }
@closedir($ch);
} }
@closedir($ch);
} }
} }
@closedir($dh);
} }
@closedir($dh);
@ksort($avatar_list); @ksort($avatar_list);

View file

@ -49,26 +49,69 @@ class phpbb_avatar_driver_upload extends phpbb_avatar_driver
*/ */
public function handle_form($template, $user_row, &$error, $submitted = false) public function handle_form($template, $user_row, &$error, $submitted = false)
{ {
if ($submitted) { $can_upload = (file_exists($this->phpbb_root_path . $this->config['avatar_path']) && phpbb_is_writable($this->phpbb_root_path . $this->config['avatar_path']) && (@ini_get('file_uploads') || strtolower(@ini_get('file_uploads')) == 'on')) ? true : false;
$error[] = 'TODO';
return '';
}
else
{
$can_upload = (file_exists($this->phpbb_root_path . $this->config['avatar_path']) && phpbb_is_writable($this->phpbb_root_path . $this->config['avatar_path']) && (@ini_get('file_uploads') || strtolower(@ini_get('file_uploads')) == 'on')) ? true : false;
if ($can_upload)
{
$template->assign_vars(array(
'S_UPLOAD_AVATAR_URL' => ($this->config['allow_avatar_remote_upload']) ? true : false,
'AV_UPLOAD_SIZE' => $this->config['avatar_filesize'],
));
return true; if ($can_upload == false)
{
return false;
}
if ($submitted)
{
include_once($this->phpbb_root_path . 'includes/functions_upload.' . $this->phpEx);
$upload = new fileupload('AVATAR_', array('jpg', 'jpeg', 'gif', 'png'), $this->config['avatar_filesize'], $this->config['avatar_min_width'], $this->config['avatar_min_height'], $this->config['avatar_max_width'], $this->config['avatar_max_height'], (isset($this->config['mime_triggers']) ? explode('|', $this->config['mime_triggers']) : false));
$url = request_var('av_upload_url', '');
if (!empty($_FILES['av_upload_file']['name']))
{
$file = $upload->form_upload('av_upload_file');
} }
else else
{ {
$file = $upload->remote_upload($url);
}
$prefix = $this->config['avatar_salt'] . '_';
$file->clean_filename('avatar', $prefix, $user_row['user_id']);
$destination = $this->config['avatar_path'];
// Adjust destination path (no trailing slash)
if (substr($destination, -1, 1) == '/' || substr($destination, -1, 1) == '\\')
{
$destination = substr($destination, 0, -1);
}
$destination = str_replace(array('../', '..\\', './', '.\\'), '', $destination);
if ($destination && ($destination[0] == '/' || $destination[0] == "\\"))
{
$destination = '';
}
// Move file and overwrite any existing image
$file->move_file($destination, true);
if (sizeof($file->error))
{
$file->remove();
$error = array_merge($error, $file->error);
return false; return false;
} }
return array(
'user_avatar' => $user_row['user_id'] . '_' . time() . '.' . $file->get('extension'),
'user_avatar_width' => $file->get('width'),
'user_avatar_height' => $file->get('height'),
);
} }
$template->assign_vars(array(
'S_UPLOAD_AVATAR_URL' => ($this->config['allow_avatar_remote_upload']) ? true : false,
'AV_UPLOAD_SIZE' => $this->config['avatar_filesize'],
));
return true;
} }
} }

View file

@ -570,7 +570,7 @@ class ucp_profile
{ {
$result = $avatar->handle_form($template, $user->data, $error, true); $result = $avatar->handle_form($template, $user->data, $error, true);
if (empty($error)) if ($result && empty($error))
{ {
// Success! Lets save the result in the database // Success! Lets save the result in the database
$result['user_avatar_type'] = $driver; $result['user_avatar_type'] = $driver;