Commit e7dd56c4 authored by Damien McKenna's avatar Damien McKenna
Browse files

Issue #3158296 by marcosdr, opdavies, DamienMcKenna, paulocs: Enable to switch...

Issue #3158296 by marcosdr, opdavies, DamienMcKenna, paulocs: Enable to switch default link protocol between HTTP and HTTPS.
parent f9b82e47
......@@ -26,6 +26,8 @@ By DamienMcKenna: Unnecessary syntax change broke PHP 5.3 compatibility.
codebymikey, bondjimbond, tcnolan7, La558, timwright_, Ron Collins, Summit,
BrianLewisDesign, chegor, danbarron, SKrossa, wylbur, zorya: Query string,
fragment stripped from URL.
#3158296 by marcosdr, opdavies, DamienMcKenna, paulocs: Enable to switch default
link protocol between HTTP and HTTPS.
Link 7.x-1.7, 2019-11-14
......
<?php
/**
* @file
* Link admin settings.
*/
/**
* Form constructor for the settings form.
*
* @ingroup forms
*/
function link_admin_settings() {
$form['link_default_protocol'] = array(
'#type' => 'select',
'#title' => t('Protocol'),
'#options' => array(
LINK_HTTP_PROTOCOL => strtoupper(LINK_HTTP_PROTOCOL),
LINK_HTTPS_PROTOCOL => strtoupper(LINK_HTTPS_PROTOCOL),
),
'#default_value' => variable_get('link_default_protocol', LINK_HTTP_PROTOCOL),
'#description' => t('Select the default protocol to use for each link.'),
);
return system_settings_form($form);
}
......@@ -2,6 +2,7 @@ name = Link
description = Defines simple link field types.
core = 7.x
package = Fields
configure = admin/config/content/link
; Migrate integration.
files[] = link.migrate.inc
......@@ -16,6 +17,7 @@ files[] = tests/LinkBaseTestClass.test
; All of the actual tests.
files[] = tests/LinkConvertInternalPathsTest.test
files[] = tests/LinkDefaultProtocolTest.test
files[] = tests/LinkEntityTokenTest.test
files[] = tests/LinkFieldAttributesTest.test
files[] = tests/LinkFieldCrudTest.test
......
......@@ -36,6 +36,7 @@ function link_update_7002() {
*/
function link_uninstall() {
variable_del('link_allowed_domains');
variable_del('link_default_protocol');
}
/**
......
......@@ -18,6 +18,8 @@ define('LINK_TARGET_DEFAULT', 'default');
define('LINK_TARGET_NEW_WINDOW', '_blank');
define('LINK_TARGET_TOP', '_top');
define('LINK_TARGET_USER', 'user');
define('LINK_HTTP_PROTOCOL', 'http');
define('LINK_HTTPS_PROTOCOL', 'https');
/**
* Maximum URLs length - needs to match value in link.install.
......@@ -586,6 +588,18 @@ function _link_validate(&$item, $delta, $field, $entity, $instance, $langcode, &
'error_element' => array('url' => FALSE, 'title' => TRUE),
);
}
// Specific logic for when using the 'select' option on the title field.
if ($instance['settings']['title'] == 'select') {
// The title is required.
if (!empty($item['title']) && !empty($item['url'])) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'link_required',
'message' => t('The title field is required when a URL is provided.'),
'error_element' => array('url' => FALSE, 'title' => TRUE),
);
}
}
}
/**
......@@ -671,7 +685,7 @@ function _link_sanitize(&$item, $delta, &$field, $instance, &$entity) {
$item['external'] = TRUE;
}
}
$url = link_cleanup_url($item['url']);
$url = link_cleanup_url($item['url'], variable_get('link_default_protocol', LINK_HTTP_PROTOCOL));
$url_parts = _link_parse_url($url);
if (!empty($url_parts['url'])) {
......@@ -920,6 +934,21 @@ function _link_parse_str($query) {
return $query_array;
}
/**
* Implements hook_menu().
*/
function link_menu() {
$items['admin/config/content/link'] = array(
'title' => 'Link settings',
'description' => 'Settings for the link module.',
'page callback' => 'drupal_get_form',
'page arguments' => array('link_admin_settings'),
'access arguments' => array('access administration pages'),
'file' => 'link.admin.inc',
);
return $items;
}
/**
* Implements hook_theme().
*/
......@@ -1093,6 +1122,7 @@ function link_field_process($element, $form_state, $complete_form) {
'#description' => t('Select the a title for this link.'),
'#default_value' => isset($element['#value']['title']) ? $element['#value']['title'] : NULL,
'#options' => $options,
'#empty_value' => '',
);
}
......@@ -1523,6 +1553,12 @@ function link_views_api() {
* The protocol to be prepended to the url if one is not specified.
*/
function link_cleanup_url($url, $protocol = 'http') {
try {
link_ensure_valid_default_protocol();
}
catch (Exception $e) {
watchdog('link', $e->getMessage(), array(), WATCHDOG_ERROR);
}
$url = trim($url);
$type = link_url_type($url);
......@@ -1543,6 +1579,23 @@ function link_cleanup_url($url, $protocol = 'http') {
return $url;
}
/**
* Validate that the protocol is either HTTP or HTTPS.
*
* @throws Exception
*/
function link_ensure_valid_default_protocol() {
$protocol = variable_get('link_default_protocol', LINK_HTTP_PROTOCOL);
if ($protocol !== LINK_HTTP_PROTOCOL && $protocol !== LINK_HTTPS_PROTOCOL) {
variable_set('link_default_protocol', LINK_HTTP_PROTOCOL);
throw new Exception(t('Protocol was set to !protocol but must be !HTTP or !HTTPS. Set to default HTTP.', array(
'!protocol' => $protocol,
'!HTTP' => LINK_HTTP_PROTOCOL,
'!HTTPS' => LINK_HTTPS_PROTOCOL,
)));
}
}
/**
* Validates a URL.
*
......
<?php
/**
* Test setting a global protocol, and using it for links when needed.
*
* Ensure that any links that are created not using an explict protocol (e.g.
* a link entered as www.drupal.org, without a http:// or https:// prefix) will
* use the globally defined protocol.
*
* Any links created with an explicit protocol will continue to use that
* protocol and will ignore the global setting.
*/
class LinkDefaultProtocolTest extends LinkBaseTestClass {
/**
* Get Info.
*/
public static function getInfo() {
return array(
'name' => 'Set default link protocol',
'description' => 'Test that an url with a default protocol set.',
'group' => 'Link',
);
}
/**
* A link without a default protocol uses the default (HTTP).
*/
public function testHttpDefaultProtocolOnUrlWithoutDefinedProtocol() {
$user = $this->drupalCreateUser($this->permissions);
$this->drupalLogin($user);
$this->createNodeWithLink('www.example.com');
$this->drupalGet('node/1');
$this->assertRaw('http://www.example.com');
}
/**
* The protocol is set to HTTP by default if a wrong protocol has been set.
*/
public function testHttpDefaultProtocolFromVariableConf() {
variable_set('link_default_protocol', 'banana');
try {
link_ensure_valid_default_protocol();
}
catch (Exception $e) {
}
$protocol = variable_get('link_default_protocol');
$this->assertEqual($protocol, LINK_HTTP_PROTOCOL);
}
/**
* A link with no defined protocol in the URL uses the default (HTTPS).
*/
public function testHttpsDefaultProtocolOnUrlWithoutDefinedProtocol() {
$this->setupDefaultProtocol(LINK_HTTPS_PROTOCOL);
$this->createNodeWithLink('www.example.com');
$this->drupalGet('node/1');
$this->assertRaw('https://www.example.com');
}
/**
* A link with a defined protocol HTTPS overrides the default HTTP protocol.
*/
public function testHttpDefaultProtocolOnUrlWithDefinedHttpsProtocol() {
$this->setupDefaultProtocol(LINK_HTTP_PROTOCOL);
$this->createNodeWithLink('https://www.example.com');
$this->drupalGet('node/1');
$this->assertRaw('https://www.example.com');
}
/**
* A link with a defined protocol HTTP overrides the default HTTPS protocol.
*/
public function testHttpsDefaultProtocolOnUrlWithDefinedHttpProtocol() {
$this->setupDefaultProtocol(LINK_HTTPS_PROTOCOL);
$this->createNodeWithLink('http://www.example.com');
$this->drupalGet('node/1');
$this->assertRaw('http://www.example.com');
}
/**
* Setup the default global link protocol to either HTTP or HTTPS.
*
* @param string $protocol
* The protocol to use.
*/
private function setupDefaultProtocol($protocol) {
$user = $this->drupalCreateUser($this->permissions);
$this->drupalLogin($user);
$this->drupalPost(
'admin/config/content/link',
array('link_default_protocol' => $protocol),
'Save configuration'
);
}
/**
* Create a page node with a link field.
*
* @param string $url
* The link URL.
*
* @return object
* The created node.
*/
private function createNodeWithLink($url) {
$link_field = $this->createLinkField('page');
$settings = array(
'title' => 'Basic page link test',
$link_field => array(
LANGUAGE_NONE => array(
array(
'title' => 'Field protocol link Test',
'url' => $url,
),
),
),
);
return $this->drupalCreateNode($settings);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment