From f102d9a631d6de464abefe2089ff1e6e13ed044d Mon Sep 17 00:00:00 2001 From: Cullen Walsh Date: Mon, 18 Apr 2011 10:44:29 -0700 Subject: [PATCH] [feature/avatars] Various cosmetic changes Various small changes based on feedback from nn- * Renaming $php_ext to $phpEx * Fixing copyright years * Explain $ignore_config * Explain Regex * Copypasta package error * rename get_singleton PHPBB3-10018 --- phpBB/includes/avatar/driver.php | 32 ++++++++---- phpBB/includes/avatar/driver/local.php | 65 ++++++++++++++----------- phpBB/includes/avatar/driver/remote.php | 12 ++--- phpBB/includes/avatar/driver/upload.php | 14 ++---- phpBB/includes/avatar/manager.php | 21 ++++---- phpBB/includes/functions_display.php | 2 +- phpBB/includes/ucp/ucp_profile.php | 2 +- 7 files changed, 80 insertions(+), 68 deletions(-) diff --git a/phpBB/includes/avatar/driver.php b/phpBB/includes/avatar/driver.php index 016f9e94a8..60ec18670e 100644 --- a/phpBB/includes/avatar/driver.php +++ b/phpBB/includes/avatar/driver.php @@ -2,7 +2,7 @@ /** * * @package avatar -* @copyright (c) 2005, 2009 phpBB Group +* @copyright (c) 2011 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * */ @@ -37,7 +37,13 @@ abstract class phpbb_avatar_driver * Current $phpEx * @type string */ - protected $php_ext; + protected $phpEx; + + /** + * A cache driver + * @type phpbb_cache_driver_interface + */ + protected $cache; /** * This flag should be set to true if the avatar requires a nonstandard image @@ -47,22 +53,27 @@ abstract class phpbb_avatar_driver public $custom_html = false; /** - * Construct an avatar object + * Construct an driver object * - * @param $user_row User data to base the avatar url/html on + * @param $config The phpBB configuration + * @param $phpbb_root_path The path to the phpBB root + * @param $phpEx The php file extension + * @param $cache A cache driver */ - public function __construct(phpbb_config $config, $phpbb_root_path, $php_ext) + public function __construct(phpbb_config $config, $phpbb_root_path, $phpEx, phpbb_cache_driver_interface $cache = null) { $this->config = $config; $this->phpbb_root_path = $phpbb_root_path; - $this->php_ext = $php_ext; + $this->phpEx = $phpEx; + $this->cache = $cache; } /** * Get the avatar url and dimensions * - * @param $ignore_config Whether $user or global avatar visibility settings - * should be ignored + * @param $ignore_config Whether this function should respect the users/board + * configuration option, or should just render the avatar anyways. + * Useful for the ACP. * @return array Avatar data */ public function get_data($user_row, $ignore_config = false) @@ -78,8 +89,9 @@ abstract class phpbb_avatar_driver * Returns custom html for displaying this avatar. * Only called if $custom_html is true. * - * @param $ignore_config Whether $user or global avatar visibility settings - * should be ignored + * @param $ignore_config Whether this function should respect the users/board + * configuration option, or should just render the avatar anyways. + * Useful for the ACP. * @return string HTML */ public function get_custom_html($user_row, $ignore_config = false) diff --git a/phpBB/includes/avatar/driver/local.php b/phpBB/includes/avatar/driver/local.php index 0a3ae51a48..65340c92ce 100644 --- a/phpBB/includes/avatar/driver/local.php +++ b/phpBB/includes/avatar/driver/local.php @@ -2,7 +2,7 @@ /** * * @package avatar -* @copyright (c) 2005, 2009 phpBB Group +* @copyright (c) 2011 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * */ @@ -22,11 +22,7 @@ if (!defined('IN_PHPBB')) class phpbb_avatar_driver_local extends phpbb_avatar_driver { /** - * Get the avatar url and dimensions - * - * @param $ignore_config Whether $user or global avatar visibility settings - * should be ignored - * @return array Avatar data + * @inheritdoc */ public function get_data($user_row, $ignore_config = false) { @@ -49,8 +45,8 @@ class phpbb_avatar_driver_local extends phpbb_avatar_driver } /** - * @TODO - **/ + * @inheritdoc + */ public function handle_form($template, &$error = array(), $submitted = false) { if ($submitted) { @@ -58,39 +54,50 @@ class phpbb_avatar_driver_local extends phpbb_avatar_driver return ''; } - $avatar_list = array(); - $path = $this->phpbb_root_path . $this->config['avatar_gallery_path']; + $avatar_list = ($this->cache == null) ? false : $this->cache->get('av_local_list'); - $dh = @opendir($path); - - if (!$dh) + if (!$avatar_list) { - return $avatar_list; - } + $avatar_list = array(); + $path = $this->phpbb_root_path . $this->config['avatar_gallery_path']; - while (($cat = readdir($dh)) !== false) { - if ($cat[0] != '.' && preg_match('#^[^&"\'<>]+$#i', $cat) && is_dir("$path/$cat")) + $dh = @opendir($path); + + if (!$dh) { - if ($ch = @opendir("$path/$cat")) + return $avatar_list; + } + + while (($cat = readdir($dh)) !== false) { + if ($cat[0] != '.' && preg_match('#^[^&"\'<>]+$#i', $cat) && is_dir("$path/$cat")) { - while (($image = readdir($ch)) !== false) + if ($ch = @opendir("$path/$cat")) { - if (preg_match('#^[^&\'"<>]+\.(?:gif|png|jpe?g)$#i', $image)) + while (($image = readdir($ch)) !== false) { - $avatar_list[$cat][] = array( - 'file' => rawurlencode($cat) . '/' . rawurlencode($image), - 'filename' => rawurlencode($image), - 'name' => ucfirst(str_replace('_', ' ', preg_replace('#^(.*)\..*$#', '\1', $image))), - ); + // Match all images in the gallery folder + if (preg_match('#^[^&\'"<>]+\.(?:gif|png|jpe?g)$#i', $image)) + { + $avatar_list[$cat][] = array( + 'file' => rawurlencode($cat) . '/' . rawurlencode($image), + 'filename' => rawurlencode($image), + 'name' => ucfirst(str_replace('_', ' ', preg_replace('#^(.*)\..*$#', '\1', $image))), + ); + } } + @closedir($ch); } - @closedir($ch); } } - } - @closedir($dh); + @closedir($dh); - @ksort($avatar_list); + @ksort($avatar_list); + + if ($this->cache != null) + { + $this->cache->put('av_local_list', $avatar_list); + } + } $category = request_var('av_local_cat', ''); $categories = array_keys($avatar_list); diff --git a/phpBB/includes/avatar/driver/remote.php b/phpBB/includes/avatar/driver/remote.php index c60102e787..bfc1be263f 100644 --- a/phpBB/includes/avatar/driver/remote.php +++ b/phpBB/includes/avatar/driver/remote.php @@ -2,7 +2,7 @@ /** * * @package avatar -* @copyright (c) 2005, 2009 phpBB Group +* @copyright (c) 2011 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * */ @@ -22,11 +22,7 @@ if (!defined('IN_PHPBB')) class phpbb_avatar_driver_remote extends phpbb_avatar_driver { /** - * Get the avatar url and dimensions - * - * @param $ignore_config Whether $user or global avatar visibility settings - * should be ignored - * @return array Avatar data + * @inheritdoc */ public function get_data($user_row, $ignore_config = false) { @@ -49,8 +45,8 @@ class phpbb_avatar_driver_remote extends phpbb_avatar_driver } /** - * @TODO - **/ + * @inheritdoc + */ public function handle_form($template, &$error = array(), $submitted = false) { if ($submitted) { diff --git a/phpBB/includes/avatar/driver/upload.php b/phpBB/includes/avatar/driver/upload.php index fbfd5dcc89..9705a888b6 100644 --- a/phpBB/includes/avatar/driver/upload.php +++ b/phpBB/includes/avatar/driver/upload.php @@ -2,7 +2,7 @@ /** * * @package avatar -* @copyright (c) 2005, 2009 phpBB Group +* @copyright (c) 2011 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * */ @@ -22,18 +22,14 @@ if (!defined('IN_PHPBB')) class phpbb_avatar_driver_upload extends phpbb_avatar_driver { /** - * Get the avatar url and dimensions - * - * @param $ignore_config Whether $user or global avatar visibility settings - * should be ignored - * @return array Avatar data + * @inheritdoc */ public function get_data($user_row, $ignore_config = false) { if ($ignore_config || $this->config['allow_avatar_upload']) { return array( - 'src' => $this->phpbb_root_path . 'download/file.' . $this->php_ext . '?avatar=' . $user_row['user_avatar'], + 'src' => $this->phpbb_root_path . 'download/file.' . $this->phpEx . '?avatar=' . $user_row['user_avatar'], 'width' => $user_row['user_avatar_width'], 'height' => $user_row['user_avatar_height'], ); @@ -49,8 +45,8 @@ class phpbb_avatar_driver_upload extends phpbb_avatar_driver } /** - * @TODO - **/ + * @inheritdoc + */ public function handle_form($template, &$error = array(), $submitted = false) { if ($submitted) { diff --git a/phpBB/includes/avatar/manager.php b/phpBB/includes/avatar/manager.php index 11b7e75017..6471c4cc9c 100644 --- a/phpBB/includes/avatar/manager.php +++ b/phpBB/includes/avatar/manager.php @@ -2,7 +2,7 @@ /** * * @package avatar -* @copyright (c) 2010 phpBB Group +* @copyright (c) 2011 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * */ @@ -16,12 +16,12 @@ if (!defined('IN_PHPBB')) } /** -* @package acm +* @package avatar */ class phpbb_avatar_manager { private $phpbb_root_path; - private $php_ext; + private $phpEx; private $config; private $cache; private static $valid_drivers = false; @@ -29,10 +29,10 @@ class phpbb_avatar_manager /** * @TODO **/ - public function __construct($phpbb_root_path, $php_ext = '.php', phpbb_config $config, phpbb_cache_driver_interface $cache = null) + public function __construct($phpbb_root_path, $phpEx, phpbb_config $config, phpbb_cache_driver_interface $cache = null) { $this->phpbb_root_path = $phpbb_root_path; - $this->php_ext = $php_ext; + $this->phpEx = $phpEx; $this->config = $config; $this->cache = $cache; } @@ -40,7 +40,7 @@ class phpbb_avatar_manager /** * @TODO **/ - public function get_singleton($avatar_type) + public function get_driver($avatar_type, $new = false) { if (self::$valid_drivers === false) { @@ -49,10 +49,10 @@ class phpbb_avatar_manager if (isset(self::$valid_drivers[$avatar_type])) { - if (!is_object(self::$valid_drivers[$avatar_type])) + if ($new || !is_object(self::$valid_drivers[$avatar_type])) { $class_name = 'phpbb_avatar_driver_' . $avatar_type; - self::$valid_drivers[$avatar_type] = new $class_name($this->config, $this->phpbb_root_path, $this->php_ext); + self::$valid_drivers[$avatar_type] = new $class_name($this->config, $this->phpbb_root_path, $this->phpEx, $this->cache); } return self::$valid_drivers[$avatar_type]; @@ -68,7 +68,7 @@ class phpbb_avatar_manager **/ private function load_valid_drivers() { - require_once($this->phpbb_root_path . 'includes/avatar/driver.' . $this->php_ext); + require_once($this->phpbb_root_path . 'includes/avatar/driver.' . $this->phpEx); if ($this->cache) { @@ -83,7 +83,8 @@ class phpbb_avatar_manager foreach ($iterator as $file) { - if (preg_match("/^(.*)\.{$this->php_ext}$/", $file, $match)) + // Match all files that appear to be php files + if (preg_match("/^(.*)\.{$this->phpEx}$/", $file, $match)) { self::$valid_drivers[] = $match[1]; } diff --git a/phpBB/includes/functions_display.php b/phpBB/includes/functions_display.php index 3aeee5f704..23900dfd88 100644 --- a/phpBB/includes/functions_display.php +++ b/phpBB/includes/functions_display.php @@ -1341,7 +1341,7 @@ function get_user_avatar($user_row, $alt = 'USER_AVATAR', $ignore_config = false $avatar_manager = new phpbb_avatar_manager($phpbb_root_path, $phpEx, $config, $cache->get_driver()); } - $avatar = $avatar_manager->get_singleton($user_row['user_avatar_type']); + $avatar = $avatar_manager->get_driver($user_row['user_avatar_type']); if ($avatar) { diff --git a/phpBB/includes/ucp/ucp_profile.php b/phpBB/includes/ucp/ucp_profile.php index 0a2440d77d..a93430644c 100644 --- a/phpBB/includes/ucp/ucp_profile.php +++ b/phpBB/includes/ucp/ucp_profile.php @@ -567,7 +567,7 @@ class ucp_profile 'avatar' => "ucp_avatar_options_$driver.html", )); - $avatar = $avatar_manager->get_singleton($driver); + $avatar = $avatar_manager->get_driver($driver); if (isset($_POST["submit_av_$driver"])) { if (check_form_key('ucp_avatar'))