From 7a954d352ef1fe84256ad691135b6c6bf0d4bcc5 Mon Sep 17 00:00:00 2001 From: Michael Cullum Date: Sat, 28 Apr 2012 18:13:28 +0100 Subject: [ticket/10631] Fixing some items mentioned in PR PHPBB3-10631 --- phpBB/includes/extension/manager.php | 64 ++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'phpBB/includes/extension') diff --git a/phpBB/includes/extension/manager.php b/phpBB/includes/extension/manager.php index 86d8fab64b..2eebebf9b2 100644 --- a/phpBB/includes/extension/manager.php +++ b/phpBB/includes/extension/manager.php @@ -464,4 +464,68 @@ class phpbb_extension_manager { return new phpbb_extension_finder($this, $this->phpbb_root_path, $this->cache, $this->php_ext, $this->cache_name . '_finder'); } + + /** + * Gets and processes the contents of the composer.json file. + * + * TODO: Add authors, fix it + * + * @param string $name Extension name to check + * @return array All the existing metadata keys + */ + public function get_meta_data($name) + { + // Find out where the composer.json is + $ext_filepath = get_extension_path($name); + $md_filepath = $phpbb_root_path . $ext_filepath . '/composer.json'; + + // Read the composer.json and decode it + $metadatafile = file_get_contents($filepath); + $metadata = json_decode($metadatafile, true); + + // What keys are required + $required_md_keys = array( + $metadata['name'], + $metadata['type'], + $metadata['description'], + $metadata['version'], + $metadata['license'], + $medadata['require']['phpbb'], + $metadata['extra']['dispay-name'], + ); + + // Check for required keys and trigger and error if it doesn't exist + foreach ($required_md_keys as $md_key) + { + if (empty($md_key)) + { + trigger_error('Not all required items exist in the composer.json'); + } + else + { + $existing_required_keys += $md_key; + } + } + + // Which keys are optional + $optional_md_keys = array( + $metadata['require']['php'], + $metadata['time'], + $metadata['homepage'], + ); + + $existing_optional_keys = array(); + + foreach ($optional_md_keys as $md_key) + { + if (!empty($md_key)) + { + $existing_optional_keys += $md_key; + } + } + + $keys = array_merge($existing_optional_keys, $existing_required_keys); + + return $keys; + } } -- cgit v1.2.1 From a0e283d7b025325476c3c44f033410b47a87c621 Mon Sep 17 00:00:00 2001 From: Michael Cullum Date: Sat, 19 May 2012 19:56:02 +0100 Subject: [ticket/10631] Adding extension metadata manager class PHPBB3-10631 --- phpBB/includes/extension/metadata_manager.php | 368 ++++++++++++++++++++++++++ 1 file changed, 368 insertions(+) create mode 100644 phpBB/includes/extension/metadata_manager.php (limited to 'phpBB/includes/extension') diff --git a/phpBB/includes/extension/metadata_manager.php b/phpBB/includes/extension/metadata_manager.php new file mode 100644 index 0000000000..b58f1c6498 --- /dev/null +++ b/phpBB/includes/extension/metadata_manager.php @@ -0,0 +1,368 @@ +phpbb_root_path = $phpbb_root_path; + $this->db = $db; + $this->phpEx = $phpEx; + $this->extension_manager = $extension_manager; + $this->ext_name = $ext_name; + $this->template = $template; + $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. + * @param bool $template_output True if you want the requested metadata assigned to template vars + * @return array Contains all of the requested metadata + */ + public function get_meta_data($element = 'all', $template_output = false) + { + // TODO: Check ext_name exists and is an extension that exists + if (!$this->set_meta_data_file()) + { + return false; + } + + switch ($element) + { + case 'all': + default: + if (!$this->clean_metadata_array()) + { + return false; + } + + if ($template_output) + { + $this->output_template_data(); + } + + return $this->metadata; + break; + + case 'name': + if($this->validate_name) + { + if ($template_output) + { + $this->template->assign_vars(array( + 'MD_NAME' => htmlspecialchars($this->metadata['name']), + )); + } + return $this->metadata['name']; + } + else + { + return false; + } + break; + // TODO: Add remaining cases + } + } + + /** + * Sets the filepath of the metadata file + * + * @return null + */ + private function set_meta_data_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)) + { + return false; + } + else + { + return true; + } + } + + /** + * This array handles the validation and cleaning of the array + * + * @return array Contains the cleaned and validated metadata array + */ + private function clean_metadata_array() + { + if (!$this->validate_name() || !$this->validate_type() || !$this->validate_license() || !$this->validate_description() || !$this->validate_version() || !$this->validate_require_phpbb() || !$this->validate_extra_display_name()) + { + return false; + } + + $this->check_for_optional(true); + +// TODO: Remove all parts of the array we don't want or shouldn't be there due to nub mod authors +// $this->metadata = $metadata_finished; + $metadata_finished = $this->metadata; + + return $metadata_finished; + } + + /** + * Validates the contents of the name field + * + * @return bool True when passes validation + */ + private function validate_name() + { + if(preg_match('^[a-zA-Z0-9_\x7f-\xff]{2,}/[a-zA-Z0-9_\x7f-\xff]{2,}$', $this->metadata['name'])) + { + return true; + } + else + { + return false; + } + } + + /** + * Validates the contents of the type field + * + * @return bool True when passes validation + */ + private function validate_type() + { + if ($this->metadata['type'] != 'phpbb3-extension') + { + return false; + } + else + { + return true; + } + } + + /** + * Validates the contents of the description field + * + * @return bool True when passes validation + */ + private function validate_description() + { + if(preg_match('^{10,}$', $this->metadata['description'])) + { + return true; + } + else + { + return false; + } + } + + /** + * Validates the contents of the version field + * + * @return bool True when passes validation + */ + private function validate_version() + { + if(preg_match('^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}$', $this->metadata['version'])) + { + return true; + } + else + { + return false; + } + } + + /** + * Validates the contents of the license field + * + * @return bool True when passes validation + */ + private function validate_license() + { + if ($this->metadata['license'] != 'GPLv2') + { + return false; + } + else + { + return true; + } + } + + /** + * Validates the contents of the phpbb requirement field + * + * @return bool True when passes validation + */ + private function validate_require_phpbb() + { + if(preg_match('^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}$', $this->metadata['require']['phpbb']) && version_compare($this->metadata['require']['phpbb']), '3.1.0', '>') + { + return true; + } + else + { + return false; + } + } + + /** + * Validates the contents of the display name field + * + * @return bool True when passes validation + */ + private function validate_extra_display_name() + { + if(preg_match('^[a-zA-Z0-9_]{2,0}$', $this->metadata['name'])) + { + return true; + } + else + { + return false; + } + } + + /** + * Checks which optional fields exist + * + * @return boolean False if any that exist fail validation, otherwise true. + */ + public function check_for_optional() + { + if ((isset($this->metadata['require']['php']) && !$this->validate_require_php()) || (isset($this->metadata['time']) && !$this->validate_time()) || (isset($this->metadata['validate_homepage']) && !$this->validate_homepage())) + { + return false; + } + } + + /** + * Validates the contents of the php requirement field + * + * @return bool True when passes validation + */ + private function validate_require_php() + { + + } + + /** + * Validates the contents of the time field + * + * @return bool True when passes validation + */ + private function validate_time() + { + + } + + /** + * Validates the contents of the homepage field + * + * @return bool True when passes validation + */ + private function validate_homepage() + { + + } + + /** + * Validates the contents of the authors field + * + * @return bool True when passes validation + */ + private function validate_authors() + { + + } + + /** + * Gets the contents of the composer.json file and can also assign template vars + * + * @return array Contains everything from the meta data file. Do not use without validating and cleaning first + */ + private function fetch_metadata() + { + // Read it + $metadata_file = file_get_contents($metadata_filepath); + $metadata = json_decode($metadata_file, true) + + $this->metadata = $metadata; + + return $metadata; + } + + /** + * Outputs the metadata into the template + * + * @return null + */ + public function output_template_data() + { + $template->assign_vars(array( + 'MD_NAME' => htmlspecialchars($this->metadata['name']), + 'MD_TYPE' => htmlspecialchars($this->metadata['type']), + 'MD_DESCRIPTION' => htmlspecialchars($this->metadata['description']), + 'MD_HOMEPAGE' => $this->metadata['homepage'], + 'MD_VERSION' => htmlspecialchars($this->metadata['version']), + 'MD_TIME' => htmlspecialchars($this->metadata['time']), + 'MD_LICENSE' => htmlspecialchars($this->metadata['license']), + 'MD_REQUIRE_PHP' => htmlspecialchars($this->metadata['require']['php']), + 'MD_REQUIRE_PHPBB' => htmlspecialchars($this->metadata['require']['phpbb']), + 'MD_DISPLAY_NAME' => htmlspecialchars($this->metadata['extra']['display-name']), + )); + + foreach ($this->metadata['authors'] as $author) + { + $template->assign_block_vars('md_authors', array( + 'AUTHOR_NAME' => htmlspecialchars($author['name']), + 'AUTHOR_EMAIL' => $author['email'], + 'AUTHOR_HOMEPAGE' => $author['homepage'], + 'AUTHOR_ROLE' => htmlspecialchars($author['role']), + )); + } + + return; + } -- cgit v1.2.1 From ac883e26528736c74f96b5bc692f685bdb25fd57 Mon Sep 17 00:00:00 2001 From: Michael Cullum Date: Sat, 19 May 2012 19:57:02 +0100 Subject: [ticket/10631] Remove the now un-used method PHPBB3-10631 --- phpBB/includes/extension/manager.php | 65 ------------------------------------ 1 file changed, 65 deletions(-) (limited to 'phpBB/includes/extension') diff --git a/phpBB/includes/extension/manager.php b/phpBB/includes/extension/manager.php index 2eebebf9b2..287e3828f9 100644 --- a/phpBB/includes/extension/manager.php +++ b/phpBB/includes/extension/manager.php @@ -464,68 +464,3 @@ class phpbb_extension_manager { return new phpbb_extension_finder($this, $this->phpbb_root_path, $this->cache, $this->php_ext, $this->cache_name . '_finder'); } - - /** - * Gets and processes the contents of the composer.json file. - * - * TODO: Add authors, fix it - * - * @param string $name Extension name to check - * @return array All the existing metadata keys - */ - public function get_meta_data($name) - { - // Find out where the composer.json is - $ext_filepath = get_extension_path($name); - $md_filepath = $phpbb_root_path . $ext_filepath . '/composer.json'; - - // Read the composer.json and decode it - $metadatafile = file_get_contents($filepath); - $metadata = json_decode($metadatafile, true); - - // What keys are required - $required_md_keys = array( - $metadata['name'], - $metadata['type'], - $metadata['description'], - $metadata['version'], - $metadata['license'], - $medadata['require']['phpbb'], - $metadata['extra']['dispay-name'], - ); - - // Check for required keys and trigger and error if it doesn't exist - foreach ($required_md_keys as $md_key) - { - if (empty($md_key)) - { - trigger_error('Not all required items exist in the composer.json'); - } - else - { - $existing_required_keys += $md_key; - } - } - - // Which keys are optional - $optional_md_keys = array( - $metadata['require']['php'], - $metadata['time'], - $metadata['homepage'], - ); - - $existing_optional_keys = array(); - - foreach ($optional_md_keys as $md_key) - { - if (!empty($md_key)) - { - $existing_optional_keys += $md_key; - } - } - - $keys = array_merge($existing_optional_keys, $existing_required_keys); - - return $keys; - } -} -- cgit v1.2.1 From 10cba1426d7119a98c55cbefe2cfb1fd7de9b057 Mon Sep 17 00:00:00 2001 From: Michael Cullum Date: Sat, 19 May 2012 23:32:34 +0100 Subject: [ticket/10631] Some tidying up PHPBB3-10631 --- phpBB/includes/extension/manager.php | 1 + phpBB/includes/extension/metadata_manager.php | 71 +++++---------------------- 2 files changed, 12 insertions(+), 60 deletions(-) (limited to 'phpBB/includes/extension') diff --git a/phpBB/includes/extension/manager.php b/phpBB/includes/extension/manager.php index 287e3828f9..86d8fab64b 100644 --- a/phpBB/includes/extension/manager.php +++ b/phpBB/includes/extension/manager.php @@ -464,3 +464,4 @@ class phpbb_extension_manager { return new phpbb_extension_finder($this, $this->phpbb_root_path, $this->cache, $this->php_ext, $this->cache_name . '_finder'); } +} diff --git a/phpBB/includes/extension/metadata_manager.php b/phpBB/includes/extension/metadata_manager.php index b58f1c6498..db8d09b696 100644 --- a/phpBB/includes/extension/metadata_manager.php +++ b/phpBB/includes/extension/metadata_manager.php @@ -39,7 +39,7 @@ class phpbb_extension_metadata_manager * @param string $phpbb_root_path Path to the phpbb includes directory. * @param string $phpEx php file extension */ - public function __construct($ext_name, dbal $db, phpbb_extension_manager $extension_manager, $phpbb_root_path, $phpEx = '.php', template $template) + public function __construct($ext_name, dbal $db, phpbb_extension_manager $extension_manager, $phpbb_root_path, $phpEx = '.php', phpbb_template $template) { $this->phpbb_root_path = $phpbb_root_path; $this->db = $db; @@ -84,7 +84,7 @@ class phpbb_extension_metadata_manager break; case 'name': - if($this->validate_name) + if ($this->validate_name) { if ($template_output) { @@ -106,7 +106,7 @@ class phpbb_extension_metadata_manager /** * Sets the filepath of the metadata file * - * @return null + * @return boolean Set to true if it exists */ private function set_meta_data_file() { @@ -115,7 +115,7 @@ class phpbb_extension_metadata_manager $this->metadata_file = $metadata_filepath; - if(!file_exists($this->metadata_file)) + if (!file_exists($this->metadata_file)) { return false; } @@ -153,14 +153,7 @@ class phpbb_extension_metadata_manager */ private function validate_name() { - if(preg_match('^[a-zA-Z0-9_\x7f-\xff]{2,}/[a-zA-Z0-9_\x7f-\xff]{2,}$', $this->metadata['name'])) - { - return true; - } - else - { - return false; - } + return preg_match('^[a-zA-Z0-9_\x7f-\xff]{2,}/[a-zA-Z0-9_\x7f-\xff]{2,}$', $this->metadata['name']); } /** @@ -170,14 +163,7 @@ class phpbb_extension_metadata_manager */ private function validate_type() { - if ($this->metadata['type'] != 'phpbb3-extension') - { - return false; - } - else - { - return true; - } + return $this->metadata['type'] != 'phpbb3-extension'; } /** @@ -187,14 +173,7 @@ class phpbb_extension_metadata_manager */ private function validate_description() { - if(preg_match('^{10,}$', $this->metadata['description'])) - { - return true; - } - else - { - return false; - } + return preg_match('^{10,}$', $this->metadata['description']); } /** @@ -204,14 +183,7 @@ class phpbb_extension_metadata_manager */ private function validate_version() { - if(preg_match('^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}$', $this->metadata['version'])) - { - return true; - } - else - { - return false; - } + return preg_match('^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}$', $this->metadata['version']); } /** @@ -221,14 +193,7 @@ class phpbb_extension_metadata_manager */ private function validate_license() { - if ($this->metadata['license'] != 'GPLv2') - { - return false; - } - else - { - return true; - } + return $this->metadata['license'] != 'GPLv2'; } /** @@ -238,14 +203,7 @@ class phpbb_extension_metadata_manager */ private function validate_require_phpbb() { - if(preg_match('^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}$', $this->metadata['require']['phpbb']) && version_compare($this->metadata['require']['phpbb']), '3.1.0', '>') - { - return true; - } - else - { - return false; - } + return (preg_match('^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}$', $this->metadata['require']['phpbb']) && version_compare($this->metadata['require']['phpbb']), '3.1.0', '>'); } /** @@ -255,14 +213,7 @@ class phpbb_extension_metadata_manager */ private function validate_extra_display_name() { - if(preg_match('^[a-zA-Z0-9_]{2,0}$', $this->metadata['name'])) - { - return true; - } - else - { - return false; - } + return preg_match('^[a-zA-Z0-9_]{2,0}$', $this->metadata['name']); } /** -- cgit v1.2.1 From 3e6761b0266dfc67f6b41fea50d377b7621dffe0 Mon Sep 17 00:00:00 2001 From: Michael Cullum Date: Sun, 20 May 2012 14:16:00 +0100 Subject: [ticket/10631] Fixing and finishing the extension metadata class. PHPBB3-10631 --- phpBB/includes/extension/metadata_manager.php | 67 +++++++++++++++++---------- 1 file changed, 43 insertions(+), 24 deletions(-) (limited to 'phpBB/includes/extension') diff --git a/phpBB/includes/extension/metadata_manager.php b/phpBB/includes/extension/metadata_manager.php index db8d09b696..6ec5a0f76d 100644 --- a/phpBB/includes/extension/metadata_manager.php +++ b/phpBB/includes/extension/metadata_manager.php @@ -28,7 +28,7 @@ class phpbb_extension_metadata_manager protected $phpbb_root_path; protected $ext_name; protected $template; - protected $metadata; + public $metadata; protected $metadata_file; /** @@ -55,13 +55,13 @@ class phpbb_extension_metadata_manager * 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. - * @param bool $template_output True if you want the requested metadata assigned to template vars + * @param boolean $template_output True if you want the requested metadata assigned to template vars * @return array Contains all of the requested metadata */ - public function get_meta_data($element = 'all', $template_output = false) + public function get_metadata($element = 'all', $template_output = false) { // TODO: Check ext_name exists and is an extension that exists - if (!$this->set_meta_data_file()) + if (!$this->set_metadata_file()) { return false; } @@ -99,7 +99,7 @@ class phpbb_extension_metadata_manager return false; } break; - // TODO: Add remaining cases + // TODO: Add remaining cases as needed } } @@ -108,7 +108,7 @@ class phpbb_extension_metadata_manager * * @return boolean Set to true if it exists */ - private function set_meta_data_file() + 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'; @@ -141,15 +141,14 @@ class phpbb_extension_metadata_manager // TODO: Remove all parts of the array we don't want or shouldn't be there due to nub mod authors // $this->metadata = $metadata_finished; - $metadata_finished = $this->metadata; - return $metadata_finished; + return $this->metadata; } /** * Validates the contents of the name field * - * @return bool True when passes validation + * @return boolean True when passes validation */ private function validate_name() { @@ -159,7 +158,7 @@ class phpbb_extension_metadata_manager /** * Validates the contents of the type field * - * @return bool True when passes validation + * @return boolean True when passes validation */ private function validate_type() { @@ -169,7 +168,7 @@ class phpbb_extension_metadata_manager /** * Validates the contents of the description field * - * @return bool True when passes validation + * @return boolean True when passes validation */ private function validate_description() { @@ -179,27 +178,28 @@ class phpbb_extension_metadata_manager /** * Validates the contents of the version field * - * @return bool True when passes validation + * @return boolean True when passes validation */ private function validate_version() { - return preg_match('^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}$', $this->metadata['version']); + return preg_match('^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}', $this->metadata['version']); } /** * Validates the contents of the license field * - * @return bool True when passes validation + * @return boolean True when passes validation */ private function validate_license() { - return $this->metadata['license'] != 'GPLv2'; + // Nothing to validate except existence + return isset($this->metadata['version']); } /** * Validates the contents of the phpbb requirement field * - * @return bool True when passes validation + * @return boolean True when passes validation */ private function validate_require_phpbb() { @@ -209,7 +209,7 @@ class phpbb_extension_metadata_manager /** * Validates the contents of the display name field * - * @return bool True when passes validation + * @return boolean True when passes validation */ private function validate_extra_display_name() { @@ -232,41 +232,60 @@ class phpbb_extension_metadata_manager /** * Validates the contents of the php requirement field * - * @return bool True when passes validation + * @return boolean True when passes validation */ private function validate_require_php() { - + return preg_match('^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}$', $this->metadata['require']['phpbb'] } /** * Validates the contents of the time field * - * @return bool True when passes validation + * @return boolean True when passes validation */ private function validate_time() { - + // Need to validate + return true; } /** * Validates the contents of the homepage field * - * @return bool True when passes validation + * @return boolean True when passes validation */ private function validate_homepage() { - + return preg_match('([\d\w-.]+?\.(a[cdefgilmnoqrstuwz]|b[abdefghijmnorstvwyz]|c[acdfghiklmnoruvxyz]|d[ejkmnoz]|e[ceghrst]|f[ijkmnor]|g[abdefghilmnpqrstuwy]|h[kmnrtu]|i[delmnoqrst]|j[emop]|k[eghimnprwyz]|l[abcikrstuvy]|m[acdghklmnopqrstuvwxyz]|n[acefgilopruz]|om|p[aefghklmnrstwy]|qa|r[eouw]|s[abcdeghijklmnortuvyz]|t[cdfghjkmnoprtvwz]|u[augkmsyz]|v[aceginu]|w[fs]|y[etu]|z[amw]|aero|arpa|biz|com|coop|edu|info|int|gov|mil|museum|name|net|org|pro)(\b|\W(?metadata['homepage']) } /** * Validates the contents of the authors field * - * @return bool True when passes validation + * @return boolean True when passes validation */ private function validate_authors() { + // Need to validate + $number_authors = sizeof($this->metadata['authors']); // Might be helpful later on + + if (!isset($this->metadata['authors']['1'])) + { + return false; + } + else + { + foreach ($this->metadata['authors'] as $author) + { + if (!isset($author['name'])) + { + return false; + } + } + } + return true; } /** -- cgit v1.2.1 From dd4f07f9bb864bdac8b6c4009d166f1df6411419 Mon Sep 17 00:00:00 2001 From: Michael Cullum Date: Sun, 20 May 2012 14:18:11 +0100 Subject: [ticket/10631] Template shouldn't be required PHPBB3-10631 --- phpBB/includes/extension/metadata_manager.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'phpBB/includes/extension') diff --git a/phpBB/includes/extension/metadata_manager.php b/phpBB/includes/extension/metadata_manager.php index 6ec5a0f76d..fea66c86fe 100644 --- a/phpBB/includes/extension/metadata_manager.php +++ b/phpBB/includes/extension/metadata_manager.php @@ -27,7 +27,6 @@ class phpbb_extension_metadata_manager protected $db; protected $phpbb_root_path; protected $ext_name; - protected $template; public $metadata; protected $metadata_file; @@ -39,14 +38,13 @@ class phpbb_extension_metadata_manager * @param string $phpbb_root_path Path to the phpbb includes directory. * @param string $phpEx php file extension */ - public function __construct($ext_name, dbal $db, phpbb_extension_manager $extension_manager, $phpbb_root_path, $phpEx = '.php', phpbb_template $template) + public function __construct($ext_name, dbal $db, phpbb_extension_manager $extension_manager, $phpbb_root_path, $phpEx = '.php') { $this->phpbb_root_path = $phpbb_root_path; $this->db = $db; $this->phpEx = $phpEx; $this->extension_manager = $extension_manager; $this->ext_name = $ext_name; - $this->template = $template; $this->metadata = array(); $this->metadata_file = ''; } @@ -58,7 +56,7 @@ class phpbb_extension_metadata_manager * @param boolean $template_output True if you want the requested metadata assigned to template vars * @return array Contains all of the requested metadata */ - public function get_metadata($element = 'all', $template_output = false) + public function get_metadata($element = 'all', $template_output = false, phpbb_template $template) { // TODO: Check ext_name exists and is an extension that exists if (!$this->set_metadata_file()) @@ -77,7 +75,7 @@ class phpbb_extension_metadata_manager if ($template_output) { - $this->output_template_data(); + $this->output_template_data($template); } return $this->metadata; @@ -88,7 +86,7 @@ class phpbb_extension_metadata_manager { if ($template_output) { - $this->template->assign_vars(array( + $template->assign_vars(array( 'MD_NAME' => htmlspecialchars($this->metadata['name']), )); } @@ -309,7 +307,7 @@ class phpbb_extension_metadata_manager * * @return null */ - public function output_template_data() + public function output_template_data(phpbb_template $template) { $template->assign_vars(array( 'MD_NAME' => htmlspecialchars($this->metadata['name']), -- cgit v1.2.1 From 3ba59c6362c955d1f9b59278f7dd19cdacecff99 Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Sun, 22 Jul 2012 18:11:56 -0500 Subject: [ticket/10631] Various tidbits and cleanup on the acp extensions manager PHPBB3-10631 --- phpBB/includes/extension/manager.php | 13 ++++++ phpBB/includes/extension/metadata_manager.php | 64 ++++++++++++++++++--------- 2 files changed, 56 insertions(+), 21 deletions(-) (limited to 'phpBB/includes/extension') diff --git a/phpBB/includes/extension/manager.php b/phpBB/includes/extension/manager.php index 86d8fab64b..e7ceef6ad5 100644 --- a/phpBB/includes/extension/manager.php +++ b/phpBB/includes/extension/manager.php @@ -22,6 +22,7 @@ if (!defined('IN_PHPBB')) */ class phpbb_extension_manager { + protected $db; protected $cache; protected $php_ext; protected $extensions; @@ -120,6 +121,18 @@ class phpbb_extension_manager } } + /** + * Instantiates the metadata manager for the extension with the given name + * + * @param string $name The extension name + * @param string $template The template manager + * @return phpbb_extension_metadata_manager Instance of the metadata manager + */ + public function get_extension_metadata($name, phpbb_template $template) + { + return new phpbb_extension_metadata_manager($name, $this->db, $this, $this->phpbb_root_path, $this->phpEx, $template); + } + /** * Runs a step of the extension enabling process. * diff --git a/phpBB/includes/extension/metadata_manager.php b/phpBB/includes/extension/metadata_manager.php index fea66c86fe..a9dcd89592 100644 --- a/phpBB/includes/extension/metadata_manager.php +++ b/phpBB/includes/extension/metadata_manager.php @@ -26,6 +26,7 @@ class phpbb_extension_metadata_manager protected $extension_manager; protected $db; protected $phpbb_root_path; + protected $template; protected $ext_name; public $metadata; protected $metadata_file; @@ -38,11 +39,12 @@ class phpbb_extension_metadata_manager * @param string $phpbb_root_path Path to the phpbb includes directory. * @param string $phpEx php file extension */ - public function __construct($ext_name, dbal $db, phpbb_extension_manager $extension_manager, $phpbb_root_path, $phpEx = '.php') + public function __construct($ext_name, dbal $db, phpbb_extension_manager $extension_manager, $phpbb_root_path, $phpEx = '.php', phpbb_template $template) { $this->phpbb_root_path = $phpbb_root_path; $this->db = $db; $this->phpEx = $phpEx; + $this->template = $template; $this->extension_manager = $extension_manager; $this->ext_name = $ext_name; $this->metadata = array(); @@ -56,7 +58,7 @@ class phpbb_extension_metadata_manager * @param boolean $template_output True if you want the requested metadata assigned to template vars * @return array Contains all of the requested metadata */ - public function get_metadata($element = 'all', $template_output = false, phpbb_template $template) + public function get_metadata($element = 'all', $template_output = false) { // TODO: Check ext_name exists and is an extension that exists if (!$this->set_metadata_file()) @@ -64,6 +66,11 @@ class phpbb_extension_metadata_manager return false; } + if (!$this->fetch_metadata()) + { + return false; + } + switch ($element) { case 'all': @@ -82,7 +89,7 @@ class phpbb_extension_metadata_manager break; case 'name': - if ($this->validate_name) + if ($this->validate_name()) { if ($template_output) { @@ -90,6 +97,7 @@ class phpbb_extension_metadata_manager 'MD_NAME' => htmlspecialchars($this->metadata['name']), )); } + return $this->metadata['name']; } else @@ -129,7 +137,7 @@ class phpbb_extension_metadata_manager * @return array Contains the cleaned and validated metadata array */ private function clean_metadata_array() - { + { if (!$this->validate_name() || !$this->validate_type() || !$this->validate_license() || !$this->validate_description() || !$this->validate_version() || !$this->validate_require_phpbb() || !$this->validate_extra_display_name()) { return false; @@ -150,7 +158,7 @@ class phpbb_extension_metadata_manager */ private function validate_name() { - return preg_match('^[a-zA-Z0-9_\x7f-\xff]{2,}/[a-zA-Z0-9_\x7f-\xff]{2,}$', $this->metadata['name']); + return preg_match('#^[a-zA-Z0-9_\x7f-\xff]{2,}/[a-zA-Z0-9_\x7f-\xff]{2,}$#', $this->metadata['name']); } /** @@ -160,7 +168,7 @@ class phpbb_extension_metadata_manager */ private function validate_type() { - return $this->metadata['type'] != 'phpbb3-extension'; + return $this->metadata['type'] == 'phpbb3-extension'; } /** @@ -170,7 +178,7 @@ class phpbb_extension_metadata_manager */ private function validate_description() { - return preg_match('^{10,}$', $this->metadata['description']); + return preg_match('#^{10,}$#', $this->metadata['description']); } /** @@ -180,7 +188,7 @@ class phpbb_extension_metadata_manager */ private function validate_version() { - return preg_match('^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}', $this->metadata['version']); + return preg_match('#^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}#', $this->metadata['version']); } /** @@ -201,7 +209,7 @@ class phpbb_extension_metadata_manager */ private function validate_require_phpbb() { - return (preg_match('^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}$', $this->metadata['require']['phpbb']) && version_compare($this->metadata['require']['phpbb']), '3.1.0', '>'); + return (preg_match('#^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}$#', $this->metadata['require']['phpbb']) && version_compare($this->metadata['require']['phpbb'], '3.1.0', '>=')); } /** @@ -211,7 +219,7 @@ class phpbb_extension_metadata_manager */ private function validate_extra_display_name() { - return preg_match('^[a-zA-Z0-9_]{2,0}$', $this->metadata['name']); + return preg_match('#^[a-zA-Z0-9_]{2,0}$#', $this->metadata['name']); } /** @@ -234,7 +242,7 @@ class phpbb_extension_metadata_manager */ private function validate_require_php() { - return preg_match('^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}$', $this->metadata['require']['phpbb'] + return (preg_match('#^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}$#', $this->metadata['require']['php']) && version_compare($this->metadata['require']['php'], phpversion(), '>=')); } /** @@ -255,7 +263,7 @@ class phpbb_extension_metadata_manager */ private function validate_homepage() { - return preg_match('([\d\w-.]+?\.(a[cdefgilmnoqrstuwz]|b[abdefghijmnorstvwyz]|c[acdfghiklmnoruvxyz]|d[ejkmnoz]|e[ceghrst]|f[ijkmnor]|g[abdefghilmnpqrstuwy]|h[kmnrtu]|i[delmnoqrst]|j[emop]|k[eghimnprwyz]|l[abcikrstuvy]|m[acdghklmnopqrstuvwxyz]|n[acefgilopruz]|om|p[aefghklmnrstwy]|qa|r[eouw]|s[abcdeghijklmnortuvyz]|t[cdfghjkmnoprtvwz]|u[augkmsyz]|v[aceginu]|w[fs]|y[etu]|z[amw]|aero|arpa|biz|com|coop|edu|info|int|gov|mil|museum|name|net|org|pro)(\b|\W(?metadata['homepage']) + return preg_match('#([\d\w-.]+?\.(a[cdefgilmnoqrstuwz]|b[abdefghijmnorstvwyz]|c[acdfghiklmnoruvxyz]|d[ejkmnoz]|e[ceghrst]|f[ijkmnor]|g[abdefghilmnpqrstuwy]|h[kmnrtu]|i[delmnoqrst]|j[emop]|k[eghimnprwyz]|l[abcikrstuvy]|m[acdghklmnopqrstuvwxyz]|n[acefgilopruz]|om|p[aefghklmnrstwy]|qa|r[eouw]|s[abcdeghijklmnortuvyz]|t[cdfghjkmnoprtvwz]|u[augkmsyz]|v[aceginu]|w[fs]|y[etu]|z[amw]|aero|arpa|biz|com|coop|edu|info|int|gov|mil|museum|name|net|org|pro)(\b|\W(?metadata['homepage']); } /** @@ -287,19 +295,32 @@ class phpbb_extension_metadata_manager } /** - * Gets the contents of the composer.json file and can also assign template vars + * Gets the contents of the composer.json file * - * @return array Contains everything from the meta data file. Do not use without validating and cleaning first + * @return bool True of false (if loading succeeded or failed) */ private function fetch_metadata() { - // Read it - $metadata_file = file_get_contents($metadata_filepath); - $metadata = json_decode($metadata_file, true) - - $this->metadata = $metadata; - - return $metadata; + if (!file_exists($this->metadata_file)) + { + return false; + } + else + { + if (!($file_contents = file_get_contents($this->metadata_file))) + { + return false; + } + + if (($metadata = json_decode($file_contents, true)) === NULL) + { + return false; + } + + $this->metadata = $metadata; + + return true; + } } /** @@ -334,3 +355,4 @@ class phpbb_extension_metadata_manager return; } +} -- cgit v1.2.1 From bf6e91b5f3f6d56c1a38cbff0ccf206f13202e50 Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Sun, 22 Jul 2012 20:24:20 -0500 Subject: [ticket/10631] Fixing some more issues PHPBB3-10631 --- phpBB/includes/extension/metadata_manager.php | 35 ++++++++++++++------------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'phpBB/includes/extension') diff --git a/phpBB/includes/extension/metadata_manager.php b/phpBB/includes/extension/metadata_manager.php index a9dcd89592..ddec918732 100644 --- a/phpBB/includes/extension/metadata_manager.php +++ b/phpBB/includes/extension/metadata_manager.php @@ -82,7 +82,7 @@ class phpbb_extension_metadata_manager if ($template_output) { - $this->output_template_data($template); + $this->output_template_data(); } return $this->metadata; @@ -138,7 +138,7 @@ class phpbb_extension_metadata_manager */ private function clean_metadata_array() { - if (!$this->validate_name() || !$this->validate_type() || !$this->validate_license() || !$this->validate_description() || !$this->validate_version() || !$this->validate_require_phpbb() || !$this->validate_extra_display_name()) + if (!$this->validate_name() || !$this->validate_type() || !$this->validate_licence() || !$this->validate_description() || !$this->validate_version() || !$this->validate_require_phpbb() || !$this->validate_extra_display_name()) { return false; } @@ -178,7 +178,7 @@ class phpbb_extension_metadata_manager */ private function validate_description() { - return preg_match('#^{10,}$#', $this->metadata['description']); + return true;//preg_match('#^{10,}$#', $this->metadata['description']); } /** @@ -196,10 +196,10 @@ class phpbb_extension_metadata_manager * * @return boolean True when passes validation */ - private function validate_license() + private function validate_licence() { // Nothing to validate except existence - return isset($this->metadata['version']); + return isset($this->metadata['licence']); } /** @@ -219,7 +219,7 @@ class phpbb_extension_metadata_manager */ private function validate_extra_display_name() { - return preg_match('#^[a-zA-Z0-9_]{2,0}$#', $this->metadata['name']); + return true;//preg_match('#^[a-zA-Z0-9_]{2,0}$#', $this->metadata['name']); } /** @@ -328,31 +328,32 @@ class phpbb_extension_metadata_manager * * @return null */ - public function output_template_data(phpbb_template $template) + public function output_template_data() { - $template->assign_vars(array( + + $this->template->assign_vars(array( 'MD_NAME' => htmlspecialchars($this->metadata['name']), 'MD_TYPE' => htmlspecialchars($this->metadata['type']), 'MD_DESCRIPTION' => htmlspecialchars($this->metadata['description']), - 'MD_HOMEPAGE' => $this->metadata['homepage'], + 'MD_HOMEPAGE' => (isset($this->metadata['homepage'])) ? $this->metadata['homepage'] : '', 'MD_VERSION' => htmlspecialchars($this->metadata['version']), 'MD_TIME' => htmlspecialchars($this->metadata['time']), - 'MD_LICENSE' => htmlspecialchars($this->metadata['license']), - 'MD_REQUIRE_PHP' => htmlspecialchars($this->metadata['require']['php']), - 'MD_REQUIRE_PHPBB' => htmlspecialchars($this->metadata['require']['phpbb']), - 'MD_DISPLAY_NAME' => htmlspecialchars($this->metadata['extra']['display-name']), + 'MD_LICENCE' => htmlspecialchars($this->metadata['licence']), + 'MD_REQUIRE_PHP' => (isset($this->metadata['require']['php'])) ? htmlspecialchars($this->metadata['require']['php']) : '', + 'MD_REQUIRE_PHPBB' => (isset($this->metadata['require']['phpbb'])) ? htmlspecialchars($this->metadata['require']['phpbb']) : '', + 'MD_DISPLAY_NAME' => (isset($this->metadata['extra']['display-name'])) ? htmlspecialchars($this->metadata['extra']['display-name']) : '', )); foreach ($this->metadata['authors'] as $author) { - $template->assign_block_vars('md_authors', array( + $this->template->assign_block_vars('md_authors', array( 'AUTHOR_NAME' => htmlspecialchars($author['name']), 'AUTHOR_EMAIL' => $author['email'], - 'AUTHOR_HOMEPAGE' => $author['homepage'], - 'AUTHOR_ROLE' => htmlspecialchars($author['role']), + 'AUTHOR_HOMEPAGE' => (isset($author['homepage'])) ? $author['homepage'] : '', + 'AUTHOR_ROLE' => (isset($author['role'])) ? htmlspecialchars($author['role']) : '', )); } - + return; } } -- cgit v1.2.1 From 74492b3cdda9538263484a6f2a2042ac1900228a Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Mon, 23 Jul 2012 14:01:13 -0500 Subject: [ticket/10631] Use display name if available PHPBB3-10631 --- phpBB/includes/extension/metadata_manager.php | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) (limited to 'phpBB/includes/extension') diff --git a/phpBB/includes/extension/metadata_manager.php b/phpBB/includes/extension/metadata_manager.php index ddec918732..6af02e47b7 100644 --- a/phpBB/includes/extension/metadata_manager.php +++ b/phpBB/includes/extension/metadata_manager.php @@ -55,7 +55,7 @@ class phpbb_extension_metadata_manager * 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. - * @param boolean $template_output True if you want the requested metadata assigned to template vars + * @param boolean $template_output True if you want the requested metadata assigned to template vars (only works on the 'all" case * @return array Contains all of the requested metadata */ public function get_metadata($element = 'all', $template_output = false) @@ -89,20 +89,17 @@ class phpbb_extension_metadata_manager break; case 'name': - if ($this->validate_name()) + return ($this->validate_name()) ? $this->metadata['name'] : false; + break; + + case 'display-name': + if ($this->validate_extra_display_name()) { - if ($template_output) - { - $template->assign_vars(array( - 'MD_NAME' => htmlspecialchars($this->metadata['name']), - )); - } - - return $this->metadata['name']; + return $this->metadata['extra']['display-name']; } else { - return false; + return ($this->validate_name()) ? $this->metadata['name'] : false; } break; // TODO: Add remaining cases as needed -- cgit v1.2.1 From 8bbab088dd5830d8dd1151a3684dde5c197ba268 Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Mon, 23 Jul 2012 15:17:42 -0500 Subject: [ticket/10631] Validation for extensions PHPBB3-10631 --- phpBB/includes/extension/manager.php | 7 +- phpBB/includes/extension/metadata_manager.php | 298 ++++++++++++++++---------- 2 files changed, 184 insertions(+), 121 deletions(-) (limited to 'phpBB/includes/extension') diff --git a/phpBB/includes/extension/manager.php b/phpBB/includes/extension/manager.php index e7ceef6ad5..9dfc0a067c 100644 --- a/phpBB/includes/extension/manager.php +++ b/phpBB/includes/extension/manager.php @@ -23,6 +23,7 @@ if (!defined('IN_PHPBB')) class phpbb_extension_manager { protected $db; + protected $config; protected $cache; protected $php_ext; protected $extensions; @@ -34,16 +35,18 @@ class phpbb_extension_manager * Creates a manager and loads information from database * * @param dbal $db A database connection + * @param phpbb_config $config phpbb_config * @param string $extension_table The name of the table holding extensions * @param string $phpbb_root_path Path to the phpbb includes directory. * @param string $php_ext php file extension * @param phpbb_cache_driver_interface $cache A cache instance or null * @param string $cache_name The name of the cache variable, defaults to _ext */ - public function __construct(dbal $db, $extension_table, $phpbb_root_path, $php_ext = '.php', phpbb_cache_driver_interface $cache = null, $cache_name = '_ext') + public function __construct(dbal $db, phpbb_config $config, $extension_table, $phpbb_root_path, $php_ext = '.php', phpbb_cache_driver_interface $cache = null, $cache_name = '_ext') { $this->phpbb_root_path = $phpbb_root_path; $this->db = $db; + $this->config = $config; $this->cache = $cache; $this->php_ext = $php_ext; $this->extension_table = $extension_table; @@ -130,7 +133,7 @@ class phpbb_extension_manager */ public function get_extension_metadata($name, phpbb_template $template) { - return new phpbb_extension_metadata_manager($name, $this->db, $this, $this->phpbb_root_path, $this->phpEx, $template); + return new phpbb_extension_metadata_manager($name, $this->db, $this, $this->phpbb_root_path, $this->php_ext, $template, $this->config); } /** diff --git a/phpBB/includes/extension/metadata_manager.php b/phpBB/includes/extension/metadata_manager.php index 6af02e47b7..0e0b609a68 100644 --- a/phpBB/includes/extension/metadata_manager.php +++ b/phpBB/includes/extension/metadata_manager.php @@ -31,18 +31,71 @@ class phpbb_extension_metadata_manager public $metadata; protected $metadata_file; + /** + * Array of validation regular expressions, see __call() + * + * @var mixed + */ + protected $validation = array( + 'name' => '#^[a-zA-Z0-9_\x7f-\xff]{2,}/[a-zA-Z0-9_\x7f-\xff]{2,}$#', + 'type' => '#^phpbb3-extension$#', + 'description' => '#.*#', + 'version' => '#.+#', + 'licence' => '#.+#', + 'extra' => array( + 'display-name' => '#.*#', + ), + ); + + /** + * Magic method to catch validation calls + * + * @param string $name + * @param mixed $arguments + * @return int + */ + public function __call($name, $arguments) + { + // Validation Magic methods + if (strpos($name, 'validate_') === 0) + { + // Remove validate_ + $name = substr($name, 9); + + // Replace underscores with dashes (underscores are not used) + $name = str_replace('_', '-', $name); + + if (strpos($name, 'extra-') === 0) + { + // Remove extra_ + $name = substr($name, 6); + + if (isset($this->validation['extra'][$name])) + { + // Extra means it's optional, so return true if it does not exist + return (isset($this->metadata['extra'][$name])) ? preg_match($this->validation['extra'][$name], $this->metadata['extra'][$name]) : true; + } + } + else if (isset($this->validation[$name])) + { + return preg_match($this->validation[$name], $this->metadata[$name]); + } + } + } + /** * Creates the metadata manager - * + * * @param dbal $db A database connection * @param string $extension_manager An instance of the phpbb extension manager * @param string $phpbb_root_path Path to the phpbb includes directory. * @param string $phpEx php file extension */ - public function __construct($ext_name, dbal $db, phpbb_extension_manager $extension_manager, $phpbb_root_path, $phpEx = '.php', phpbb_template $template) + public function __construct($ext_name, dbal $db, phpbb_extension_manager $extension_manager, $phpbb_root_path, $phpEx = '.php', phpbb_template $template, phpbb_config $config) { $this->phpbb_root_path = $phpbb_root_path; $this->db = $db; + $this->config = $config; $this->phpEx = $phpEx; $this->template = $template; $this->extension_manager = $extension_manager; @@ -53,12 +106,11 @@ class phpbb_extension_metadata_manager /** * 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. - * @param boolean $template_output True if you want the requested metadata assigned to template vars (only works on the 'all" case - * @return array Contains all of the requested metadata + * + * @param string $element All for all metadata that it has and is valid, otherwise specify which section you want by its shorthand term. + * @return bool|array Contains all of the requested metadata or bool False if not valid */ - public function get_metadata($element = 'all', $template_output = false) + public function get_metadata($element = 'all') { // TODO: Check ext_name exists and is an extension that exists if (!$this->set_metadata_file()) @@ -66,34 +118,37 @@ class phpbb_extension_metadata_manager return false; } + // Fetch the metadata if (!$this->fetch_metadata()) { return false; } - switch ($element) + // Clean the metadata + if (!$this->clean_metadata_array()) + { + return false; + } + + switch ($element) { case 'all': default: - if (!$this->clean_metadata_array()) + // Validate the metadata + if (!$this->validate_metadata_array()) { return false; } - if ($template_output) - { - $this->output_template_data(); - } - return $this->metadata; break; - + case 'name': return ($this->validate_name()) ? $this->metadata['name'] : false; break; - + case 'display-name': - if ($this->validate_extra_display_name()) + if (isset($this->metadata['extra']['display-name']) && $this->validate_extra_display_name()) { return $this->metadata['extra']['display-name']; } @@ -108,7 +163,7 @@ class phpbb_extension_metadata_manager /** * Sets the filepath of the metadata file - * + * * @return boolean Set to true if it exists */ private function set_metadata_file() @@ -129,122 +184,156 @@ class phpbb_extension_metadata_manager } /** - * This array handles the validation and cleaning of the array - * - * @return array Contains the cleaned and validated metadata array + * Gets the contents of the composer.json file + * + * @return bool True of false (if loading succeeded or failed) */ - private function clean_metadata_array() - { - if (!$this->validate_name() || !$this->validate_type() || !$this->validate_licence() || !$this->validate_description() || !$this->validate_version() || !$this->validate_require_phpbb() || !$this->validate_extra_display_name()) + private function fetch_metadata() + { + if (!file_exists($this->metadata_file)) { return false; } - - $this->check_for_optional(true); + else + { + if (!($file_contents = file_get_contents($this->metadata_file))) + { + return false; + } -// TODO: Remove all parts of the array we don't want or shouldn't be there due to nub mod authors -// $this->metadata = $metadata_finished; + if (($metadata = json_decode($file_contents, true)) === NULL) + { + return false; + } - return $this->metadata; - } + $this->metadata = $metadata; - /** - * Validates the contents of the name field - * - * @return boolean True when passes validation - */ - private function validate_name() - { - return preg_match('#^[a-zA-Z0-9_\x7f-\xff]{2,}/[a-zA-Z0-9_\x7f-\xff]{2,}$#', $this->metadata['name']); + return true; + } } /** - * Validates the contents of the type field - * - * @return boolean True when passes validation + * This array handles the validation and cleaning of the array + * + * @return array Contains the cleaned and validated metadata array */ - private function validate_type() + private function clean_metadata_array() { - return $this->metadata['type'] == 'phpbb3-extension'; - } +// TODO: Remove all parts of the array we don't want or shouldn't be there due to nub mod authors +// $this->metadata = $metadata_finished; - /** - * Validates the contents of the description field - * - * @return boolean True when passes validation - */ - private function validate_description() - { - return true;//preg_match('#^{10,}$#', $this->metadata['description']); + return $this->metadata; } /** - * Validates the contents of the version field - * - * @return boolean True when passes validation + * This array handles the validation of strings + * + * @return bool True if validation succeeded, False if failed */ - private function validate_version() + public function validate_metadata_array() { - return preg_match('#^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}#', $this->metadata['version']); + $validate = array( + 'name', + 'type', + 'licence', + 'description', + 'version', + 'extra_display-name', + ); + + foreach ($validate as $type) + { + $type = 'validate_' . $type; + + if (!$this->$type()) + { + return false; + } + } + + return true; } /** - * Validates the contents of the license field - * - * @return boolean True when passes validation + * This array handles the verification that this extension can be enabled on this board + * + * @return bool True if validation succeeded, False if failed */ - private function validate_licence() + public function validate_enable() { - // Nothing to validate except existence - return isset($this->metadata['licence']); + $validate = array( + 'require_phpbb', + 'require_php', + ); + + foreach ($validate as $type) + { + $type = 'validate_' . $type; + + if (!$this->$type()) + { + return false; + } + } + + return true; } + /** * Validates the contents of the phpbb requirement field - * + * * @return boolean True when passes validation */ private function validate_require_phpbb() { - return (preg_match('#^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}$#', $this->metadata['require']['phpbb']) && version_compare($this->metadata['require']['phpbb'], '3.1.0', '>=')); + if (!isset($this->metadata['require']['phpbb'])) + { + return true; + } + + return $this->_validate_version($this->metadata['require']['phpbb'], $this->config['version']); } /** - * Validates the contents of the display name field - * + * Validates the contents of the php requirement field + * * @return boolean True when passes validation */ - private function validate_extra_display_name() + private function validate_require_php() { - return true;//preg_match('#^[a-zA-Z0-9_]{2,0}$#', $this->metadata['name']); + if (!isset($this->metadata['require']['php'])) + { + return true; + } + + return $this->_validate_version($this->metadata['require']['php'], phpversion()); } /** - * Checks which optional fields exist - * - * @return boolean False if any that exist fail validation, otherwise true. - */ - public function check_for_optional() + * Version validation helper + * + * @param string $string The string for comparing to a version + * @param string $current_version The version to compare to + * @return bool True/False if meets version requirements + */ + private function _validate_version($string, $current_version) { - if ((isset($this->metadata['require']['php']) && !$this->validate_require_php()) || (isset($this->metadata['time']) && !$this->validate_time()) || (isset($this->metadata['validate_homepage']) && !$this->validate_homepage())) + // Allow them to specify their own comparison operator (ex: <3.1.2, >=3.1.0) + $comparison_matches = false; + preg_match('#[=<>]+#', $string, $comparison_matches); + + if (!empty($comparison_matches)) { - return false; + return version_compare($current_version, str_replace(array($comparison_matches[0], ' '), '', $string), $comparison_matches[0]); } - } - /** - * Validates the contents of the php requirement field - * - * @return boolean True when passes validation - */ - private function validate_require_php() - { - return (preg_match('#^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}$#', $this->metadata['require']['php']) && version_compare($this->metadata['require']['php'], phpversion(), '>=')); + return version_compare($current_version, $string, '>='); } /** * Validates the contents of the time field - * + * * @return boolean True when passes validation */ private function validate_time() @@ -255,7 +344,7 @@ class phpbb_extension_metadata_manager /** * Validates the contents of the homepage field - * + * * @return boolean True when passes validation */ private function validate_homepage() @@ -265,7 +354,7 @@ class phpbb_extension_metadata_manager /** * Validates the contents of the authors field - * + * * @return boolean True when passes validation */ private function validate_authors() @@ -291,38 +380,9 @@ class phpbb_extension_metadata_manager return true; } - /** - * Gets the contents of the composer.json file - * - * @return bool True of false (if loading succeeded or failed) - */ - private function fetch_metadata() - { - if (!file_exists($this->metadata_file)) - { - return false; - } - else - { - if (!($file_contents = file_get_contents($this->metadata_file))) - { - return false; - } - - if (($metadata = json_decode($file_contents, true)) === NULL) - { - return false; - } - - $this->metadata = $metadata; - - return true; - } - } - /** * Outputs the metadata into the template - * + * * @return null */ public function output_template_data() @@ -350,7 +410,7 @@ class phpbb_extension_metadata_manager 'AUTHOR_ROLE' => (isset($author['role'])) ? htmlspecialchars($author['role']) : '', )); } - + return; } } -- cgit v1.2.1 From 4314284de12ceac5ae0792f3f4014b765d75d332 Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Mon, 23 Jul 2012 15:22:48 -0500 Subject: [ticket/10631] Remove code duplication PHPBB3-10631 --- phpBB/includes/extension/metadata_manager.php | 33 ++++++++++++++++----------- 1 file changed, 20 insertions(+), 13 deletions(-) (limited to 'phpBB/includes/extension') diff --git a/phpBB/includes/extension/metadata_manager.php b/phpBB/includes/extension/metadata_manager.php index 0e0b609a68..c5e9baf1e7 100644 --- a/phpBB/includes/extension/metadata_manager.php +++ b/phpBB/includes/extension/metadata_manager.php @@ -232,22 +232,29 @@ class phpbb_extension_metadata_manager */ public function validate_metadata_array() { - $validate = array( - 'name', - 'type', - 'licence', - 'description', - 'version', - 'extra_display-name', - ); - - foreach ($validate as $type) + foreach ($this->validation as $name => $regex) { - $type = 'validate_' . $type; + if (is_array($regex)) + { + foreach ($regex as $extra_name => $extra_regex) + { + $type = 'validate_' . $name . '_' . $extra_name; - if (!$this->$type()) + if (!$this->$type()) + { + return false; + } + } + } + else { - return false; + + $type = 'validate_' . $name; + + if (!$this->$type()) + { + return false; + } } } -- cgit v1.2.1 From 8df9963fcc7e2962b6b4e1e32e809f2d8fe00835 Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Mon, 23 Jul 2012 15:39:13 -0500 Subject: [ticket/10631] Additional validation PHPBB3-10631 --- phpBB/includes/extension/metadata_manager.php | 76 +++++++++------------------ 1 file changed, 25 insertions(+), 51 deletions(-) (limited to 'phpBB/includes/extension') diff --git a/phpBB/includes/extension/metadata_manager.php b/phpBB/includes/extension/metadata_manager.php index c5e9baf1e7..d2dc72e5fc 100644 --- a/phpBB/includes/extension/metadata_manager.php +++ b/phpBB/includes/extension/metadata_manager.php @@ -42,6 +42,7 @@ class phpbb_extension_metadata_manager 'description' => '#.*#', 'version' => '#.+#', 'licence' => '#.+#', + //'homepage' => '#([\d\w-.]+?\.(a[cdefgilmnoqrstuwz]|b[abdefghijmnorstvwyz]|c[acdfghiklmnoruvxyz]|d[ejkmnoz]|e[ceghrst]|f[ijkmnor]|g[abdefghilmnpqrstuwy]|h[kmnrtu]|i[delmnoqrst]|j[emop]|k[eghimnprwyz]|l[abcikrstuvy]|m[acdghklmnopqrstuvwxyz]|n[acefgilopruz]|om|p[aefghklmnrstwy]|qa|r[eouw]|s[abcdeghijklmnortuvyz]|t[cdfghjkmnoprtvwz]|u[augkmsyz]|v[aceginu]|w[fs]|y[etu]|z[amw]|aero|arpa|biz|com|coop|edu|info|int|gov|mil|museum|name|net|org|pro)(\b|\W(? array( 'display-name' => '#.*#', ), @@ -76,7 +77,7 @@ class phpbb_extension_metadata_manager return (isset($this->metadata['extra'][$name])) ? preg_match($this->validation['extra'][$name], $this->metadata['extra'][$name]) : true; } } - else if (isset($this->validation[$name])) + else if (isset($this->validation[$name]) && isset($this->metadata[$name])) { return preg_match($this->validation[$name], $this->metadata[$name]); } @@ -258,6 +259,29 @@ class phpbb_extension_metadata_manager } } + return $this->validate_authors(); + } + + /** + * Validates the contents of the authors field + * + * @return boolean True when passes validation + */ + private function validate_authors() + { + if (empty($this->metadata['authors'])) + { + return false; + } + + foreach ($this->metadata['authors'] as $author) + { + if (!isset($author['name'])) + { + return false; + } + } + return true; } @@ -338,55 +362,6 @@ class phpbb_extension_metadata_manager return version_compare($current_version, $string, '>='); } - /** - * Validates the contents of the time field - * - * @return boolean True when passes validation - */ - private function validate_time() - { - // Need to validate - return true; - } - - /** - * Validates the contents of the homepage field - * - * @return boolean True when passes validation - */ - private function validate_homepage() - { - return preg_match('#([\d\w-.]+?\.(a[cdefgilmnoqrstuwz]|b[abdefghijmnorstvwyz]|c[acdfghiklmnoruvxyz]|d[ejkmnoz]|e[ceghrst]|f[ijkmnor]|g[abdefghilmnpqrstuwy]|h[kmnrtu]|i[delmnoqrst]|j[emop]|k[eghimnprwyz]|l[abcikrstuvy]|m[acdghklmnopqrstuvwxyz]|n[acefgilopruz]|om|p[aefghklmnrstwy]|qa|r[eouw]|s[abcdeghijklmnortuvyz]|t[cdfghjkmnoprtvwz]|u[augkmsyz]|v[aceginu]|w[fs]|y[etu]|z[amw]|aero|arpa|biz|com|coop|edu|info|int|gov|mil|museum|name|net|org|pro)(\b|\W(?metadata['homepage']); - } - - /** - * Validates the contents of the authors field - * - * @return boolean True when passes validation - */ - private function validate_authors() - { - // Need to validate - $number_authors = sizeof($this->metadata['authors']); // Might be helpful later on - - if (!isset($this->metadata['authors']['1'])) - { - return false; - } - else - { - foreach ($this->metadata['authors'] as $author) - { - if (!isset($author['name'])) - { - return false; - } - } - } - - return true; - } - /** * Outputs the metadata into the template * @@ -394,7 +369,6 @@ class phpbb_extension_metadata_manager */ public function output_template_data() { - $this->template->assign_vars(array( 'MD_NAME' => htmlspecialchars($this->metadata['name']), 'MD_TYPE' => htmlspecialchars($this->metadata['type']), -- cgit v1.2.1 From 106c105113886f9a9e603dbb11549c06049b255f Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Mon, 23 Jul 2012 18:22:35 -0500 Subject: [ticket/10631] Fix some issues as noted in github comments, significantly simplified validation PHPBB3-10631 --- phpBB/includes/extension/manager.php | 2 +- phpBB/includes/extension/metadata_manager.php | 144 ++++++++------------------ 2 files changed, 44 insertions(+), 102 deletions(-) (limited to 'phpBB/includes/extension') diff --git a/phpBB/includes/extension/manager.php b/phpBB/includes/extension/manager.php index 9dfc0a067c..9342c936f9 100644 --- a/phpBB/includes/extension/manager.php +++ b/phpBB/includes/extension/manager.php @@ -131,7 +131,7 @@ class phpbb_extension_manager * @param string $template The template manager * @return phpbb_extension_metadata_manager Instance of the metadata manager */ - public function get_extension_metadata($name, phpbb_template $template) + public function get_extension_metadata_manager($name, phpbb_template $template) { return new phpbb_extension_metadata_manager($name, $this->db, $this, $this->phpbb_root_path, $this->php_ext, $template, $this->config); } diff --git a/phpBB/includes/extension/metadata_manager.php b/phpBB/includes/extension/metadata_manager.php index d2dc72e5fc..aa163b1190 100644 --- a/phpBB/includes/extension/metadata_manager.php +++ b/phpBB/includes/extension/metadata_manager.php @@ -28,62 +28,9 @@ class phpbb_extension_metadata_manager protected $phpbb_root_path; protected $template; protected $ext_name; - public $metadata; + protected $metadata; protected $metadata_file; - /** - * Array of validation regular expressions, see __call() - * - * @var mixed - */ - protected $validation = array( - 'name' => '#^[a-zA-Z0-9_\x7f-\xff]{2,}/[a-zA-Z0-9_\x7f-\xff]{2,}$#', - 'type' => '#^phpbb3-extension$#', - 'description' => '#.*#', - 'version' => '#.+#', - 'licence' => '#.+#', - //'homepage' => '#([\d\w-.]+?\.(a[cdefgilmnoqrstuwz]|b[abdefghijmnorstvwyz]|c[acdfghiklmnoruvxyz]|d[ejkmnoz]|e[ceghrst]|f[ijkmnor]|g[abdefghilmnpqrstuwy]|h[kmnrtu]|i[delmnoqrst]|j[emop]|k[eghimnprwyz]|l[abcikrstuvy]|m[acdghklmnopqrstuvwxyz]|n[acefgilopruz]|om|p[aefghklmnrstwy]|qa|r[eouw]|s[abcdeghijklmnortuvyz]|t[cdfghjkmnoprtvwz]|u[augkmsyz]|v[aceginu]|w[fs]|y[etu]|z[amw]|aero|arpa|biz|com|coop|edu|info|int|gov|mil|museum|name|net|org|pro)(\b|\W(? array( - 'display-name' => '#.*#', - ), - ); - - /** - * Magic method to catch validation calls - * - * @param string $name - * @param mixed $arguments - * @return int - */ - public function __call($name, $arguments) - { - // Validation Magic methods - if (strpos($name, 'validate_') === 0) - { - // Remove validate_ - $name = substr($name, 9); - - // Replace underscores with dashes (underscores are not used) - $name = str_replace('_', '-', $name); - - if (strpos($name, 'extra-') === 0) - { - // Remove extra_ - $name = substr($name, 6); - - if (isset($this->validation['extra'][$name])) - { - // Extra means it's optional, so return true if it does not exist - return (isset($this->metadata['extra'][$name])) ? preg_match($this->validation['extra'][$name], $this->metadata['extra'][$name]) : true; - } - } - else if (isset($this->validation[$name]) && isset($this->metadata[$name])) - { - return preg_match($this->validation[$name], $this->metadata[$name]); - } - } - } - /** * Creates the metadata manager * @@ -136,7 +83,7 @@ class phpbb_extension_metadata_manager case 'all': default: // Validate the metadata - if (!$this->validate_metadata_array()) + if (!$this->validate()) { return false; } @@ -145,17 +92,17 @@ class phpbb_extension_metadata_manager break; case 'name': - return ($this->validate_name()) ? $this->metadata['name'] : false; + return ($this->validate('name')) ? $this->metadata['name'] : false; break; case 'display-name': - if (isset($this->metadata['extra']['display-name']) && $this->validate_extra_display_name()) + if (isset($this->metadata['extra']['display-name'])) { return $this->metadata['extra']['display-name']; } else { - return ($this->validate_name()) ? $this->metadata['name'] : false; + return ($this->validate('name')) ? $this->metadata['name'] : false; } break; // TODO: Add remaining cases as needed @@ -216,7 +163,7 @@ class phpbb_extension_metadata_manager /** * This array handles the validation and cleaning of the array * - * @return array Contains the cleaned and validated metadata array + * @return array Contains the cleaned metadata array */ private function clean_metadata_array() { @@ -227,40 +174,44 @@ class phpbb_extension_metadata_manager } /** - * This array handles the validation of strings - * - * @return bool True if validation succeeded, False if failed - */ - public function validate_metadata_array() - { - foreach ($this->validation as $name => $regex) - { - if (is_array($regex)) - { - foreach ($regex as $extra_name => $extra_regex) - { - $type = 'validate_' . $name . '_' . $extra_name; + * Validate fields + * + * @param string $name ("all" for display and enable validation + * "display" for name, type, and authors + * "name", "type") + * @return Bool False if validation fails, true if valid + */ + 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' => '#^phpbb3-extension$#', + 'licence' => '#.+#', + 'version' => '#.+#', + ); + + if (isset($fields[$name])) + { + return (isset($this->metadata[$name])) ? (bool) preg_match($this->validation[$name], $this->metadata[$name]) : false; + } - if (!$this->$type()) - { - return false; - } - } - } - else + // Validate all fields + if ($name == 'all') + { + foreach ($fields as $field => $data) { - - $type = 'validate_' . $name; - - if (!$this->$type()) + if (!$this->validate($field)) { return false; } } + + return $this->validate_authors(); } - return $this->validate_authors(); - } + return true; + } /** * Validates the contents of the authors field @@ -292,19 +243,10 @@ class phpbb_extension_metadata_manager */ public function validate_enable() { - $validate = array( - 'require_phpbb', - 'require_php', - ); - - foreach ($validate as $type) + // Check for phpBB, PHP versions + if (!$this->validate_require_phpbb || !$this->validate_require_php) { - $type = 'validate_' . $type; - - if (!$this->$type()) - { - return false; - } + return false; } return true; @@ -372,10 +314,10 @@ class phpbb_extension_metadata_manager $this->template->assign_vars(array( 'MD_NAME' => htmlspecialchars($this->metadata['name']), 'MD_TYPE' => htmlspecialchars($this->metadata['type']), - 'MD_DESCRIPTION' => htmlspecialchars($this->metadata['description']), + 'MD_DESCRIPTION' => (isset($this->metadata['description'])) ? htmlspecialchars($this->metadata['description']) : '', 'MD_HOMEPAGE' => (isset($this->metadata['homepage'])) ? $this->metadata['homepage'] : '', - 'MD_VERSION' => htmlspecialchars($this->metadata['version']), - 'MD_TIME' => htmlspecialchars($this->metadata['time']), + 'MD_VERSION' => (isset($this->metadata['version'])) ? htmlspecialchars($this->metadata['version']) : '', + 'MD_TIME' => (isset($this->metadata['time'])) ? htmlspecialchars($this->metadata['time']) : '', 'MD_LICENCE' => htmlspecialchars($this->metadata['licence']), 'MD_REQUIRE_PHP' => (isset($this->metadata['require']['php'])) ? htmlspecialchars($this->metadata['require']['php']) : '', 'MD_REQUIRE_PHPBB' => (isset($this->metadata['require']['phpbb'])) ? htmlspecialchars($this->metadata['require']['phpbb']) : '', @@ -386,7 +328,7 @@ class phpbb_extension_metadata_manager { $this->template->assign_block_vars('md_authors', array( 'AUTHOR_NAME' => htmlspecialchars($author['name']), - 'AUTHOR_EMAIL' => $author['email'], + 'AUTHOR_EMAIL' => (isset($author['email'])) ? $author['email'] : '', 'AUTHOR_HOMEPAGE' => (isset($author['homepage'])) ? $author['homepage'] : '', 'AUTHOR_ROLE' => (isset($author['role'])) ? htmlspecialchars($author['role']) : '', )); -- cgit v1.2.1 From 89f4cf6a8c10f9b0875cf7f278016aff67eb38fc Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Mon, 23 Jul 2012 19:46:21 -0500 Subject: [ticket/10631] Use exceptions for errors. Build action list dynamically. PHPBB3-10631 --- phpBB/includes/extension/metadata_manager.php | 72 +++++++++++++-------------- 1 file changed, 36 insertions(+), 36 deletions(-) (limited to 'phpBB/includes/extension') diff --git a/phpBB/includes/extension/metadata_manager.php b/phpBB/includes/extension/metadata_manager.php index aa163b1190..126331ce1c 100644 --- a/phpBB/includes/extension/metadata_manager.php +++ b/phpBB/includes/extension/metadata_manager.php @@ -56,27 +56,18 @@ class phpbb_extension_metadata_manager * 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 bool|array Contains all of the requested metadata or bool False if not valid + * @return array Contains all of the requested metadata, throws an exception on failure */ public function get_metadata($element = 'all') { // TODO: Check ext_name exists and is an extension that exists - if (!$this->set_metadata_file()) - { - return false; - } + $this->set_metadata_file(); // Fetch the metadata - if (!$this->fetch_metadata()) - { - return false; - } + $this->fetch_metadata(); // Clean the metadata - if (!$this->clean_metadata_array()) - { - return false; - } + $this->clean_metadata_array(); switch ($element) { @@ -112,46 +103,42 @@ class phpbb_extension_metadata_manager /** * Sets the filepath of the metadata file * - * @return boolean Set to true if it exists + * @return boolean Set to true if it exists, throws an exception on failure */ 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'; + $metadata_filepath = $this->phpbb_root_path . $ext_filepath . 'composer.json'; $this->metadata_file = $metadata_filepath; if (!file_exists($this->metadata_file)) { - return false; - } - else - { - return true; + throw new phpbb_exception_metadata(phpbb_exception_metadata::FILE_DOES_NOT_EXIST, $this->metadata_file); } } /** * Gets the contents of the composer.json file * - * @return bool True of false (if loading succeeded or failed) + * @return bool True if success, throws an exception on failure */ private function fetch_metadata() { if (!file_exists($this->metadata_file)) { - return false; + throw new phpbb_exception_metadata(phpbb_exception_metadata::FILE_DOES_NOT_EXIST, $this->metadata_file); } else { if (!($file_contents = file_get_contents($this->metadata_file))) { - return false; + throw new phpbb_exception_metadata(phpbb_exception_metadata::FILE_GET_CONTENTS, $this->metadata_file); } if (($metadata = json_decode($file_contents, true)) === NULL) { - return false; + throw new phpbb_exception_metadata(phpbb_exception_metadata::JSON_DECODE, $this->metadata_file); } $this->metadata = $metadata; @@ -161,7 +148,7 @@ class phpbb_extension_metadata_manager } /** - * This array handles the validation and cleaning of the array + * This array handles the cleaning of the array * * @return array Contains the cleaned metadata array */ @@ -179,7 +166,7 @@ class phpbb_extension_metadata_manager * @param string $name ("all" for display and enable validation * "display" for name, type, and authors * "name", "type") - * @return Bool False if validation fails, true if valid + * @return Bool True if valid, throws an exception if invalid */ public function validate($name = 'display') { @@ -193,21 +180,34 @@ class phpbb_extension_metadata_manager if (isset($fields[$name])) { - return (isset($this->metadata[$name])) ? (bool) preg_match($this->validation[$name], $this->metadata[$name]) : false; + if (!isset($this->metadata[$name])) + { + throw new phpbb_exception_metadata(phpbb_exception_metadata::NOT_SET, $name); + } + + if (!preg_match($fields[$name], $this->metadata[$name])) + { + throw new phpbb_exception_metadata(phpbb_exception_metadata::INVALID, $name); + } } // Validate all fields if ($name == 'all') + { + $this->validate('display'); + + $this->validate_enable(); + } + + // Validate display fields + if ($name == 'display') { foreach ($fields as $field => $data) { - if (!$this->validate($field)) - { - return false; - } + $this->validate($field); } - return $this->validate_authors(); + $this->validate_authors(); } return true; @@ -216,20 +216,20 @@ class phpbb_extension_metadata_manager /** * Validates the contents of the authors field * - * @return boolean True when passes validation + * @return boolean True when passes validation, throws exception if invalid */ private function validate_authors() { if (empty($this->metadata['authors'])) { - return false; + throw new phpbb_exception_metadata(phpbb_exception_metadata::NOT_SET, 'authors'); } foreach ($this->metadata['authors'] as $author) { if (!isset($author['name'])) { - return false; + throw new phpbb_exception_metadata(phpbb_exception_metadata::NOT_SET, 'author name'); } } @@ -244,7 +244,7 @@ class phpbb_extension_metadata_manager public function validate_enable() { // Check for phpBB, PHP versions - if (!$this->validate_require_phpbb || !$this->validate_require_php) + if (!$this->validate_require_phpbb() || !$this->validate_require_php()) { return false; } -- cgit v1.2.1 From 2a7e1292919ed1397a3f1951e510d84565d002d7 Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Mon, 23 Jul 2012 20:28:04 -0500 Subject: [ticket/10631] Simplify exceptions PHPBB-10631 --- phpBB/includes/extension/exception.php | 27 +++++++++++++++++++++++++++ phpBB/includes/extension/metadata_manager.php | 16 ++++++++-------- 2 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 phpBB/includes/extension/exception.php (limited to 'phpBB/includes/extension') diff --git a/phpBB/includes/extension/exception.php b/phpBB/includes/extension/exception.php new file mode 100644 index 0000000000..e08a8912ea --- /dev/null +++ b/phpBB/includes/extension/exception.php @@ -0,0 +1,27 @@ +getMessage(); + } +} \ No newline at end of file diff --git a/phpBB/includes/extension/metadata_manager.php b/phpBB/includes/extension/metadata_manager.php index 126331ce1c..27b04d4c08 100644 --- a/phpBB/includes/extension/metadata_manager.php +++ b/phpBB/includes/extension/metadata_manager.php @@ -114,7 +114,7 @@ class phpbb_extension_metadata_manager if (!file_exists($this->metadata_file)) { - throw new phpbb_exception_metadata(phpbb_exception_metadata::FILE_DOES_NOT_EXIST, $this->metadata_file); + throw new phpbb_extension_exception('The required file does not exist: ' . $this->metadata_file); } } @@ -127,18 +127,18 @@ class phpbb_extension_metadata_manager { if (!file_exists($this->metadata_file)) { - throw new phpbb_exception_metadata(phpbb_exception_metadata::FILE_DOES_NOT_EXIST, $this->metadata_file); + throw new phpbb_extension_exception('The required file does not exist: ' . $this->metadata_file); } else { if (!($file_contents = file_get_contents($this->metadata_file))) { - throw new phpbb_exception_metadata(phpbb_exception_metadata::FILE_GET_CONTENTS, $this->metadata_file); + throw new phpbb_extension_exception('file_get_contents failed on ' . $this->metadata_file); } if (($metadata = json_decode($file_contents, true)) === NULL) { - throw new phpbb_exception_metadata(phpbb_exception_metadata::JSON_DECODE, $this->metadata_file); + throw new phpbb_extension_exception('json_decode failed on ' . $this->metadata_file); } $this->metadata = $metadata; @@ -182,12 +182,12 @@ class phpbb_extension_metadata_manager { if (!isset($this->metadata[$name])) { - throw new phpbb_exception_metadata(phpbb_exception_metadata::NOT_SET, $name); + throw new phpbb_extension_exception("Required meta field '$name' has not been set."); } if (!preg_match($fields[$name], $this->metadata[$name])) { - throw new phpbb_exception_metadata(phpbb_exception_metadata::INVALID, $name); + throw new phpbb_extension_exception("Meta field '$name' is invalid."); } } @@ -222,14 +222,14 @@ class phpbb_extension_metadata_manager { if (empty($this->metadata['authors'])) { - throw new phpbb_exception_metadata(phpbb_exception_metadata::NOT_SET, 'authors'); + throw new phpbb_extension_exception("Required meta field 'authors' has not been set."); } foreach ($this->metadata['authors'] as $author) { if (!isset($author['name'])) { - throw new phpbb_exception_metadata(phpbb_exception_metadata::NOT_SET, 'author name'); + throw new phpbb_extension_exception("Required meta field 'author name' has not been set."); } } -- cgit v1.2.1 From 747c16240fd56be0e24b4c54f01c82aa0a78b91e Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Mon, 23 Jul 2012 20:40:54 -0500 Subject: [ticket/10631] get_extension_metadata_manager -> create_extension_metadata_manager PHPBB3-10631 --- phpBB/includes/extension/manager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes/extension') diff --git a/phpBB/includes/extension/manager.php b/phpBB/includes/extension/manager.php index 9342c936f9..9a518c215f 100644 --- a/phpBB/includes/extension/manager.php +++ b/phpBB/includes/extension/manager.php @@ -131,7 +131,7 @@ class phpbb_extension_manager * @param string $template The template manager * @return phpbb_extension_metadata_manager Instance of the metadata manager */ - public function get_extension_metadata_manager($name, phpbb_template $template) + public function create_extension_metadata_manager($name, phpbb_template $template) { return new phpbb_extension_metadata_manager($name, $this->db, $this, $this->phpbb_root_path, $this->php_ext, $template, $this->config); } -- cgit v1.2.1 From 500879520c40a71f0b83799ab3e59c86c12a801a Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Sat, 28 Jul 2012 14:59:55 -0500 Subject: [ticket/10631] Metadata manager tests PHPBB3-10631 --- phpBB/includes/extension/metadata_manager.php | 59 ++++++++++++++------------- 1 file changed, 30 insertions(+), 29 deletions(-) (limited to 'phpBB/includes/extension') diff --git a/phpBB/includes/extension/metadata_manager.php b/phpBB/includes/extension/metadata_manager.php index 27b04d4c08..c7f52b7c02 100644 --- a/phpBB/includes/extension/metadata_manager.php +++ b/phpBB/includes/extension/metadata_manager.php @@ -178,36 +178,37 @@ class phpbb_extension_metadata_manager 'version' => '#.+#', ); - if (isset($fields[$name])) + switch ($name) { - if (!isset($this->metadata[$name])) - { - throw new phpbb_extension_exception("Required meta field '$name' has not been set."); - } - - if (!preg_match($fields[$name], $this->metadata[$name])) - { - throw new phpbb_extension_exception("Meta field '$name' is invalid."); - } - } - - // Validate all fields - if ($name == 'all') - { - $this->validate('display'); + case 'all': + $this->validate('display'); - $this->validate_enable(); - } + $this->validate_enable(); + break; - // Validate display fields - if ($name == 'display') - { - foreach ($fields as $field => $data) - { - $this->validate($field); - } + case 'display': + foreach ($fields as $field => $data) + { + $this->validate($field); + } - $this->validate_authors(); + $this->validate_authors(); + break; + + default: + if (isset($fields[$name])) + { + if (!isset($this->metadata[$name])) + { + throw new phpbb_extension_exception("Required meta field '$name' has not been set."); + } + + if (!preg_match($fields[$name], $this->metadata[$name])) + { + throw new phpbb_extension_exception("Meta field '$name' is invalid."); + } + } + break; } return true; @@ -218,7 +219,7 @@ class phpbb_extension_metadata_manager * * @return boolean True when passes validation, throws exception if invalid */ - private function validate_authors() + public function validate_authors() { if (empty($this->metadata['authors'])) { @@ -258,7 +259,7 @@ class phpbb_extension_metadata_manager * * @return boolean True when passes validation */ - private function validate_require_phpbb() + public function validate_require_phpbb() { if (!isset($this->metadata['require']['phpbb'])) { @@ -273,7 +274,7 @@ class phpbb_extension_metadata_manager * * @return boolean True when passes validation */ - private function validate_require_php() + public function validate_require_php() { if (!isset($this->metadata['require']['php'])) { -- cgit v1.2.1 From 7b643fe8a5fd3b92bb4db9eacb27645417004709 Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Sun, 5 Aug 2012 19:00:20 -0500 Subject: [ticket/10631] Make failure to meet ext enable requirements clearer Turn the blocks red on the details page if requirement is not met. Also changing a how the errors come up when trying to enable/disable an extension when they cannot be. PHPBB3-10631 --- phpBB/includes/extension/metadata_manager.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'phpBB/includes/extension') diff --git a/phpBB/includes/extension/metadata_manager.php b/phpBB/includes/extension/metadata_manager.php index c7f52b7c02..8a68e464a7 100644 --- a/phpBB/includes/extension/metadata_manager.php +++ b/phpBB/includes/extension/metadata_manager.php @@ -320,8 +320,13 @@ class phpbb_extension_metadata_manager 'MD_VERSION' => (isset($this->metadata['version'])) ? htmlspecialchars($this->metadata['version']) : '', 'MD_TIME' => (isset($this->metadata['time'])) ? htmlspecialchars($this->metadata['time']) : '', 'MD_LICENCE' => htmlspecialchars($this->metadata['licence']), - 'MD_REQUIRE_PHP' => (isset($this->metadata['require']['php'])) ? htmlspecialchars($this->metadata['require']['php']) : '', - 'MD_REQUIRE_PHPBB' => (isset($this->metadata['require']['phpbb'])) ? htmlspecialchars($this->metadata['require']['phpbb']) : '', + + 'MD_REQUIRE_PHP' => (isset($this->metadata['require']['php'])) ? htmlspecialchars($this->metadata['require']['php']) : '', + 'MD_REQUIRE_PHP_FAIL' => !$this->validate_require_php(), + + 'MD_REQUIRE_PHPBB' => (isset($this->metadata['require']['phpbb'])) ? htmlspecialchars($this->metadata['require']['phpbb']) : '', + 'MD_REQUIRE_PHPBB_FAIL' => !$this->validate_require_phpbb(), + 'MD_DISPLAY_NAME' => (isset($this->metadata['extra']['display-name'])) ? htmlspecialchars($this->metadata['extra']['display-name']) : '', )); -- cgit v1.2.1 From 323bbf9b523984a592dc17bf35d2bb91a435be1b Mon Sep 17 00:00:00 2001 From: Unknown Bliss Date: Fri, 17 Aug 2012 14:06:23 +0100 Subject: [ticket/10631] Adjust prefixes to be easier to understand PHPBB3-10631 --- phpBB/includes/extension/metadata_manager.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'phpBB/includes/extension') diff --git a/phpBB/includes/extension/metadata_manager.php b/phpBB/includes/extension/metadata_manager.php index 8a68e464a7..1e3bbe48c9 100644 --- a/phpBB/includes/extension/metadata_manager.php +++ b/phpBB/includes/extension/metadata_manager.php @@ -313,26 +313,26 @@ class phpbb_extension_metadata_manager public function output_template_data() { $this->template->assign_vars(array( - 'MD_NAME' => htmlspecialchars($this->metadata['name']), - 'MD_TYPE' => htmlspecialchars($this->metadata['type']), - 'MD_DESCRIPTION' => (isset($this->metadata['description'])) ? htmlspecialchars($this->metadata['description']) : '', - 'MD_HOMEPAGE' => (isset($this->metadata['homepage'])) ? $this->metadata['homepage'] : '', - 'MD_VERSION' => (isset($this->metadata['version'])) ? htmlspecialchars($this->metadata['version']) : '', - 'MD_TIME' => (isset($this->metadata['time'])) ? htmlspecialchars($this->metadata['time']) : '', - 'MD_LICENCE' => htmlspecialchars($this->metadata['licence']), + '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_LICENCE' => htmlspecialchars($this->metadata['licence']), - 'MD_REQUIRE_PHP' => (isset($this->metadata['require']['php'])) ? htmlspecialchars($this->metadata['require']['php']) : '', - 'MD_REQUIRE_PHP_FAIL' => !$this->validate_require_php(), + 'META_REQUIRE_PHP' => (isset($this->metadata['require']['php'])) ? htmlspecialchars($this->metadata['require']['php']) : '', + 'META_REQUIRE_PHP_FAIL' => !$this->validate_require_php(), - 'MD_REQUIRE_PHPBB' => (isset($this->metadata['require']['phpbb'])) ? htmlspecialchars($this->metadata['require']['phpbb']) : '', - 'MD_REQUIRE_PHPBB_FAIL' => !$this->validate_require_phpbb(), + 'META_REQUIRE_PHPBB' => (isset($this->metadata['require']['phpbb'])) ? htmlspecialchars($this->metadata['require']['phpbb']) : '', + 'META_REQUIRE_PHPBB_FAIL' => !$this->validate_require_phpbb(), - 'MD_DISPLAY_NAME' => (isset($this->metadata['extra']['display-name'])) ? htmlspecialchars($this->metadata['extra']['display-name']) : '', + '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('md_authors', array( + $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'] : '', -- cgit v1.2.1 From f05a175e3955d1dc1d2b85b9929ca4b30340ad4a Mon Sep 17 00:00:00 2001 From: Unknown Bliss Date: Fri, 17 Aug 2012 14:38:03 +0100 Subject: [ticket/10631] Fixing a few extension admin issues PHPBB3-10631 --- phpBB/includes/extension/metadata_manager.php | 3 --- 1 file changed, 3 deletions(-) (limited to 'phpBB/includes/extension') diff --git a/phpBB/includes/extension/metadata_manager.php b/phpBB/includes/extension/metadata_manager.php index 1e3bbe48c9..8c570830fe 100644 --- a/phpBB/includes/extension/metadata_manager.php +++ b/phpBB/includes/extension/metadata_manager.php @@ -60,7 +60,6 @@ class phpbb_extension_metadata_manager */ public function get_metadata($element = 'all') { - // TODO: Check ext_name exists and is an extension that exists $this->set_metadata_file(); // Fetch the metadata @@ -339,7 +338,5 @@ class phpbb_extension_metadata_manager 'AUTHOR_ROLE' => (isset($author['role'])) ? htmlspecialchars($author['role']) : '', )); } - - return; } } -- cgit v1.2.1 From 81f7f28cc33d896973c2912097103ea84fa14114 Mon Sep 17 00:00:00 2001 From: Unknown Bliss Date: Sat, 1 Sep 2012 21:30:35 +0100 Subject: [ticket/10631] Removing un-needed TODOs that are no longer needed. PHPBB3-10631 --- phpBB/includes/extension/metadata_manager.php | 4 ---- 1 file changed, 4 deletions(-) (limited to 'phpBB/includes/extension') diff --git a/phpBB/includes/extension/metadata_manager.php b/phpBB/includes/extension/metadata_manager.php index 8c570830fe..ea85bd3c4e 100644 --- a/phpBB/includes/extension/metadata_manager.php +++ b/phpBB/includes/extension/metadata_manager.php @@ -95,7 +95,6 @@ class phpbb_extension_metadata_manager return ($this->validate('name')) ? $this->metadata['name'] : false; } break; - // TODO: Add remaining cases as needed } } @@ -153,9 +152,6 @@ class phpbb_extension_metadata_manager */ private function clean_metadata_array() { -// TODO: Remove all parts of the array we don't want or shouldn't be there due to nub mod authors -// $this->metadata = $metadata_finished; - return $this->metadata; } -- cgit v1.2.1 From d7a626c70bdfd7aea5e10ec891230cbd2e94f862 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Mon, 25 Jun 2012 23:27:08 -0400 Subject: [ticket/10933] Expanded prose documentation for phpbb_extension_provider. PHPBB3-10933 --- phpBB/includes/extension/provider.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'phpBB/includes/extension') diff --git a/phpBB/includes/extension/provider.php b/phpBB/includes/extension/provider.php index d0541fa007..45b55e5cab 100644 --- a/phpBB/includes/extension/provider.php +++ b/phpBB/includes/extension/provider.php @@ -16,7 +16,15 @@ if (!defined('IN_PHPBB')) } /** -* Provides a set of items found in extensions +* Provides a set of items found in extensions. +* +* This abstract class is essentially a wrapper around item-specific +* finding logic. It handles storing the extension manager via constructor +* for the finding logic to use to find the items, and provides an +* iterator interface over the items found by the finding logic. +* +* Items could be anything, for example template paths or cron task names. +* Derived classes completely define what the items are. * * @package extension */ @@ -45,7 +53,7 @@ abstract class phpbb_extension_provider implements IteratorAggregate } /** - * Finds template paths using the extension manager. + * Finds items using the extension manager. * * @return array List of task names */ -- cgit v1.2.1 From 2400a300273e76f0af9189fdbfa7a5e3e04360e7 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Wed, 7 Nov 2012 19:19:07 -0500 Subject: [ticket/11154] Do not try to load any extensions when installing. Might be the right fix, might be not. Works for me. PHPBB3-11154 --- phpBB/includes/extension/manager.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'phpBB/includes/extension') diff --git a/phpBB/includes/extension/manager.php b/phpBB/includes/extension/manager.php index 9a518c215f..cfa6a0e000 100644 --- a/phpBB/includes/extension/manager.php +++ b/phpBB/includes/extension/manager.php @@ -67,6 +67,17 @@ class phpbb_extension_manager */ public function load_extensions() { + $this->extensions = array(); + + // Do not try to load any extensions when installing or updating + // Note: database updater invokes this code, and in 3.0 + // there is no extension table therefore the rest of this function + // fails + if (defined('IN_INSTALL')) + { + return; + } + $sql = 'SELECT * FROM ' . $this->extension_table; @@ -74,7 +85,6 @@ class phpbb_extension_manager $extensions = $this->db->sql_fetchrowset($result); $this->db->sql_freeresult($result); - $this->extensions = array(); foreach ($extensions as $extension) { $extension['ext_path'] = $this->get_extension_path($extension['ext_name']); -- cgit v1.2.1