Avatar uploading scripting for PHPBB 3.3

Need some custom code changes to the phpBB core simple enough that you feel doesn't require an extension? Then post your request here so that community members can provide some assistance.

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Post Reply
davyg
Registered User
Posts: 6
Joined: Tue Apr 03, 2018 5:50 pm

Avatar uploading scripting for PHPBB 3.3

Post by davyg »

Hello guys :)

I found this script to fetch all external avatars from my forum and convert them to local uploads:
https://gist.github.com/tierra/0024360d130d26660fa5

(my board runs https and remote http avatars generate mixed content errors in visitor browsers)

This 3.1 script uses functions from includes/functions_upload.php, but this has been removed in phpbb3.3

This phpbb documentation explains the removal of functions_upload, but unfortunately I'm having trouble adjusting the script with this info.
https://area51.phpbb.com/docs/dev/3.3.x ... rview.html
My knowledge of phpbb coding is quite basic :roll:

Any suggestions on fixing this? Or would require an extensive rewrite of the code?
Any examples of other 3.3 scripts using similar logic could also be helpfull to solve this.

Thanks for the help!

Script:

Code: Select all

if (php_sapi_name() != 'cli')
{
	echo 'This program must be run from the command line.' . PHP_EOL;
	exit(1);
}

define('IN_PHPBB', true);
$phpbb_root_path = __DIR__ . '/';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
require($phpbb_root_path . 'includes/startup.' . $phpEx);
require($phpbb_root_path . 'phpbb/class_loader.' . $phpEx);

$phpbb_class_loader = new \phpbb\class_loader('phpbb\\', "{$phpbb_root_path}phpbb/", $phpEx);
$phpbb_class_loader->register();

$phpbb_config_php_file = new \phpbb\config_php_file($phpbb_root_path, $phpEx);
extract($phpbb_config_php_file->get_all());

require($phpbb_root_path . 'includes/constants.' . $phpEx);
require($phpbb_root_path . 'includes/functions.' . $phpEx);
require($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
require($phpbb_root_path . 'includes/functions_upload.' . $phpEx);
require($phpbb_root_path . 'includes/utf/utf_tools.' . $phpEx);

$phpbb_container_builder = new \phpbb\di\container_builder($phpbb_config_php_file, $phpbb_root_path, $phpEx);
$phpbb_container_builder->set_dump_container(false);
$phpbb_container_builder->set_use_extensions(false);

$phpbb_container = $phpbb_container_builder->get_container();
$phpbb_container->get('request')->enable_super_globals();
require($phpbb_root_path . 'includes/compatibility_globals.' . $phpEx);

/** @var \phpbb\config\config $config */
$config = $phpbb_container->get('config');

/** @var \phpbb\db\driver\factory $db */
$db = $phpbb_container->get('dbal.conn');

/** @var \phpbb\path_helper $path_helper */
$path_helper = $phpbb_container->get('path_helper');

/** @var \phpbb\mimetype\guesser $mimetype_guesser */
$mimetype_guesser = $phpbb_container->get('mimetype.guesser');

$db->sql_query("
	SELECT `user_id`, `user_avatar` FROM " . USERS_TABLE . "
	WHERE `user_avatar_type` = 'avatar.driver.remote'
");
$remote_avatars = $db->sql_fetchrowset();

foreach ($remote_avatars as $avatar)
{
	echo sprintf("ID: %7d URL: %s\n", $avatar['user_id'], $avatar['user_avatar']);

$upload = new \fileupload('AVATAR_',
		array('gif', 'jpg', 'jpeg', 'png'), $config['avatar_filesize'],
		$config['avatar_min_width'], $config['avatar_min_height'],
		$config['avatar_max_width'], $config['avatar_max_height'],
		(isset($config['mime_triggers']) ? explode('|', $config['mime_triggers']) : false)
);

	/** @var \filespec $file */
	$file = $upload->remote_upload($avatar['user_avatar'], $mimetype_guesser);
	$prefix = $config['avatar_salt'] . '_';
	$file->clean_filename('avatar', $prefix, $avatar['user_id']);

	$destination = $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 (!empty($file->error))
	{
		$file->remove();
		echo sprintf("Error (skipped): %s\n\n", $file->error[0]);
		continue;
	}

	$update_data = array(
		'user_avatar' => $avatar['user_id'] . '_' . time() . '.' . $file->get('extension'),
		'user_avatar_type' => 'avatar.driver.upload',
		'user_avatar_width' => $file->get('width'),
		'user_avatar_height' => $file->get('height'),
	);
	$db->sql_query("
		UPDATE " . USERS_TABLE . "
		SET " . $db->sql_build_array('UPDATE', $update_data) . "
		WHERE `user_id` = " . (int) $avatar['user_id']
	);
	if ($db->get_sql_error_triggered())
	{
		$file->remove();
		print_r($db->get_sql_error_returned());
		die();
	}
}
Upload snippets:

Code: Select all

require($phpbb_root_path . 'includes/functions_upload.' . $phpEx);

Code: Select all

$upload = new \fileupload('AVATAR_',
		array('gif', 'jpg', 'jpeg', 'png'), $config['avatar_filesize'],
		$config['avatar_min_width'], $config['avatar_min_height'],
		$config['avatar_max_width'], $config['avatar_max_height'],
		(isset($config['mime_triggers']) ? explode('|', $config['mime_triggers']) : false)
);
Post Reply

Return to “phpBB Custom Coding”