[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/phpbb/avatar/ -> manager.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb\avatar;
  15  
  16  class manager
  17  {
  18      /**
  19      * phpBB configuration
  20      * @var \phpbb\config\config
  21      */
  22      protected $config;
  23  
  24      /**
  25      * Array that contains a list of enabled drivers
  26      * @var array
  27      */
  28      static protected $enabled_drivers = false;
  29  
  30      /**
  31      * Array that contains all available avatar drivers which are passed via the
  32      * service container
  33      * @var array
  34      */
  35      protected $avatar_drivers;
  36  
  37      /**
  38      * Default avatar data row
  39      * @var array
  40      */
  41      static protected $default_row = array(
  42          'avatar'        => '',
  43          'avatar_type'    => '',
  44          'avatar_width'    => 0,
  45          'avatar_height'    => 0,
  46      );
  47  
  48      /**
  49      * Construct an avatar manager object
  50      *
  51      * @param \phpbb\config\config $config phpBB configuration
  52      * @param array $avatar_drivers Avatar drivers passed via the service container
  53      */
  54  	public function __construct(\phpbb\config\config $config, $avatar_drivers)
  55      {
  56          $this->config = $config;
  57          $this->register_avatar_drivers($avatar_drivers);
  58      }
  59  
  60      /**
  61      * Register avatar drivers
  62      *
  63      * @param array $avatar_drivers Service collection of avatar drivers
  64      */
  65  	protected function register_avatar_drivers($avatar_drivers)
  66      {
  67          if (!empty($avatar_drivers))
  68          {
  69              foreach ($avatar_drivers as $driver)
  70              {
  71                  $this->avatar_drivers[$driver->get_name()] = $driver;
  72              }
  73          }
  74      }
  75  
  76      /**
  77      * Get the driver object specified by the avatar type
  78      *
  79      * @param string $avatar_type Avatar type; by default an avatar's service container name
  80      * @param bool $load_enabled Load only enabled avatars
  81      *
  82      * @return object Avatar driver object
  83      */
  84  	public function get_driver($avatar_type, $load_enabled = true)
  85      {
  86          if (self::$enabled_drivers === false)
  87          {
  88              $this->load_enabled_drivers();
  89          }
  90  
  91          $avatar_drivers = ($load_enabled) ? self::$enabled_drivers : $this->get_all_drivers();
  92  
  93          // Legacy stuff...
  94          switch ($avatar_type)
  95          {
  96              case AVATAR_GALLERY:
  97                  $avatar_type = 'avatar.driver.local';
  98              break;
  99              case AVATAR_UPLOAD:
 100                  $avatar_type = 'avatar.driver.upload';
 101              break;
 102              case AVATAR_REMOTE:
 103                  $avatar_type = 'avatar.driver.remote';
 104              break;
 105          }
 106  
 107          if (!isset($avatar_drivers[$avatar_type]))
 108          {
 109              return null;
 110          }
 111  
 112          /*
 113          * There is no need to handle invalid avatar types as the following code
 114          * will cause a ServiceNotFoundException if the type does not exist
 115          */
 116          $driver = $this->avatar_drivers[$avatar_type];
 117  
 118          return $driver;
 119      }
 120  
 121      /**
 122      * Load the list of enabled drivers
 123      * This is executed once and fills self::$enabled_drivers
 124      */
 125  	protected function load_enabled_drivers()
 126      {
 127          if (!empty($this->avatar_drivers))
 128          {
 129              self::$enabled_drivers = array();
 130              foreach ($this->avatar_drivers as $driver)
 131              {
 132                  if ($this->is_enabled($driver))
 133                  {
 134                      self::$enabled_drivers[$driver->get_name()] = $driver->get_name();
 135                  }
 136              }
 137              asort(self::$enabled_drivers);
 138          }
 139      }
 140  
 141      /**
 142      * Get a list of all avatar drivers
 143      *
 144      * As this function will only be called in the ACP avatar settings page, it
 145      * doesn't make much sense to cache the list of all avatar drivers like the
 146      * list of the enabled drivers.
 147      *
 148      * @return array Array containing a list of all avatar drivers
 149      */
 150  	public function get_all_drivers()
 151      {
 152          $drivers = array();
 153  
 154          if (!empty($this->avatar_drivers))
 155          {
 156              foreach ($this->avatar_drivers as $driver)
 157              {
 158                  $drivers[$driver->get_name()] = $driver->get_name();
 159              }
 160              asort($drivers);
 161          }
 162  
 163          return $drivers;
 164      }
 165  
 166      /**
 167      * Get a list of enabled avatar drivers
 168      *
 169      * @return array Array containing a list of the enabled avatar drivers
 170      */
 171  	public function get_enabled_drivers()
 172      {
 173          if (self::$enabled_drivers === false)
 174          {
 175              $this->load_enabled_drivers();
 176          }
 177  
 178          return self::$enabled_drivers;
 179      }
 180  
 181      /**
 182      * Strip out user_, group_, or other prefixes from array keys
 183      *
 184      * @param array    $row            User data or group data
 185      * @param string $prefix            Prefix of data keys (e.g. user), should not include the trailing underscore
 186      *
 187      * @return array    User or group data with keys that have been
 188      *            stripped from the preceding "user_" or "group_"
 189      *            Also the group id is prefixed with g, when the prefix group is removed.
 190      */
 191  	static public function clean_row($row, $prefix = '')
 192      {
 193          // Upon creation of a user/group $row might be empty
 194          if (empty($row))
 195          {
 196              return self::$default_row;
 197          }
 198  
 199          $output = array();
 200          foreach ($row as $key => $value)
 201          {
 202              $key = preg_replace("#^(?:{$prefix}_)#", '', $key);
 203              $output[$key] = $value;
 204          }
 205  
 206          if ($prefix === 'group' && isset($output['id']))
 207          {
 208              $output['id'] = 'g' . $output['id'];
 209          }
 210  
 211          return $output;
 212      }
 213  
 214      /**
 215      * Clean driver names that are returned from template files
 216      * Underscores are replaced with dots
 217      *
 218      * @param string $name Driver name
 219      *
 220      * @return string Cleaned driver name
 221      */
 222  	static public function clean_driver_name($name)
 223      {
 224          return str_replace(array('\\', '_'), '.', $name);
 225      }
 226  
 227      /**
 228      * Prepare driver names for use in template files
 229      * Dots are replaced with underscores
 230      *
 231      * @param string $name Clean driver name
 232      *
 233      * @return string Prepared driver name
 234      */
 235  	static public function prepare_driver_name($name)
 236      {
 237          return str_replace('.', '_', $name);
 238      }
 239  
 240      /**
 241      * Check if avatar is enabled
 242      *
 243      * @param object $driver Avatar driver object
 244      *
 245      * @return bool True if avatar is enabled, false if it's disabled
 246      */
 247  	public function is_enabled($driver)
 248      {
 249          $config_name = $driver->get_config_name();
 250  
 251          return $this->config["allow_avatar_{$config_name}"];
 252      }
 253  
 254      /**
 255      * Get the settings array for enabling/disabling an avatar driver
 256      *
 257      * @param object $driver Avatar driver object
 258      *
 259      * @return array Array of configuration options as consumed by acp_board
 260      */
 261  	public function get_avatar_settings($driver)
 262      {
 263          $config_name = $driver->get_config_name();
 264  
 265          return array(
 266              'allow_avatar_' . $config_name    => array('lang' => 'ALLOW_' . strtoupper(str_replace('\\', '_', $config_name)),        'validate' => 'bool',    'type' => 'radio:yes_no', 'explain' => false),
 267          );
 268      }
 269  
 270      /**
 271      * Replace "error" strings with their real, localized form
 272      *
 273      * @param \phpbb\user phpBB User object
 274      * @param array    $error Array containing error strings
 275      *        Key values can either be a string with a language key or an array
 276      *        that will be passed to vsprintf() with the language key in the
 277      *        first array key.
 278      *
 279      * @return array Array containing the localized error strings
 280      */
 281  	public function localize_errors(\phpbb\user $user, $error)
 282      {
 283          foreach ($error as $key => $lang)
 284          {
 285              if (is_array($lang))
 286              {
 287                  $lang_key = array_shift($lang);
 288                  $error[$key] = vsprintf($user->lang($lang_key), $lang);
 289              }
 290              else
 291              {
 292                  $error[$key] = $user->lang("$lang");
 293              }
 294          }
 295  
 296          return $error;
 297      }
 298  
 299      /**
 300      * Handle deleting avatars
 301      *
 302      * @param \phpbb\db\driver\driver_interface $db phpBB dbal
 303      * @param \phpbb\user    $user phpBB user object
 304      * @param array          $avatar_data Cleaned user data containing the user's
 305      *                               avatar data
 306      * @param string         $table Database table from which the avatar should be deleted
 307      * @param string         $prefix Prefix of user data columns in database
 308      * @return null
 309      */
 310  	public function handle_avatar_delete(\phpbb\db\driver\driver_interface $db, \phpbb\user $user, $avatar_data, $table, $prefix)
 311      {
 312          if ($driver = $this->get_driver($avatar_data['avatar_type']))
 313          {
 314              $driver->delete($avatar_data);
 315          }
 316  
 317          $result = $this->prefix_avatar_columns($prefix, self::$default_row);
 318  
 319          $sql = 'UPDATE ' . $table . '
 320              SET ' . $db->sql_build_array('UPDATE', $result) . '
 321              WHERE ' . $prefix . 'id = ' . (int) $avatar_data['id'];
 322          $db->sql_query($sql);
 323  
 324          // Make sure we also delete this avatar from the users
 325          if ($prefix === 'group_')
 326          {
 327              $result = $this->prefix_avatar_columns('user_', self::$default_row);
 328  
 329              $sql = 'UPDATE ' . USERS_TABLE . '
 330                  SET ' . $db->sql_build_array('UPDATE', $result) . "
 331                  WHERE user_avatar = '" . $db->sql_escape($avatar_data['avatar']) . "'";
 332              $db->sql_query($sql);
 333          }
 334      }
 335  
 336      /**
 337       * Prefix avatar columns
 338       *
 339       * @param string $prefix Column prefix
 340       * @param array $data Column data
 341       *
 342       * @return array Column data with prefixed column names
 343       */
 344  	public function prefix_avatar_columns($prefix, $data)
 345      {
 346          foreach ($data as $key => $value)
 347          {
 348              $data[$prefix . $key] = $value;
 349              unset($data[$key]);
 350          }
 351  
 352          return $data;
 353      }
 354  }


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1