Skip to content

Instantly share code, notes, and snippets.

@3D-I
Forked from tierra/convert-avatars.php
Created October 20, 2017 21:52
Show Gist options
  • Save 3D-I/471293eeacd744c5a9974c8d19e2358d to your computer and use it in GitHub Desktop.
Save 3D-I/471293eeacd744c5a9974c8d19e2358d to your computer and use it in GitHub Desktop.
phpBB 3.1 script that fetches all remote avatars, and converts them to uploaded avatars hosted locally.
<?php
/**
* Converts all phpBB 3.1 remote avatars to uploaded avatars.
*
* To use this script:
*
* 1. Copy this file into the root of your phpBB installation.
* 2. Run the script from the command line: php convert-avatars.php
*
* Any avatar will be skipped if any errors come up, like these:
* - DNS Lookup Failed (domain gone)
* - Failed to connect to server
* - File not found (404)
* - Redirects to HTML pages (not images).
* - Invalid image size (these were valid for some forums in the past).
*
* If, after running this script, you'd just like to delete all of the
* remaining remote avatars (that were broken in some way listed above),
* you can run this SQL query:
*
* UPDATE phpbb_users SET user_avatar = '', user_avatar_type = '',
* user_avatar_width = 0, user_avatar_height = 0
* WHERE user_avatar_type = 'avatar.driver.remote'
*
* @copyright (c) Bryan Petty
* @license GNU General Public License, version 2 (GPL-2.0)
*/
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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment