2011-04-17 19:29:41 -07:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @package avatar
|
2011-04-18 10:44:29 -07:00
|
|
|
* @copyright (c) 2011 phpBB Group
|
2011-04-17 19:29:41 -07:00
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
if (!defined('IN_PHPBB'))
|
|
|
|
{
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-04-18 10:44:29 -07:00
|
|
|
* @package avatar
|
2011-04-17 19:29:41 -07:00
|
|
|
*/
|
|
|
|
class phpbb_avatar_manager
|
|
|
|
{
|
2012-11-19 23:50:34 +01:00
|
|
|
protected $phpbb_root_path;
|
|
|
|
protected $phpEx;
|
|
|
|
protected $config;
|
|
|
|
protected $request;
|
|
|
|
protected $cache;
|
|
|
|
protected static $valid_drivers = false;
|
|
|
|
protected $avatar_drivers;
|
|
|
|
protected $container;
|
2011-04-17 19:29:41 -07:00
|
|
|
|
2011-04-17 21:58:51 -07:00
|
|
|
/**
|
2012-11-19 00:30:18 +01:00
|
|
|
* Construct an avatar manager object
|
|
|
|
*
|
2012-11-25 21:14:05 +01:00
|
|
|
* @param string $phpbb_root_path Path to the phpBB root
|
|
|
|
* @param string $phpEx PHP file extension
|
|
|
|
* @param phpbb_config $config phpBB configuration
|
|
|
|
* @param phpbb_request $request Request object
|
|
|
|
* @param phpbb_cache_driver_interface $cache Cache driver
|
|
|
|
* @param array $avatar_drivers Avatar drivers passed via the service container
|
|
|
|
* @param object $container Container object
|
|
|
|
*/
|
2012-11-19 00:30:18 +01:00
|
|
|
public function __construct($phpbb_root_path, $phpEx, phpbb_config $config, phpbb_request $request, phpbb_cache_driver_interface $cache, $avatar_drivers, $container)
|
2011-04-17 19:29:41 -07:00
|
|
|
{
|
|
|
|
$this->phpbb_root_path = $phpbb_root_path;
|
2011-04-18 10:44:29 -07:00
|
|
|
$this->phpEx = $phpEx;
|
2011-04-17 19:29:41 -07:00
|
|
|
$this->config = $config;
|
2012-04-07 19:19:13 +02:00
|
|
|
$this->request = $request;
|
2011-04-17 19:29:41 -07:00
|
|
|
$this->cache = $cache;
|
2012-11-19 00:30:18 +01:00
|
|
|
$this->avatar_drivers = $avatar_drivers;
|
2012-11-14 23:14:41 +01:00
|
|
|
$this->container = $container;
|
2011-04-17 19:29:41 -07:00
|
|
|
}
|
|
|
|
|
2011-04-17 21:58:51 -07:00
|
|
|
/**
|
2012-11-19 00:30:18 +01:00
|
|
|
* Get the driver object specified by the avatar type
|
|
|
|
*
|
2012-11-30 15:12:34 +01:00
|
|
|
* @param string $avatar_type Avatar type; by default an avatar's service container name
|
|
|
|
* @param bool $force_all Grab all avatar drivers, no matter if enabled or not
|
2012-11-19 00:30:18 +01:00
|
|
|
*
|
2012-11-25 21:14:05 +01:00
|
|
|
* @return object Avatar driver object
|
|
|
|
*/
|
2012-11-30 15:12:34 +01:00
|
|
|
public function get_driver($avatar_type, $force_all = false)
|
2011-04-17 19:29:41 -07:00
|
|
|
{
|
2012-11-30 15:12:34 +01:00
|
|
|
if (self::$valid_drivers === false || $force_all)
|
2011-04-17 19:29:41 -07:00
|
|
|
{
|
2012-11-30 15:12:34 +01:00
|
|
|
$this->load_valid_drivers($force_all);
|
2011-04-17 19:29:41 -07:00
|
|
|
}
|
|
|
|
|
2011-04-20 23:14:38 -07:00
|
|
|
// Legacy stuff...
|
|
|
|
switch ($avatar_type)
|
|
|
|
{
|
2011-06-15 12:38:17 -07:00
|
|
|
case AVATAR_GALLERY:
|
2012-11-16 17:19:04 +01:00
|
|
|
$avatar_type = 'avatar.driver.local';
|
2012-11-21 17:24:02 +01:00
|
|
|
break;
|
2011-04-20 23:14:38 -07:00
|
|
|
case AVATAR_UPLOAD:
|
2012-11-16 17:19:04 +01:00
|
|
|
$avatar_type = 'avatar.driver.upload';
|
2012-11-21 17:24:02 +01:00
|
|
|
break;
|
2011-04-20 23:14:38 -07:00
|
|
|
case AVATAR_REMOTE:
|
2012-11-16 17:19:04 +01:00
|
|
|
$avatar_type = 'avatar.driver.remote';
|
2012-11-21 17:24:02 +01:00
|
|
|
break;
|
2011-04-20 23:14:38 -07:00
|
|
|
}
|
|
|
|
|
2012-11-21 19:20:39 +01:00
|
|
|
if (!isset(self::$valid_drivers[$avatar_type]))
|
2011-04-17 19:29:41 -07:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2012-06-27 21:02:07 +02:00
|
|
|
|
2012-12-01 00:27:52 +01:00
|
|
|
/*
|
|
|
|
* There is no need to handle invalid avatar types as the following code
|
|
|
|
* will cause a ServiceNotFoundException if the type does not exist
|
|
|
|
*/
|
2012-11-14 23:14:41 +01:00
|
|
|
$driver = $this->container->get($avatar_type);
|
2012-06-27 21:02:07 +02:00
|
|
|
|
|
|
|
return $driver;
|
2011-04-17 19:29:41 -07:00
|
|
|
}
|
|
|
|
|
2011-04-17 21:58:51 -07:00
|
|
|
/**
|
2012-11-19 00:30:18 +01:00
|
|
|
* Load the list of valid drivers
|
|
|
|
* This is executed once and fills self::$valid_drivers
|
2012-11-29 23:50:17 +01:00
|
|
|
*
|
|
|
|
* @param bool $force_all Force showing all avatar drivers
|
2012-11-25 21:14:05 +01:00
|
|
|
*/
|
2012-11-29 23:50:17 +01:00
|
|
|
protected function load_valid_drivers($force_all = false)
|
2011-04-17 19:29:41 -07:00
|
|
|
{
|
2012-11-19 00:30:18 +01:00
|
|
|
if (!empty($this->avatar_drivers))
|
2011-04-17 19:29:41 -07:00
|
|
|
{
|
|
|
|
self::$valid_drivers = array();
|
2012-11-19 00:30:18 +01:00
|
|
|
foreach ($this->avatar_drivers as $driver)
|
2011-04-17 19:29:41 -07:00
|
|
|
{
|
2012-11-29 23:50:17 +01:00
|
|
|
if ($force_all || $this->is_enabled($driver))
|
|
|
|
{
|
|
|
|
self::$valid_drivers[$driver->get_name()] = $driver->get_name();
|
|
|
|
}
|
2011-04-17 19:29:41 -07:00
|
|
|
}
|
2012-11-30 16:46:11 +01:00
|
|
|
asort(self::$valid_drivers);
|
2011-04-17 19:29:41 -07:00
|
|
|
}
|
|
|
|
}
|
2011-04-17 21:58:51 -07:00
|
|
|
|
|
|
|
/**
|
2012-11-19 00:30:18 +01:00
|
|
|
* Get a list of valid avatar drivers
|
|
|
|
*
|
2012-11-29 23:50:17 +01:00
|
|
|
* @param bool $force_all Force showing all avatar drivers
|
|
|
|
*
|
2012-11-25 21:14:05 +01:00
|
|
|
* @return array Array containing a list of the valid avatar drivers
|
|
|
|
*/
|
2012-11-29 23:50:17 +01:00
|
|
|
public function get_valid_drivers($force_all = false)
|
2012-04-07 20:27:11 +02:00
|
|
|
{
|
2011-04-17 21:58:51 -07:00
|
|
|
if (self::$valid_drivers === false)
|
|
|
|
{
|
2012-11-29 23:50:17 +01:00
|
|
|
$this->load_valid_drivers($force_all);
|
2011-04-17 21:58:51 -07:00
|
|
|
}
|
|
|
|
|
2012-06-27 21:02:07 +02:00
|
|
|
return self::$valid_drivers;
|
2011-04-17 21:58:51 -07:00
|
|
|
}
|
2012-04-08 21:29:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Strip out user_ and group_ prefixes from keys
|
2012-11-19 00:30:18 +01:00
|
|
|
*
|
2012-11-25 21:14:05 +01:00
|
|
|
* @param array $row User data or group data
|
2012-11-19 00:30:18 +01:00
|
|
|
*
|
2012-11-25 21:14:05 +01:00
|
|
|
* @return array User data or group data with keys that have been
|
2012-11-19 00:30:18 +01:00
|
|
|
* stripped from the preceding "user_" or "group_"
|
2012-11-25 21:14:05 +01:00
|
|
|
*/
|
2012-04-08 21:29:52 +02:00
|
|
|
public static function clean_row($row)
|
|
|
|
{
|
|
|
|
$keys = array_keys($row);
|
|
|
|
$values = array_values($row);
|
|
|
|
|
|
|
|
$keys = array_map(
|
|
|
|
function ($key)
|
|
|
|
{
|
2012-04-08 22:28:40 +02:00
|
|
|
return preg_replace('#^(?:user_|group_)#', '', $key);
|
2012-04-08 21:29:52 +02:00
|
|
|
},
|
2012-06-27 14:48:51 +02:00
|
|
|
$keys
|
2012-04-08 21:29:52 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return array_combine($keys, $values);
|
|
|
|
}
|
2012-11-25 16:04:59 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean driver names that are returned from template files
|
|
|
|
* Underscores are replaced with dots
|
|
|
|
*
|
|
|
|
* @param string $name Driver name
|
|
|
|
*
|
|
|
|
* @return string Cleaned driver name
|
|
|
|
*/
|
|
|
|
public static function clean_driver_name($name)
|
|
|
|
{
|
|
|
|
return str_replace('_', '.', $name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepare driver names for use in template files
|
|
|
|
* Dots are replaced with underscores
|
|
|
|
*
|
|
|
|
* @param string $name Clean driver name
|
|
|
|
*
|
|
|
|
* @return string Prepared driver name
|
|
|
|
*/
|
|
|
|
public static function prepare_driver_name($name)
|
|
|
|
{
|
|
|
|
return str_replace('.', '_', $name);
|
|
|
|
}
|
2012-11-29 23:50:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if avatar is enabled
|
|
|
|
*
|
|
|
|
* @param object $driver Avatar driver object
|
|
|
|
*
|
|
|
|
* @return bool True if avatar is enabled, false if it's disabled
|
|
|
|
*/
|
|
|
|
public function is_enabled($driver)
|
|
|
|
{
|
|
|
|
$config_name = preg_replace('#^phpbb_avatar_driver_#', '', get_class($driver));
|
|
|
|
|
|
|
|
return $this->config["allow_avatar_{$config_name}"];
|
|
|
|
}
|
2012-11-30 23:11:44 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the settings array for enabling/disabling an avatar driver
|
|
|
|
*
|
|
|
|
* @param string $driver Avatar driver object
|
|
|
|
*
|
|
|
|
* @return array Array of configuration options as consumed by acp_board
|
|
|
|
*/
|
|
|
|
public function get_avatar_settings($driver)
|
|
|
|
{
|
|
|
|
$config_name = preg_replace('#^phpbb_avatar_driver_#', '', get_class($driver));
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'allow_avatar_' . $config_name => array('lang' => 'ALLOW_' . strtoupper($config_name), 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => false),
|
|
|
|
);
|
|
|
|
}
|
2011-04-17 19:29:41 -07:00
|
|
|
}
|