aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb
diff options
context:
space:
mode:
authorJoseph Warner <hardolaf@hardolaf.com>2013-08-02 14:05:09 -0400
committerJoseph Warner <hardolaf@hardolaf.com>2013-08-02 14:05:44 -0400
commit245e71e4e20b8d4ec80fc5e059dc12db51d10651 (patch)
treee77402c16215a38b0a631b2aedde4463d73e6df1 /phpBB/phpbb
parent381e7c347b0d7cfc0f02d677aa61b92701606504 (diff)
downloadforums-245e71e4e20b8d4ec80fc5e059dc12db51d10651.tar
forums-245e71e4e20b8d4ec80fc5e059dc12db51d10651.tar.gz
forums-245e71e4e20b8d4ec80fc5e059dc12db51d10651.tar.bz2
forums-245e71e4e20b8d4ec80fc5e059dc12db51d10651.tar.xz
forums-245e71e4e20b8d4ec80fc5e059dc12db51d10651.zip
[feature/oauth] Add get_login_data to the auth_provider_interface
PHPBB3-11673
Diffstat (limited to 'phpBB/phpbb')
-rw-r--r--phpBB/phpbb/auth/provider/base.php8
-rw-r--r--phpBB/phpbb/auth/provider/interface.php21
-rw-r--r--phpBB/phpbb/auth/provider/oauth/oauth.php4
3 files changed, 30 insertions, 3 deletions
diff --git a/phpBB/phpbb/auth/provider/base.php b/phpBB/phpbb/auth/provider/base.php
index ca1c635b15..ae1daba82b 100644
--- a/phpBB/phpbb/auth/provider/base.php
+++ b/phpBB/phpbb/auth/provider/base.php
@@ -57,6 +57,14 @@ abstract class phpbb_auth_provider_base implements phpbb_auth_provider_interface
/**
* {@inheritdoc}
*/
+ public function get_login_data()
+ {
+ return;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
public function logout($data, $new_session)
{
return;
diff --git a/phpBB/phpbb/auth/provider/interface.php b/phpBB/phpbb/auth/provider/interface.php
index fd3fa7d879..21526fd858 100644
--- a/phpBB/phpbb/auth/provider/interface.php
+++ b/phpBB/phpbb/auth/provider/interface.php
@@ -107,6 +107,27 @@ interface phpbb_auth_provider_interface
public function get_acp_template($new_config);
/**
+ * Returns an array of data necessary to build custom elements on the login
+ * form.
+ *
+ * @return array|null If this function is not implemented on an auth
+ * provider then it returns null. If it is implemented
+ * it will return an array of up to four elements of
+ * which only 'TEMPLATE_FILE'. If 'BLOCK_VAR_NAME' is
+ * present then 'BLOCK_VARS' must also be present in
+ * the array. The fourth element 'VARS' is also
+ * optional. The array, with all four elements present
+ * looks like the following:
+ * array(
+ * 'TEMPLATE_FILE' => string,
+ * 'BLOCK_VAR_NAME' => string,
+ * 'BLOCK_VARS' => array(...),
+ * 'VARS' => array(...),
+ * )
+ */
+ public function get_login_data();
+
+ /**
* Performs additional actions during logout.
*
* @param array $data An array corresponding to
diff --git a/phpBB/phpbb/auth/provider/oauth/oauth.php b/phpBB/phpbb/auth/provider/oauth/oauth.php
index 8979f413b5..62024ff094 100644
--- a/phpBB/phpbb/auth/provider/oauth/oauth.php
+++ b/phpBB/phpbb/auth/provider/oauth/oauth.php
@@ -274,9 +274,7 @@ class phpbb_auth_provider_oauth extends phpbb_auth_provider_base
}
/**
- * Returns an array of login data for all enabled OAuth services.
- *
- * @return array
+ * {@inheritdoc}
*/
public function get_login_data()
{
href='#n268'>268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

namespace phpbb\extension;

/**
* The extension metadata manager validates and gets meta-data for extensions
*/
class metadata_manager
{
	/**
	* phpBB Config instance
	* @var \phpbb\config\config
	*/
	protected $config;

	/**
	* phpBB Extension Manager
	* @var \phpbb\extension\manager
	*/
	protected $extension_manager;

	/**
	* phpBB Template instance
	* @var \phpbb\template\template
	*/
	protected $template;

	/**
	* phpBB User instance
	* @var \phpbb\user
	*/
	protected $user;

	/**
	* phpBB root path
	* @var string
	*/
	protected $phpbb_root_path;

	/**
	* Name (including vendor) of the extension
	* @var string
	*/
	protected $ext_name;

	/**
	* Metadata from the composer.json file
	* @var array
	*/
	protected $metadata;

	/**
	* Link (including root path) to the metadata file
	* @var string
	*/
	protected $metadata_file;

	/**
	* Creates the metadata manager
	*
	* @param string				$ext_name			Name (including vendor) of the extension
	* @param \phpbb\config\config		$config				phpBB Config instance
	* @param \phpbb\extension\manager	$extension_manager	An instance of the phpBB extension manager
	* @param \phpbb\template\template	$template			phpBB Template instance
	* @param \phpbb\user 		$user 				User instance
	* @param string				$phpbb_root_path	Path to the phpbb includes directory.
	*/
	public function __construct($ext_name, \phpbb\config\config $config, \phpbb\extension\manager $extension_manager, \phpbb\template\template $template, \phpbb\user $user, $phpbb_root_path)
	{
		$this->config = $config;
		$this->extension_manager = $extension_manager;
		$this->template = $template;
		$this->user = $user;
		$this->phpbb_root_path = $phpbb_root_path;

		$this->ext_name = $ext_name;
		$this->metadata = array();
		$this->metadata_file = '';
	}

	/**
	* Processes and gets the metadata requested
	*
	* @param  string $element			All for all metadata that it has and is valid, otherwise specify which section you want by its shorthand term.
	* @return array					Contains all of the requested metadata, throws an exception on failure
	*/
	public function get_metadata($element = 'all')
	{
		$this->set_metadata_file();

		// Fetch the metadata
		$this->fetch_metadata();

		// Clean the metadata
		$this->clean_metadata_array();

		switch ($element)
		{
			case 'all':
			default:
				// Validate the metadata
				if (!$this->validate())
				{
					return false;
				}

				return $this->metadata;
			break;

			case 'name':
				return ($this->validate('name')) ? $this->metadata['name'] : false;
			break;

			case 'display-name':
				if (isset($this->metadata['extra']['display-name']))
				{
					return $this->metadata['extra']['display-name'];
				}
				else
				{
					return ($this->validate('name')) ? $this->metadata['name'] : false;
				}
			break;
		}
	}

	/**
	* Sets the filepath of the metadata file
	*
	* @throws \phpbb\extension\exception
	*/
	private function set_metadata_file()
	{
		$ext_filepath = $this->extension_manager->get_extension_path($this->ext_name);
		$metadata_filepath = $this->phpbb_root_path . $ext_filepath . 'composer.json';

		$this->metadata_file = $metadata_filepath;

		if (!file_exists($this->metadata_file))
		{
			throw new \phpbb\extension\exception($this->user->lang('FILE_NOT_FOUND', $this->metadata_file));
		}
	}

	/**
	* Gets the contents of the composer.json file
	*
	* @return bool True if success, throws an exception on failure
	* @throws \phpbb\extension\exception
	*/
	private function fetch_metadata()
	{
		if (!file_exists($this->metadata_file))
		{
			throw new \phpbb\extension\exception($this->user->lang('FILE_NOT_FOUND', $this->metadata_file));
		}
		else
		{
			if (!($file_contents = file_get_contents($this->metadata_file)))
			{
				throw new \phpbb\extension\exception($this->user->lang('FILE_CONTENT_ERR', $this->metadata_file));
			}

			if (($metadata = json_decode($file_contents, true)) === null)
			{
				throw new \phpbb\extension\exception($this->user->lang('FILE_JSON_DECODE_ERR', $this->metadata_file));
			}

			$this->metadata = $metadata;

			return true;
		}
	}

	/**
	* This array handles the cleaning of the array
	*
	* @return array Contains the cleaned metadata array
	*/
	private function clean_metadata_array()
	{
		return $this->metadata;
	}

	/**
	* Validate fields
	*
	* @param string $name  ("all" for display and enable validation
	* 						"display" for name, type, and authors
	* 						"name", "type")
	* @return Bool True if valid, throws an exception if invalid
	* @throws \phpbb\extension\exception
	*/
	public function validate($name = 'display')
	{
		// Basic fields
		$fields = array(
			'name'		=> '#^[a-zA-Z0-9_\x7f-\xff]{2,}/[a-zA-Z0-9_\x7f-\xff]{2,}$#',
			'type'		=> '#^phpbb-extension$#',
			'license'	=> '#.+#',
			'version'	=> '#.+#',
		);

		switch ($name)
		{
			case 'all':
				$this->validate('display');

				$this->validate_enable();
			break;

			case 'display':
				foreach ($fields as $field => $data)
				{
					$this->validate($field);
				}

				$this->validate_authors();
			break;

			default:
				if (isset($fields[$name]))
				{
					if (!isset($this->metadata[$name]))
					{
						throw new \phpbb\extension\exception($this->user->lang('META_FIELD_NOT_SET', $name));
					}

					if (!preg_match($fields[$name], $this->metadata[$name]))
					{
						throw new \phpbb\extension\exception($this->user->lang('META_FIELD_INVALID', $name));
					}
				}
			break;
		}

		return true;
	}

	/**
	* Validates the contents of the authors field
	*
	* @return boolean True when passes validation, throws exception if invalid
	* @throws \phpbb\extension\exception
	*/
	public function validate_authors()
	{
		if (empty($this->metadata['authors']))
		{
			throw new \phpbb\extension\exception($this->user->lang('META_FIELD_NOT_SET', 'authors'));
		}

		foreach ($this->metadata['authors'] as $author)
		{
			if (!isset($author['name']))
			{
				throw new \phpbb\extension\exception($this->user->lang('META_FIELD_NOT_SET', 'author name'));
			}
		}

		return true;
	}

	/**
	* This array handles the verification that this extension can be enabled on this board
	*
	* @return bool True if validation succeeded, False if failed
	*/
	public function validate_enable()
	{
		// Check for valid directory & phpBB, PHP versions
		if (!$this->validate_dir() || !$this->validate_require_phpbb() || !$this->validate_require_php())
		{
			return false;
		}

		return true;
	}

	/**
	* Validates the most basic directory structure to ensure it follows <vendor>/<ext> convention.
	*
	* @return boolean True when passes validation
	*/
	public function validate_dir()
	{
		return (substr_count($this->ext_name, '/') === 1 && $this->ext_name == $this->get_metadata('name'));
	}


	/**
	* Validates the contents of the phpbb requirement field
	*
	* @return boolean True when passes validation
	*/
	public function validate_require_phpbb()
	{
		if (!isset($this->metadata['extra']['soft-require']['phpbb/phpbb']))
		{
			return false;
		}

		return true;
	}

	/**
	* Validates the contents of the php requirement field
	*
	* @return boolean True when passes validation
	*/
	public function validate_require_php()
	{
		if (!isset($this->metadata['require']['php']))
		{
			return false;
		}

		return true;
	}

	/**
	* Outputs the metadata into the template
	*
	* @return null
	*/
	public function output_template_data()
	{
		$this->template->assign_vars(array(
			'META_NAME'			=> htmlspecialchars($this->metadata['name']),
			'META_TYPE'			=> htmlspecialchars($this->metadata['type']),
			'META_DESCRIPTION'	=> (isset($this->metadata['description'])) ? htmlspecialchars($this->metadata['description']) : '',
			'META_HOMEPAGE'		=> (isset($this->metadata['homepage'])) ? $this->metadata['homepage'] : '',
			'META_VERSION'		=> (isset($this->metadata['version'])) ? htmlspecialchars($this->metadata['version']) : '',
			'META_TIME'			=> (isset($this->metadata['time'])) ? htmlspecialchars($this->metadata['time']) : '',
			'META_LICENSE'		=> htmlspecialchars($this->metadata['license']),

			'META_REQUIRE_PHP'		=> (isset($this->metadata['require']['php'])) ? htmlspecialchars($this->metadata['require']['php']) : '',
			'META_REQUIRE_PHP_FAIL'	=> !$this->validate_require_php(),

			'META_REQUIRE_PHPBB'		=> (isset($this->metadata['extra']['soft-require']['phpbb/phpbb'])) ? htmlspecialchars($this->metadata['extra']['soft-require']['phpbb/phpbb']) : '',
			'META_REQUIRE_PHPBB_FAIL'	=> !$this->validate_require_phpbb(),

			'META_DISPLAY_NAME'	=> (isset($this->metadata['extra']['display-name'])) ? htmlspecialchars($this->metadata['extra']['display-name']) : '',
		));

		foreach ($this->metadata['authors'] as $author)
		{
			$this->template->assign_block_vars('meta_authors', array(
				'AUTHOR_NAME'		=> htmlspecialchars($author['name']),
				'AUTHOR_EMAIL'		=> (isset($author['email'])) ? $author['email'] : '',
				'AUTHOR_HOMEPAGE'	=> (isset($author['homepage'])) ? $author['homepage'] : '',
				'AUTHOR_ROLE'		=> (isset($author['role'])) ? htmlspecialchars($author['role']) : '',
			));
		}
	}
}