Skip to content

Commit 0638ef4

Browse files
authored
Merge pull request #458 from ECYaz/fix/394-url-permalink
Reject links entered as contribution names or permalinks
2 parents bf9999b + 8bf264b commit 0638ef4

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

‎controller/contribution/manage.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ public function manage($contrib_type, $contrib)
216216
$authors,
217217
$this->settings['custom'],
218218
$this->contrib->contrib_name_clean,
219-
$this->settings['permalink']
219+
$this->settings['permalink'],
220+
$old_settings['contrib_name']
220221
));
221222

222223
// Did we succeed or have an error?

‎includes/objects/contribution.php‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1590,10 +1590,11 @@ public function change_permalink($new_permalink)
15901590
* @param array $custom_fields Custom field values.
15911591
* @param string $old_permalink Old permalink. Defaults to empty string.
15921592
* @param string|null $new_permalink Submitted permalink. Defaults to the value on the entity.
1593+
* @param string $old_name Old contribution name. Defaults to empty string.
15931594
*
15941595
* @return array Returns array containing any errors found.
15951596
*/
1596-
public function validate($contrib_categories, $authors, $custom_fields, $old_permalink = '', $new_permalink = null)
1597+
public function validate($contrib_categories, $authors, $custom_fields, $old_permalink = '', $new_permalink = null, $old_name = '')
15971598
{
15981599
phpbb::$user->add_lang('ucp');
15991600

@@ -1603,6 +1604,10 @@ public function validate($contrib_categories, $authors, $custom_fields, $old_per
16031604
{
16041605
$error[] = phpbb::$user->lang['EMPTY_CONTRIB_NAME'];
16051606
}
1607+
else if ($this->contrib_name !== $old_name && $this->is_link($this->contrib_name))
1608+
{
1609+
$error[] = phpbb::$user->lang['CONTRIB_NAME_IS_LINK'];
1610+
}
16061611

16071612
$metadata = array(
16081613
$this->contrib_name,
@@ -1800,6 +1805,17 @@ protected function generate_permalink_slug($value)
18001805
return preg_replace('/[^\p{L}\p{M}\p{N}_]+/u', '_', url::generate_slug($value));
18011806
}
18021807

1808+
/**
1809+
* Check whether a value is shaped like a link rather than a name.
1810+
*
1811+
* @param string $value
1812+
* @return bool
1813+
*/
1814+
protected function is_link($value)
1815+
{
1816+
return (bool) preg_match('#^(?:[a-z][a-z0-9+.\-]*://|www\.)#i', trim($value));
1817+
}
1818+
18031819
/*
18041820
* Validate a contrib permalink
18051821
*
@@ -1816,6 +1832,14 @@ public function validate_permalink($permalink, $old_permalink)
18161832
return false;
18171833
}
18181834

1835+
// Only allow the characters the permalink generator itself produces.
1836+
// A pasted link fails this outright, so no slugified link is ever
1837+
// echoed back as a valid permalink example.
1838+
if (!preg_match('/^[\p{L}\p{M}\p{N}_]*$/u', $permalink))
1839+
{
1840+
return phpbb::$user->lang['CONTRIB_PERMALINK_INVALID_CHARACTERS'];
1841+
}
1842+
18191843
$generated_permalink = $this->generate_permalink_slug($permalink);
18201844
if ($generated_permalink !== $permalink)
18211845
{

‎language/en/contributions.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
'CONTRIB_LOCAL_NAME' => 'Local name',
8080
'CONTRIB_LOCAL_NAME_EXPLAIN' => 'The localized name of the language, e.g. <em>Français</em>.',
8181
'CONTRIB_NAME' => 'Contribution Name',
82+
'CONTRIB_NAME_IS_LINK' => 'The contribution name cannot be a link. Please enter the name of your contribution.',
8283
'CONTRIB_NEW' => 'New',
8384
'CONTRIB_NONACTIVE_AUTHORS' => 'Non-Active Co-Authors (Past Contributors)',
8485
'CONTRIB_NONACTIVE_AUTHORS_EXPLAIN' => 'Non-Active Co-Authors can not manage anything for the contribution and are only listed as previous authors.',
@@ -87,6 +88,7 @@
8788
'CONTRIB_PERMALINK' => 'Contribution Permalink',
8889
'CONTRIB_PERMALINK_EXISTS' => 'That contribution permalink is already in use.',
8990
'CONTRIB_PERMALINK_EXPLAIN' => 'Cleaned version of the contribution name, used to build the url for the contribution.<br /><strong>Leave blank to have one automatically created based on the contribution name.</strong>',
91+
'CONTRIB_PERMALINK_INVALID_CHARACTERS' => 'The permalink may only contain letters, numbers and underscores. Leave the field blank to have a valid one created from the contribution name.',
9092
'CONTRIB_RELEASE_DATE' => 'Release date',
9193
'CONTRIB_STATUS' => 'Contribution status',
9294
'CONTRIB_STATUS_EXPLAIN' => 'Change the contribution status',

0 commit comments

Comments
 (0)